Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use std::thread in MainWindow ?

How to use std::thread in MainWindow ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 3.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hiloshi
    wrote on last edited by Hiloshi
    #1

    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";
         mThread.join();
    }
    

    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~

    Thanks,

    JonBJ 1 Reply Last reply
    0
    • H Hiloshi

      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";
           mThread.join();
      }
      

      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~

      Thanks,

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • H Offline
          H Offline
          Hiloshi
          wrote on last edited by
          #4

          Dear @VRonin ,

          It is working, thank you very much.

          Thanks,

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved