Results 1 to 7 of 7

Thread: Function running on its own

  1. #1
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Function running on its own

    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:
    Qt Code:
    1. AvailableDialog::AvailableDialog(BomTableModel *model, QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::AvailableDialog)
    4. {
    5. // ...
    6.  
    7. // for(int i = 1; i < maxProjects + 1; i++)
    8. // ui->quantityCombo->addItem(QString::number(i));
    9.  
    10. qDebug() << "fillTable being called";
    11. fillTable();
    12. qDebug() << "fillTable finished";
    13. }
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Function running on its own

    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?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    admkrk (4th September 2017)

  4. #3
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Function running on its own

    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!

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Function running on its own

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #5
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Function running on its own

    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!

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Function running on its own

    Alternatively, you could bracket the for() loop in your constructor with calls to blockSignals( true ) and blockSignals( false ) through the combo box pointer.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #7
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Function running on its own

    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...

Similar Threads

  1. Replies: 12
    Last Post: 18th September 2014, 19:54
  2. Replies: 1
    Last Post: 8th January 2012, 11:33
  3. show a rotating circle when running a busy function
    By hoffen in forum Qt Programming
    Replies: 1
    Last Post: 11th October 2011, 09:33
  4. Running an external function in a new thread
    By Andrea Landi in forum Newbie
    Replies: 3
    Last Post: 29th May 2011, 04:05
  5. Replies: 1
    Last Post: 17th May 2006, 00:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.