Results 1 to 7 of 7

Thread: Runtime Error!

  1. #1
    Join Date
    Sep 2008
    Posts
    38
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Runtime Error!

    ich have written a multi thread program, it`s used to draw Elipses,
    there are three buttons in main Thread Frame, "Start A", "StartB" and "Quit".
    if i press "StartA", it will draw Elipse A in a QLabel, press "StartB" draw Elipse B.

    But by runing, if i press "StartA" or "StartB", a error occurs as follows:

    Microsoft Visual C++ Runtime Library(titel)
    Runtime Error
    ...
    This application has requested the Runtime to terminate it in an unsusal way.
    Please contact the application`s support team for more information.

    my develop environment is Eclipse+ MinGW+QT4.4.0(open edition)

    what is wrong with it?

    thanks for your Help!

  2. #2
    Join Date
    Sep 2008
    Posts
    38
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default CircleThread.h

    Qt Code:
    1. #include <QThread>
    2. #include <QMutex>
    3. class QLabel;
    4. class QPixmap;
    5. class CircleThread : public QThread {
    6. Q_OBJECT
    7.  
    8. public:
    9. CircleThread(QLabel *label, QPixmap *pixmap, int y);
    10. void stop();
    11.  
    12. protected:
    13. void run();
    14.  
    15. private:
    16. QLabel *label;
    17. QPixmap *pixmap;
    18. int y;
    19. volatile bool stopped;
    20. QMutex mutex;
    21.  
    22. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2008
    Posts
    38
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default CircleThread.cpp

    Qt Code:
    1. #include "CircleThread.h"
    2. #include <QPainter>
    3. #include <QLabel>
    4. #include <QPixmap>
    5.  
    6. CircleThread::CircleThread(QLabel *label, QPixmap *pixmap, int y)
    7. {
    8. this->label = label;
    9. this->pixmap = pixmap;
    10. this->y = y;
    11.  
    12. stopped = false;
    13. }
    14.  
    15. void CircleThread::run()
    16. {
    17.  
    18. QPainter painter(pixmap);
    19.  
    20. while (!stopped){
    21. for(int i = 10; i < 300; i += 10) {
    22. //mutex.lock();
    23. painter.drawEllipse(i, y, 30, 30);
    24. label->setPixmap(*pixmap);
    25. //mutex.unlock();
    26. QThread::msleep(500);
    27. }
    28. }
    29. stopped = false;
    30.  
    31. }
    32.  
    33. void CircleThread::stop()
    34. {
    35. stopped = true;
    36. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2008
    Posts
    38
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Runtime Error!

    Qt Code:
    1. ThreadDialog::ThreadDialog(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. ...
    5. label = new QLabel(this);
    6. QPixmap pixmap(width(), height()-100);
    7. pixmap.fill(Qt::white);
    8. threadA = new CircleThread(label, &pixmap, 50);
    9. threadB = new CircleThread(label, &pixmap, 100);
    10.  
    11. connect(threadAButton, SIGNAL(clicked()),
    12. this, SLOT(startOrStopThreadA()));
    13. connect(threadBButton, SIGNAL(clicked()),
    14. this, SLOT(startOrStopThreadB()));
    15. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    16. ...
    17. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ThreadDialog::startOrStopThreadA()
    2. {
    3. if (threadA->isRunning()) {
    4. threadA->stop();
    5. threadAButton->setText(tr("Start A"));
    6. } else {
    7. threadA->start();
    8. threadAButton->setText(tr("Stop A"));
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Runtime Error!

    You must not touch GUI (widgets nor pixmaps) in worker threads. Take a look at the mandelbrot example.
    J-P Nurmi

  6. #6
    Join Date
    Sep 2008
    Posts
    38
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Runtime Error!

    Quote Originally Posted by jpn View Post
    You must not touch GUI (widgets nor pixmaps) in worker threads. Take a look at the mandelbrot example.
    thanks, u are right,

    but i find, there is another problem,

    now i let the work thread just do data process, and send data back to main window

    in main thread i can`t use GUI too, for example,
    Qt Code:
    1. MainDialog::MainDialog(QWidaget *parent) : QDialog(parent)
    2. {
    3. ...
    4. textEdit = new QTextEdit(this);
    5. connect(processthread, SIGNAL(newDataCome(double), this, SLOT(writeNew(double)), Qt::DirectConnection);
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 
    if i write in SLOT like this:
    Qt Code:
    1. void MainDialog::writeNew(double point)
    2. {
    3. textEdit->append(QString::number(point, `g`, 6);
    4. }
    To copy to clipboard, switch view to plain text mode 

    then got the same "Runtime"error.

    I don`t understand, I can not use GUI in the main window, if i have used work thread??

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Runtime Error!

    Remove the Qt::DirectConnection parameter. You don't want the connection to be direct, but you want it to be queued from the worker thread to the main GUI thread. You don't need to specify any connection type at all, the default automatic connection works fine.
    J-P Nurmi

Similar Threads

  1. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  3. Error compiling psql plugin
    By vieraci in forum Installation and Deployment
    Replies: 4
    Last Post: 7th October 2007, 02:49
  4. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 12:19
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

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.