Results 1 to 4 of 4

Thread: One function in connect() and QTimer::singleShot()

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: One function in connect() and QTimer::singleShot()

    QTimer::singleShot(8500, this, SLOT(searchTable(50)));
    First thing: this is not allowed. You cannot pass an argument in a connect() statement (which is what this does, in effect - it says to connect the QTimer's timeout() signal to your searchTable() slot). However the function signatures for the signal and slot must match. QTimer::timeout() does not pass any arguments, so you can't make something up for it to pass to your slot.

    QObject::connect: No such slot MainWindow::searchTable(50) in ../NewApp/mainwindow.cpp:70
    Second, this occurs because SLOT() is a C++ macro, which converts its argument into a literal string. So your SLOT() argument is being passed as the string "searchTable(50)" and internally the connect() method is trying to match it up with a method defined in MainWindow named "searchTable(50)", which of course there isn't one. That's what the compiler error message is telling you.

    So, if what you want to do is to fire off an initial search of the table 8500 ms after your program starts (technically, 8500 ms after the MainWindow constructor exits), then you need to create a slot that takes no arguments, connect that to the timeout signal, then within that slot, call searchTable( 50 ):

    Qt Code:
    1. private slots: // in MainWindow class
    2. void doInitialSearch();
    3. void searchTable( int searchID );
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent):
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. // SEARCH TABLE UPDATE
    8. connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(searchTable(int)));
    9.  
    10. // LOAD TABLES
    11. QTimer::singleShot( 8500, this, SLOT( doInitialSearch() ) );
    12.  
    13. // etc..
    14. }
    15.  
    16. void MainWindow::doInitialSearch()
    17. {
    18. searchTable( 50 );
    19. }
    20.  
    21. void MainWindow::searchTable(int searchID)
    22. {
    23. //my code
    24. }
    To copy to clipboard, switch view to plain text mode 

    What are you trying to do with this 8 1/2 second delay before you do the initial search? There are probably better ways to do it than make your user sit and watch the main window for that long while nothing happens.

    If all you want is to ensure that the MainWindow is visible before doing this search, then you can implement an override of the showEvent() and make a direct call to searchTable() in that:

    Qt Code:
    1. void MainWIndow::showEvent( QShowEvent * pEvent )
    2. {
    3. QMainWindow::showEvent( pEvent ); // same as super.showEvent() in python
    4.  
    5. searchTable( 50 );
    6. }
    To copy to clipboard, switch view to plain text mode 

    or you can use a QTimer with zero timeout to accomplish the same thing. The timer signal will fire immediately after control returns to the Qt event loop (ie. after the showEvent() method exits).

    Qt Code:
    1. void MainWIndow::showEvent( QShowEvent * pEvent )
    2. {
    3. QMainWindow::showEvent( pEvent ); // same as super.showEvent() in python
    4.  
    5. QTimer::singleShot( 0, this, SLOT( doInitialSearch() ) );
    6. }
    To copy to clipboard, switch view to plain text mode 

    But I suspect there is no need for you to wait for the MainWindow to be visible before doing the initial search. Even if the window is not visible, all of the QWidgets in it are complete after the call to setupUi(), so you can fill tables, etc. and they will become visible when the MainWindow does.
    <=== 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.

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

    ZajtiM (11th September 2021)

Similar Threads

  1. QTimer::singleShot(0, ... confusion
    By bjoern83 in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2022, 10:30
  2. QTimer::singleShot in while
    By Ema in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2019, 20:45
  3. QTimer::singleShot stop at 12:00AM on second day
    By croliver in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2016, 17:37
  4. Replies: 3
    Last Post: 31st January 2010, 16:56
  5. Can we connect QTimer::SingleShot with a slot taking arguments?
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 17th September 2008, 18:02

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
  •  
Qt is a trademark of The Qt Company.