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. Problem with QThread..
Qt 6.11 is out! See what's new in the release blog

Problem with QThread..

Scheduled Pinned Locked Moved General and Desktop
14 Posts 8 Posters 9.1k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    endla.ravi
    wrote on last edited by
    #1

    hi..

    I have a widget which is working fyn..

    But in background an infinite loop should be there.When i put an infinite loop,the Widget becomes dark and finally crashes..

    I think Threads can be used to solve this problem..(correct me if im wrong)

    But i am unable to use QThread class ..for example if i have

    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::MainWindow *ui;
    };

    #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);
    }

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

    main.cpp
    @
    #include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    And a MyThread class like this
    @
    class MyThread
    {
    public:void run();

    };

    void MyThread::run()
    {
    }
    @

    Now can anyone explain how to put the widget and while(1) {} loop in two different threads,so that both are running simultaneously...

    Thanking u in advance

    [edit: Code highlighted / Denis Kormalev]

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vcsala
      wrote on last edited by
      #2

      Can you please use '@' tags to make the code more readable?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vcsala
        wrote on last edited by
        #3

        I am not sure you need threads, you can call processEvents from your infinite loop, which makes your GUI items responsive.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

          Please use @ tags for code. I edited your post, but in further posts please use it by yourself.

          I don't see any usage of your MyThread class. And it should be inherited from QThread if you want to use threading.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Smar
            wrote on last edited by
            #5

            Usually (mis)using threads causes gui going unresponsive...

            Qt already have event loop, using own events and/or signal/slot system.

            http://doc.qt.nokia.com/latest/signalsandslots.html
            http://doc.qt.nokia.com/latest/eventsandfilters.html

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              endla.ravi what do you want to do in your infinite loop? what is it needed or? Is it background processing (could be done in a thread) or shall it make event processing (is already done by return a.exec();)?

              Please specify your needs a bit more.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • E Offline
                E Offline
                endla.ravi
                wrote on last edited by
                #7

                @above,

                I am actually coding for a messenger

                and my client should always be in listen mode,it should be listening for messages from other clients.
                But when the client i listening for messages,the client become crashes after some time.

                How could i handle this?

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  You can look at the examples: "here":http://doc.qt.nokia.com/4.7/network-network-chat.html

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • W Offline
                    W Offline
                    Wolf P.
                    wrote on last edited by
                    #9

                    You have to subclass MyThread from QThread then define in its run method what you want to do in the background. Did you read the "basic thread example":http://doc.qt.nokia.com/4.7/qthread.html#details ?

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      rugginoso
                      wrote on last edited by
                      #10

                      Maybe in your case it's better to use the QThread::run() default implementation that runs a local (for thread) event loop.
                      You simply have to create your QWidget and your QThread, than use QObject::moveToThread() method and call the QThread::start() method.

                      References:
                      http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/
                      http://doc.qt.nokia.com/4.7/qthread.html#start
                      http://doc.qt.nokia.com/4.7/qobject.html#moveToThread

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        dangelog
                        wrote on last edited by
                        #11

                        [quote]and my client should always be in listen mode,it should be listening for messages from other clients.[/quote]

                        Please read my article here: http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects . This seems to be the another case where threads are absolutely not needed (and indeed they just make your life a lot harder).

                        Software Engineer
                        KDAB (UK) Ltd., a KDAB Group company

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          rugginoso
                          wrote on last edited by
                          #12

                          [quote author="peppe" date="1292837082"]
                          Please read my article here: http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects . This seems to be the another case where threads are absolutely not needed (and indeed they just make your life a lot harder).[/quote]

                          Really good article :D

                          1 Reply Last reply
                          0
                          • W Offline
                            W Offline
                            Wolf P.
                            wrote on last edited by
                            #13

                            [quote author="Il Rugginoso" date="1292836477"] http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/[/quote] Thanks for the tipp! I didn't know the concepts behind the QThread class...

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              rugginoso
                              wrote on last edited by
                              #14

                              [quote author="Wolf P." date="1292842687"][quote author="Il Rugginoso" date="1292836477"] http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/[/quote] Thanks for the tipp! I didn't know the concepts behind the QThread class...
                              [/quote]

                              You're welcome :D

                              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