Results 1 to 8 of 8

Thread: Thread problem

  1. #1
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Thread problem

    Good morning people,

    I'm with a problem threading.

    I want to start multiple threads at program start and I am not getting.

    In mainwindow.ccp have the following.

    MainWindow.cpp
    Qt Code:
    1. * The alarm;
    2. a.run ();
    To copy to clipboard, switch view to plain text mode 


    Class Alarm
    Qt Code:
    1. class Alarm : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. Alarm(QObject* = 0);
    6. void iniciarValoresAlarme();
    7. void run();
    8.  
    9. signals:
    10. void itsTime();
    11.  
    12. };
    13. #endif
    To copy to clipboard, switch view to plain text mode 

    Alarm.cpp

    Qt Code:
    1. Alarm::Alarm(QObject* parent) :
    2. QThread(parent)
    3. {}
    4.  
    5. void Alarm::run(){
    6. exec();
    7. }
    8. void Alarm::iniciarValoresAlarme()
    9. {
    10. QDateTime today = QDateTime::currentDateTime();
    11. year = today.date().year();
    12. month = today.date().month();
    13. day = today.date().day();
    14. // hours = 0;
    15. // minutes = 0;
    16. seconds = 0;
    17. timeToAlarm = 0;
    18.  
    19.  
    20. timer = new QTimer(this);
    21.  
    22. connect(timer, SIGNAL(timeout()), this, SLOT(finalizarAlarm()));
    23. connect(this,SIGNAL(itsTime()),this,SLOT(launchAlarm()));
    24. iniciarAlarme();
    25. start();
    26. qDebug()<<"Depois do start antes do wait";
    27. wait();
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

    this code don´t work.

    What the problem?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Thread problem

    1. Show us full method in MainWindow which creates this threads.
    2. What is "this code don't work" ?

  3. #3
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Thread problem

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. Alarm a;
    7. a.run();
    8. }
    To copy to clipboard, switch view to plain text mode 

    The main window is not shown.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Thread problem

    Lines 6 and 7 should be :
    Qt Code:
    1. Alarm *a = new Alarm(this);
    2. a->run();
    To copy to clipboard, switch view to plain text mode 
    Basics of C/C++

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: Thread problem

    "The main window is not shown."

    That is what show() is for.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    Default Re: Thread problem

    QThread::run() does not start a new thread. QThread::start() does.
    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.


  7. #7
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Thread problem

    Thanks my friends. Already works.

    It took just put
    Alarm *a = new Alarm(this);
    a->run();

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

    Default Re: Thread problem

    Still, you have no extra thread here.
    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.


Similar Threads

  1. Qt thread problem
    By sinaru in forum Qt Programming
    Replies: 9
    Last Post: 18th August 2010, 08:46
  2. Gui and non-Gui thread problem
    By AviMittal in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2009, 14:03
  3. thread problem
    By talk2amulya in forum Qt Programming
    Replies: 21
    Last Post: 18th February 2009, 13:05
  4. GUI and non-GUI thread problem
    By dimaz in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 22:25
  5. Thread Problem
    By qball2k5 in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2006, 18:31

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.