Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: -how to run a function in separate thread

  1. #1
    Join Date
    Dec 2012
    Posts
    45
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default -how to run a function in separate thread

    Hello
    I want to run a function in separate thread because in that function there is very heavy calculation and it freezes my ui for the time, and also because it is taking too much time.
    This is my function.

    void SRP_Ray_Plot(double No_Of_Rays, double Max_Range,double Source_Depth,double Initial_Angle, double Angle_Difference);
    and this function is currently being called on button click.

    I tried to run with QtConcurent. Like this.
    Qt Code:
    1. QFuture<void> future = QtConcurrent::run(SRP_Ray_Plot,SIP_number_of_rays,SIP_horizontal_range,SIP_source_depth,SIP_ray_initial_angle,SIP_Angle_Difference);
    To copy to clipboard, switch view to plain text mode 
    But i get the following error
    D:/Shivendra ]/SRP_24_Oct/mainwindow.h:42: QtConcurrent: No such file or directory
    Please help me with this one.

  2. #2
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: -how to run a function in separate thread

    I would create a class subclassing OObject and do the calculation there and run it as a separate thread ie. worker thread using MoveToThread() function.

    Good Luck.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: -how to run a function in separate thread

    Qt4 or Qt5?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Dec 2012
    Posts
    45
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: -how to run a function in separate thread

    can any one tell me whats wrong in the following code
    mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include<QThread>
    #include <QDebug>
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    protected:
    void changeEvent(QEvent *e);

    private slots:
    void on_pushButton_clicked();
    void debug();

    private:
    Ui::MainWindow *ui;
    };
    class MyThread : public QThread
    {
    Q_OBJECT


    public:
    void run();

    signals:

    void v_update();
    void value();
    };


    #endif // MAINWINDOW_H
    mainwindow.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    MyThread thread;
    connect(&thread,SIGNAL(v_update()),this,SLOT(debug ()));
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::changeEvent(QEvent *e)
    {
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
    default:
    break;
    }
    }
    void MainWindow::debug()
    {
    qDebug()<<"hello";
    }

    void MyThread::run()
    {
    for (int i=0;i<=10;i++)
    {
    emit this->v_update();

    }
    this->quit();
    }

    void MainWindow:n_pushButton_clicked()
    {
    MyThread thread;
    thread.run();

    }

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: -how to run a function in separate thread

    MyThread thread; will be destroyed when it goes out of ctor.
    You need to create an object in heap.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Dec 2012
    Posts
    45
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: -how to run a function in separate thread

    even that too dont work.
    Problem is that thread is no emitting the signal.

  7. #7
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: -how to run a function in separate thread

    Where are you connecting the threads signal to the debug slot ?
    in the constructor giving it a reference of a thread?
    But then in your onClickButton method you are creating another thread and not connecting signals and slots.
    So easily solved by removing what you wrote in the constructor ie :
    Quote Originally Posted by shivendra46d
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. MyThread thread;
    7. connect(&thread,SIGNAL(v_update()),this,SLOT(debug ()));
    8. }
    To copy to clipboard, switch view to plain text mode 
    Now in your OnClickButton method add the same code you deleted ie:
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. MyThread thread;
    4. connect(&thread,SIGNAL(v_update()),this,SLOT(debug ()));
    5. thread.run();
    6. }
    To copy to clipboard, switch view to plain text mode 
    Even for better practice create the thread in a heap rather than a stack.
    Good Luck.
    Last edited by toufic.dbouk; 24th October 2013 at 14:48.

  8. #8
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: -how to run a function in separate thread

    I haven't written Qt multithreaded apps so far. I didn't need them but, AFAIK from writing multithreaded apps under other environments...

    MyThread must not be on the stack - neither in the ctor nor in the push button handler. The thread.run() triggers the thread and returns immediately. Therefore, the push button handler creates a thread, connects it, runs it but then kills it immediately and destroys the thread object - or rather crashes the app in Qt. See documentation of QThread.

    (1) MyThread must be a member of MyWindow so that the thread object gets destroyed when MyWindow does (and MyThread has already finished). Even then MyThread needs some "stop me ASAP" flag which the thread is testing periodically and which you set in the MyWindow dtor and then wait until you get isFinished() from MyThread.
    (2) Another possibility is creating MyThread dynamically, again with the "stop me ASAP" flag and setting the flag in the MyThread dtor (and wainting with return from the dtor until you get isFinished()).

  9. #9
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: -how to run a function in separate thread

    @Radek,
    You went too far, his question was simple. And the mentioned method doesn't lead to a crash of the application if taken of properly.
    Emit a finish signal when the threads job is done and connect it to the desired slot , maybe a deleteLater slot depending on the implementation.
    Normally i wouldn't do threads in this way, i would subclass QObject and use moveToThread when needed then start the thread with the correct signals and slots to take care of managing and deleting properly.
    Its a sure thing that its for the best to create it on the heap for several reasons.
    The users method is not wrong but it has its own cases to use it same as all other implementations.

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: -how to run a function in separate thread

    The users method is not wrong (...)
    Yeah, except it doesn't run anything in separate thread this way
    OP: can you answer wysota's question ? I think he wanted to help you with this error
    QtConcurrent: No such file or directory

  11. #11
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: -how to run a function in separate thread

    Quote Originally Posted by stampede
    ...except it doesn't run anything in separate thread this way
    Why ?
    Deriving a class from QThread, re-implementing the QThread::run() method, and using QThread::start() to run it will make sure that that method is run in a different thread.
    So all he has to do is create the thread in a heap and call start().

    And yes he hasn't answered the main question i am still waiting whether he is using Qt4 or Qt5.

  12. #12
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: -how to run a function in separate thread

    I mean this way, how it is currently implemented Anyway, i think it will be better for OP to use QtConcurrent and give QThread another try later.

  13. #13
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: -how to run a function in separate thread

    I agree with you.
    My bad i was helping him step by step so i thought you meant the whole approach doesnt work...

  14. #14
    Join Date
    Dec 2012
    Posts
    45
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: -how to run a function in separate thread

    I am trying on both the version Qt 4 as well as on Qt 5

    Hey can you tell me what i am doing wrong because when ever i try to use connect() signal slot in the call back of push button i get some kind of error why is it so ?


    Added after 12 minutes:


    This is for all those who want to use some thing like this to play with signal and slot and threads also
    Attached Files Attached Files
    Last edited by shivendra46d; 24th October 2013 at 20:20.

  15. #15
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: -how to run a function in separate thread

    Show us what you are doing and what are the errors ?
    Check my previous posts they solve the problem.

    Good Luck.

  16. #16
    Join Date
    Dec 2012
    Posts
    45
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: -how to run a function in separate thread

    Hey thanks a lot buddy
    It solved the problem, but i want to do it more properly so please tell me
    my email id
    shivendra46d@gmail.com
    i would like to keep in touch with you pelople

  17. #17
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: -how to run a function in separate thread

    You can keep in touch on this forum its more helpful than an email over gmail

  18. #18
    Join Date
    Dec 2012
    Posts
    45
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: -how to run a function in separate thread

    does running a function in separate thread which carries out heavy computation and which when in main thread freezes the ui . will help in over coming the problem? is there any other way to remove freezing of ui?
    i went through some tutorial but not of good help

  19. #19
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: -how to run a function in separate thread

    does running a function in separate thread which carries out heavy computation and which when in main thread freezes the ui . will help in over coming the problem?
    Yes, if implemented properly. Keep in mind that you will have to synchronize with main thread if you want to display the results.
    If it is only a single function, I'd use QtConcurrent to run it. Few lines of code and you have your processing done in separate thread.

  20. #20
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: -how to run a function in separate thread

    Quote Originally Posted by shivendra46d
    other way to remove freezing of ui?
    Depends on what is causing the GI freezing in your heavy computation method.
    Quote Originally Posted by shivendra46d
    does running a function in separate thread which carries out heavy computation and which when in main thread freezes the ui . will help in over coming the problem?
    Yes, when you run it in a different thread obviously the main GUI wont freeze.
    Quote Originally Posted by shivendra46d
    i went through some tutorial but not of good help
    Read more tutorials, read Qt documentation and reference. Check this link for example Keeping the GUI Responsive

    Good Luck.

Similar Threads

  1. OpenGL rendering in separate thread, Qt 5.0.1
    By jaf in forum Qt Programming
    Replies: 3
    Last Post: 28th October 2015, 00:03
  2. How to Have a class run in a separate thread.
    By sona1111 in forum Newbie
    Replies: 7
    Last Post: 29th August 2013, 07:44
  3. GUI Updates from separate Thread
    By sa5webber in forum Newbie
    Replies: 5
    Last Post: 16th June 2012, 21:08
  4. Accesing widgets from separate thread
    By msmihai in forum Qt Programming
    Replies: 2
    Last Post: 8th December 2008, 12:48
  5. new QWidget in separate thread
    By magland in forum Qt Programming
    Replies: 15
    Last Post: 7th February 2008, 13:32

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.