I have 20 elements stored in vector, what I want to do is that :
when user selects one element from drop down list (Combo Box) the Value of Source or Destination of My Navigation (Routing) gets the unsigned int value with same index of element on Combo Box from the vector, how can I do That with Combo Box on Qt ?
//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 allows you to store additional information through the roles that can be accessed through 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"