Important: Please read the Qt Code of Conduct -

How to use std::thread in MainWindow ?

  • Dear Sirs,

    I am trying to add a thread function in the mainwindow.c as below:

    void MainWindow::OutputValue( )
      for( int i = 0; i < 125; ++ i )
        std::this_thread::sleep_for( std::chrono::duration<int, std::milli>( 5 ) );
        qDebug() << "inside thread function, i=" << i;
    void MainWindow::on_pushButton_13_clicked()
         std::thread mThread(OutputValue);
         qDebug()<<"Main Thread is running";

    But it generate multiple error,:

    1. error: static assertion failed: Wrong number of arguments for pointer-to-member
    2. error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (MainWindow::*)

    But if I put the code inside the main.cpp of "Qt Console Application" project, instead of mainwindow.cpp of "Qt Widgets Application " project.
    Then no error happen.

    Why I cannot write that code inside the MainWindow ?

    PS. I tried movetothread is working, but I want to test std.

    Any suggestions is appreciate~


  • @Hiloshi
    Do those two error messages from the compiler not specify a line number in your code?

  • The errors are quite clear. OutputValue is a non-static member function so it is asking you what object should that function be called from. change it to std::thread mThread(OutputValue,this);.

    I'm not getting into safety details here as the above seems like just an example code

  • Dear @VRonin ,

    It is working, thank you very much.


Log in to reply