Make 2 QCombobox dependent



  • Hi,

    I have 2 combo boxes, and I want to enable/disable current indexes dependent on the index of the other one. The disable part goes well. But I cannot figure out how to enable the index back when it is in acceptable.

    @void MainWindow::on_vert_axis_dropbox_currentIndexChanged()
    {
    int v = ui->vert_axis_dropbox->currentIndex();
    ui->hor_axis_dropbox->setItemData(v, 0, Qt::UserRole -1);
    y = v;
    ui->hor_axis_dropbox->setItemData(y, 1, Qt::UserRole -1);
    }

    void MainWindow::on_hor_axis_dropbox_currentIndexChanged()
    {
    int h = ui->hor_axis_dropbox->currentIndex();
    ui->vert_axis_dropbox->setItemData(h, 0, Qt::UserRole -1);
    x = h;
    ui->vert_axis_dropbox->setItemData(x, 1, Qt::UserRole -1);
    }@

    This part automatically disables the index if it is equal to the index of the other combo box.
    @ui->vert_axis_dropbox->setItemData(h, 0, Qt::UserRole -1);@
    @ui->hor_axis_dropbox->setItemData(v, 0, Qt::UserRole -1);@

    I guess this part should reactivate that index if it is changed, and is not equal to the index of the other one.
    @ui->hor_axis_dropbox->setItemData(y, 1, Qt::UserRole -1);@
    @ui->vert_axis_dropbox->setItemData(x, 1, Qt::UserRole -1);@

    But actually it is not enabled.

    Thanks in advance.


  • Lifetime Qt Champion

    Hi,

    This is not the way to do it, you should rather do something like:
    @
    QStandardItemModel *model = qobject_cast<QStandardItemModel *>(combobox->model());
    QStandardItem *item = model->item(itemIndex);
    item->setEnabled(enableCondition);
    @

    Hope it helps



  • Hi,

    Thanks for the answer.

    I made privates in .h file:

    @ QStandardItemModel *model_h;
    QStandardItem *item_h;
    QStandardItemModel *model_v;
    QStandardItem *item_v;@

    then applied them in standard constructor:

    @model_h = qobject_cast<QStandardItemModel *>(ui->hor_axis_dropbox->model());
    item_h = model_h->item(ui->hor_axis_dropbox->currentIndex());

    model_v = qobject_cast<QStandardItemModel *>(ui->vert_axis_dropbox->model());
    item_v = model_v->item(ui->vert_axis_dropbox->currentIndex());@
    

    After it when wanting to enable back the current index i did:

    @void MainWindow::on_hor_axis_dropbox_currentIndexChanged()
    {
    ...
    item_h->setEnabled(h!=ui->vert_axis_dropbox->currentIndex());
    }

    void MainWindow::on_vert_axis_dropbox_currentIndexChanged()
    {
    ...
    item_v->setEnabled(v!=ui->hor_axis_dropbox->currentIndex());
    }@

    But the application didn't even start
    bq. The program has unexpectedly finished.
    C:\Qt\latest test\build-Prototype-Desktop_Qt_5_1_0_MinGW_32bit-Debug\debug\Prototype.exe exited with code -1073741819

    Could you please help me to solve it? I am new in it.

    Thanks.


  • Lifetime Qt Champion

    Run it through a debugger, It'll show you where it fails. Probably some pointer problem


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.