gpt4 book ai didi

c++ - 如何将Qt中的组合框元素与具有相同索引的静态 vector 中的预定义元素链接?

转载 作者:行者123 更新时间:2023-12-02 10:23:11 24 4
gpt4 key购买 nike

我在 vector 中存储了 20个元素,我想要做的是:
当用户从下拉列表(组合框)中选择一个元素时,我的导航(路由)的源或目的地的值从 vector 中获得与组合框上元素相同的索引的无符号int值,如何我用Qt上的Combo Box来做吗?
My GUI Interface

//Start
void MainWindow::fill_MyPlaces(){
MyPlaces.push_back(make_pair("Aldi Store",1545694404)); //1 Aldi
MyPlaces.push_back(make_pair("Restaurant Le Fut",1544558413)); //2 Restaurant Le Fut
MyPlaces.push_back(make_pair("Place Schnider",1455348628)); //3 place schnider
MyPlaces.push_back(make_pair("Acacias Residence",1540689869)); //4 Acacias
MyPlaces.push_back(make_pair("IUT University",442028765)); //5 IUT
MyPlaces.push_back(make_pair("Train Station (Gare du Creusot)",1387416514)); //6 Train Station (Gare du Creusot)
MyPlaces.push_back(make_pair("Carrefour Shopping Center",1829507841)); //7 Carrefour
MyPlaces.push_back(make_pair("Condorcet Center University",2019043458)); //8 Condorcet
MyPlaces.push_back(make_pair("Hospital Creusot",2258325836)); //9 Hospital Creusot
MyPlaces.push_back(make_pair("BNP PariBank",1544079469)); //10 BNP PariBank
MyPlaces.push_back(make_pair("Acion Store",889114982)); //11 Action Store
MyPlaces.push_back(make_pair("El-Loco Cafe",1540689876));//12 El-Loco Cafe
MyPlaces.push_back(make_pair("Residence Jean Monnent",2244694498));//13 Residence Jean Monnent
MyPlaces.push_back(make_pair("Crous Office & Restaurant",1540680545));//14 Crous Office & Restaurant
MyPlaces.push_back(make_pair("Euro Lav(Laundry)",4846313478));//15 Euro Lav(Laundry)
MyPlaces.push_back(make_pair("Pharmacie des Acacias",1540689863));//16 Pharmacie des Acacias
MyPlaces.push_back(make_pair("Centre Medico Scolaire (Medical school)",1544558417));//17 centre medico scolaire (Medical school)
MyPlaces.push_back(make_pair("Driving School",1544558167));//18 Driving School
MyPlaces.push_back(make_pair("Le Poste",2019027442));//19 Le Poste
MyPlaces.push_back(make_pair("MCDonld's",6269205284));//20 McDonald's
}
//End

最佳答案

QComboBox允许您通过可以通过itemData()访问的角色来存储其他信息:

#include <QtWidgets>

class Widget: public QWidget{
Q_OBJECT
public:
Widget(QWidget *parent=nullptr):
QWidget(parent), combo(new QComboBox), label(new QLabel)
{
QVBoxLayout *lay = new QVBoxLayout{this};
lay->addWidget(combo);
lay->addWidget(label);
connect(combo,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&Widget::onCurrentIndexChanged);

for(const std::pair<QString, int> & p: std::vector<std::pair<QString, int>>{
{"Aldi Store", 1545694404}, //1 Aldi
{"Restaurant Le Fut",1544558413}, //2 Restaurant Le Fut
{"Place Schnider",1455348628}, //3 place schnider
{"Acacias Residence",1540689869}, //4 Acacias
{"IUT University",442028765}, //5 IUT
{"Train Station (Gare du Creusot)",1387416514}, //6 Train Station (Gare du Creusot)
{"Carrefour Shopping Center",1829507841}, //7 Carrefour
{"Condorcet Center University",2019043458}, //8 Condorcet
{"Hospital Creusot",2258325836}, //9 Hospital Creusot
{"BNP PariBank",1544079469}, //10 BNP PariBank
{"Acion Store",889114982}, //11 Action Store
{"El-Loco Cafe",1540689876},//12 El-Loco Cafe
{"Residence Jean Monnent",2244694498},//13 Residence Jean Monnent
{"Crous Office & Restaurant",1540680545},//14 Crous Office & Restaurant
{"Euro Lav(Laundry)",4846313478},//15 Euro Lav(Laundry)
{"Pharmacie des Acacias",1540689863},//16 Pharmacie des Acacias
{"Centre Medico Scolaire (Medical school)",1544558417},//17 centre medico scolaire (Medical school)
{"Driving School",1544558167},//18 Driving School
{"Le Poste",2019027442},//19 Le Poste
{"MCDonld's",6269205284},//20 McDonald's
}){
combo->addItem(p.first, p.second);
}
}
public slots:
void onCurrentIndexChanged(int index){
int value = combo->itemData(index).value<int>();
label->setNum(value);
}
private:
QComboBox *combo;
QLabel *label;
};


int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}

#include "main.moc"

关于c++ - 如何将Qt中的组合框元素与具有相同索引的静态 vector 中的预定义元素链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59123512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com