PDA

View Full Version : Function running on its own



admkrk
3rd September 2017, 23:00
I am probably overlooking something simple, but do not understand why this function runs without being called. At the end of the constructor I have:

AvailableDialog::AvailableDialog(BomTableModel *model, QWidget *parent) :
QDialog(parent),
ui(new Ui::AvailableDialog)
{
// ...

// for(int i = 1; i < maxProjects + 1; i++)
// ui->quantityCombo->addItem(QString::number(i));

qDebug() << "fillTable being called";
fillTable();
qDebug() << "fillTable finished";
}
With the loop commented out, it works as expected. The loop just fills a combo box with numbers (1 - 10). My debug output is:

fillTable being called
fillTable starting
0
1
2
3
4
5
6
7
8
fillTable ending
fillTable finished
The lines, other than the first and last are inside the function. If I uncomment the loop, the output is:

fillTable starting
0
...
8
fillTable ending
fillTable being called
fillTable starting
0
...
8
fillTable ending
fillTable finished
I originally had the loop after the function call, and noticed that it jumped right back to the function after completing the loop, when I was stepping through it. In that case the output was:

fillTable being called
fillTable starting
0
...
8
fillTable ending
fillTable finished
fillTable starting
0
...
8
fillTable ending
As you can see, for some reason, the code appears to jump inside the function, after the loop ends. I cannot think of any reason for this behavior.

d_stranz
4th September 2017, 01:34
I cannot think of any reason for this behavior.

I can think of one: Is fillTable() a slot? Have you connected it (in Qt Designer, maybe) to a signal from your combo box?

admkrk
4th September 2017, 02:30
No, it is a normal function, but that did point me in the right direction. I forgot I called it when the index changes, which is emitted when the combo box is created. I am not sure how to get around that, but at least I know what the problem is now. Thanks!

d_stranz
4th September 2017, 19:51
I am not sure how to get around that

If you are doing this in the constructor, then the combo box is created but not yet visible. In the slot, you can check isVisible() and only call fillTable() if it returns true.

admkrk
4th September 2017, 22:01
If you are doing this in the constructor, then the combo box is created but not yet visible.
I did not realize that. That is certainly much cleaner than what I was planning. Thanks again!

d_stranz
5th September 2017, 23:07
Alternatively, you could bracket the for() loop in your constructor with calls to blockSignals( true ) and blockSignals( false ) through the combo box pointer.

admkrk
6th September 2017, 01:48
That might be another option, but I think it might be better suited for a different situation. Now that I know the combo box is not visible, at that point, I think the first way is more readable.

Now if I can just get my queries to work right...