Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to properly create a two way connection with QComboBox?

    General and Desktop
    qcombobox
    3
    6
    416
    Loading More Posts
    • 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.
    • L
      lansing last edited by lansing

      I have a main window with a QTabWidget. The main window also has a child dialog widget with a QcomboBox. The name of the tab in the tabwidget will be the selections of the qcombobox. The QTabWidget and the QComboBox were created in their corresponded ui files.

      What I want now is that when I switched tab in the main window, I want the selection in the qcombobox to also be changed, matching the tab index. And vise versa when I changed selection in qcombobox, I want the tab to also be changed.

      My signal connection now works only one way going from qcombobox to qtabwidget. When I tried to connect them the other way around I was getting into an infinite loop that I don't know how to get out.

      main window.cpp

      // somewhere on top
      m_pMyDialog = new MyDialog(this);
      
      connect(m_pMyDialog, &MyDialog::signalComboBoxChanged,
                  this, &MainWindow::slotSetComboBox);
      
      connect(m_ui->tabWidget, &QTabWidget::currentChanged,
                  this, &MainWindow::slotChangeTab);
      
      
      void MainWindow::slotChangeTab(int a_index) 
      {
          // some logic
          m_pMyDialog->slotUpdateSelection(a_index);
      }
      
      void MainWindow::slotSetComboBox(int a_index)
      {
         // some logic to talk to combobox in mydiag.cpp   
          m_pMyDialog->slotSetSomeValue(some value);
      
          m_ui->tabWidget->setCurrentIndex(a_index);
      }
      

      mydialog.cpp

      connect(ui->selectComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), 
          this, MyDialog::signalComboBoxChanged);
      
      
      void MyDialog::slotUpdateSelection(int a_index) 
      {
          if (ui->selectComboBox->currentIndex() != a_index)    
              ui->selectComboBox->setCurrentIndex(a_index);
      }
      

      I can make a function in mydialog.cpp to update combobox selection and called it in slotChangeTab, but that will get me into an infinite loop. What is the proper way to do this?

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        Simply check if the current index in the QComboBox is the selected one and if then don't call setCurrentIndex()

        Qt has to stay free or it will die.
        Online Installer direct download: https://download.qt.io/official_releases/online_installers/

        L 1 Reply Last reply Reply Quote 2
        • L
          lansing @Christian Ehrlicher last edited by

          @Christian-Ehrlicher

          Where do I start calling when going from tab Change to QcomboBox change?

          Christian Ehrlicher 1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion @lansing last edited by

            @lansing said in How to properly create a two way connection with QComboBox?:

            Where do I start calling when going from tab Change to QcomboBox change?

            I do not understand what you mean.

            Qt has to stay free or it will die.
            Online Installer direct download: https://download.qt.io/official_releases/online_installers/

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              Hi,

              You can do that at the top of each slot.

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

              L 1 Reply Last reply Reply Quote 0
              • L
                lansing @SGaist last edited by

                @SGaist

                Thanks I got it working, I don't know why it works but I'll take it. I updated the first post for future reference.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post