Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Make 2 QCombobox dependent
Forum Updated to NodeBB v4.3 + New Features

Make 2 QCombobox dependent

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike_newbye
    wrote on last edited by
    #1

    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.

    Kind regards,
    Mikael

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mike_newbye
        wrote on last edited by
        #3

        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.

        Kind regards,
        Mikael

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved