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. How to properly create a two way connection with QComboBox?
QtWS25 Last Chance

How to properly create a two way connection with QComboBox?

Scheduled Pinned Locked Moved Solved General and Desktop
qcombobox
6 Posts 3 Posters 1.1k Views
  • 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 Offline
    L Offline
    lansing
    wrote on last edited by lansing
    #1

    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
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      L 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

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

        L Offline
        L Offline
        lansing
        wrote on last edited by
        #3

        @Christian-Ehrlicher

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

        Christian EhrlicherC 1 Reply Last reply
        0
        • L lansing

          @Christian-Ehrlicher

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

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

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

            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
            0
            • SGaistS SGaist

              Hi,

              You can do that at the top of each slot.

              L Offline
              L Offline
              lansing
              wrote on last edited by
              #6

              @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
              0

              • Login

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