Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to populate and control the one qcombo box elements from another qcombo box



  • Hi All,
    I have two qcombo boxes are there if i have choose one parameter from first qcombo box associated with that parameters only need to display how to do it ...?


  • Lifetime Qt Champion

    Hi
    Your sentence is a bit broken ;)
    You mean ?
    You have 2 combos
    Combo 1 display
    aaa
    bbb
    cccc
    Combo 2 display
    nothing yet.

    You click on aaa
    now Combo 2 display
    Related to aaa

    Kind of way ?



  • Your question is very unclear. try giving us an example of what you want to do.

    Spoiler Alert:
    99.9999999% chance the answer is "use the same model for the 2 combo boxes"



  • first qcombox having ticket_printer,bill_acceptor
    second qcombox having slot_admin,smartcard,tckt_in,tckt_out,bill_acceptor_in,bill_acceptor_out

    if i choose bill_acceptor in first qcombo box ,in second qcombo box need to show bill_acceptor_in,bill_acceptor_out only not all in second qcombo box .....



  • Hi All,
    Now you got it or shall i send the screen shot of the gui ?



  • QAbstarctItemModel* model = new QStandardItemModel(this);
    model->insertColumn(0);
    model->insertRows(0,2);
    auto rootIdx = model->index(0,0);
    model->setData(rootIdx,"ticket_printer");
    model->insertColumn(0,rootIdx);
    model->insertRows(0,4,rootIdx);
    model->setData(model->index(0,0,rootIdx),"slot_admin");
    model->setData(model->index(1,0,rootIdx),"smartcard");
    model->setData(model->index(2,0,rootIdx),"tckt_in");
    model->setData(model->index(3,0,rootIdx),"tckt_out");
    rootIdx = model->index(1,0);
    model->setData(rootIdx,"bill_acceptor");
    model->insertColumn(0,rootIdx);
    model->insertRows(0,2,rootIdx);
    model->setData(model->index(0,0,rootIdx),"bill_acceptor_in");
    model->setData(model->index(1,0,rootIdx),"bill_acceptor_out");
    combo1->setModel(model);
    combo2->setModel(model);
    connect(combo1, QOverload<int>::of(&QComboBox::currentIndexChanged),[=](int idx){combo2->setRootModelIndex(model->index(idx,0)); });
    


  • @VRonin said in how to populate and control the one qcombo box elements from another qcombo box:

    QAbstarctItemModel* model = new QStandardItemModel(this);

    @VRonin ,
    Thanks a lot ,i have already dialog gui window everything i put into the qcombo boxes.here in this above code you are manually creating it ?



  • Yes, Qt Designer doesn't allow to do this so you have to do it manually in the code.

    That thing goes just after ui->setupUi(this);



  • This post is deleted!


  • @VRonin
    okay fine ... i will try this



  • An alternative, suggested by @mrjj is to use a QSortFilterProxy model on the second combo to filter out options you don't want to see


Log in to reply