我对QMap有问题。每个键都有许多值。想法是用其唯一键显示所有值。要使用QtGui应用程序遍历此 map ,我拥有TextEdit(名称:t2)。代码位于按钮内部,其内容如下:
void MainWindow::on_push2_clicked()
{
QMap<int, QStringList> myMaps;
myMaps[1].append("Autodesk Maya 2014");
myMaps[1].append("Autodesk Inventor 2014");
myMaps[1].append("Autodesk Alias 2014");
myMaps[1].append("Autodesk 3ds Max 2014");
myMaps[1].append("Autodesk Softimage 2014");
myMaps[2].append("Adobe Photoshop CS6");
myMaps[2].append("Adobe Illustrator CS6");
myMaps[2].append("Adobe InDesign CS6");
myMaps[1].append("Autodesk AutoCAD 2014");
QStringList stsl; // the StringList which contains all values
QMap<int, QStringList>::iterator it;
for (it=myMaps.begin(); it!=myMaps.end(); ++it) {
QString qsi=QString::number(it.key());
QString qs=it.value().join(" - ");
stsl << "<span style='color:#FF1000;font-weight:bold;'>"+qsi + " : </span>"+ qs;
}
QString st4=stsl.join("<br/>"); //With setHtml
ui->t2->setHtml(st4);
}
}
// 输出 :
1:Autodesk Maya 2014-Autodesk Inventor 2014-Autodesk Alias 2014-Autodesk 3ds Max 2014-Autodesk Softimage 2014-Autodesk AutoCAD 2014-Lightwave 12
2:Adobe Photoshop CS6-Adobe Illustrator CS6-Adobe InDesign CS6。
//我要执行的输出:
1:Autodesk Maya 2014
1:Autodesk Inventor 2014
1:Autodesk Alias 2014
1:Autodesk 3ds Max 2014
1:Autodesk Softimage 2014
1:Autodesk AutoCAD 2014
2:Adobe Photoshop CS6
2:Adobe Illustrator CS6
2:Adobe InDesign CS6
最佳答案
我将以以下方式重写它:
[..]
QMultiMap<int, QString> myMaps;
myMaps.insert(1, "Autodesk Maya 2014");
myMaps.insert(1, "Autodesk Inventor 2014");
myMaps.insert(1, "Autodesk Alias 2014");
myMaps.insert(1, "Autodesk 3ds Max 2014");
myMaps.insert(1, "Autodesk Softimage 2014");
myMaps.insert(2, "Adobe Photoshop CS6");
myMaps.insert(2, "Adobe Illustrator CS6");
myMaps.insert(2, "Adobe InDesign CS6");
myMaps.insert(1, "Autodesk AutoCAD 2014");
QMap<int, QString>::iterator it;
QString output;
for (it = myMaps.begin(); it != myMaps.end(); ++it) {
// Format output here.
output += QString("%1 : %2").arg(it.key()).arg(it.value());
}
[..]
关于c++ - 如何使用QStringList显示QMap中的所有键?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18645134/