How to properly create a two way connection with QComboBox?
-
I have a main window with a
QTabWidget
. The main window also has a child dialog widget with aQcomboBox
. 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? -
Simply check if the current index in the QComboBox is the selected one and if then don't call setCurrentIndex()
-
Where do I start calling when going from tab Change to QcomboBox change?
-
@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.
-
Hi,
You can do that at the top of each slot.