PDA

View Full Version : Runtime Error!



blm
22nd September 2008, 10:37
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?:confused:

thanks for your Help!

blm
22nd September 2008, 10:39
#include <QThread>
#include <QMutex>
class QLabel;
class QPixmap;
class CircleThread : public QThread {
Q_OBJECT

public:
CircleThread(QLabel *label, QPixmap *pixmap, int y);
void stop();

protected:
void run();

private:
QLabel *label;
QPixmap *pixmap;
int y;
volatile bool stopped;
QMutex mutex;

};

blm
22nd September 2008, 10:42
#include "CircleThread.h"
#include <QPainter>
#include <QLabel>
#include <QPixmap>

CircleThread::CircleThread(QLabel *label, QPixmap *pixmap, int y)
{
this->label = label;
this->pixmap = pixmap;
this->y = y;

stopped = false;
}

void CircleThread::run()
{

QPainter painter(pixmap);

while (!stopped){
for(int i = 10; i < 300; i += 10) {
//mutex.lock();
painter.drawEllipse(i, y, 30, 30);
label->setPixmap(*pixmap);
//mutex.unlock();
QThread::msleep(500);
}
}
stopped = false;

}

void CircleThread::stop()
{
stopped = true;
}

blm
22nd September 2008, 10:45
ThreadDialog::ThreadDialog(QWidget *parent)
: QWidget(parent)
{
...
label = new QLabel(this);
QPixmap pixmap(width(), height()-100);
pixmap.fill(Qt::white);
threadA = new CircleThread(label, &pixmap, 50);
threadB = new CircleThread(label, &pixmap, 100);

connect(threadAButton, SIGNAL(clicked()),
this, SLOT(startOrStopThreadA()));
connect(threadBButton, SIGNAL(clicked()),
this, SLOT(startOrStopThreadB()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
...
}




void ThreadDialog::startOrStopThreadA()
{
if (threadA->isRunning()) {
threadA->stop();
threadAButton->setText(tr("Start A"));
} else {
threadA->start();
threadAButton->setText(tr("Stop A"));
}
}

jpn
22nd September 2008, 16:22
You must not touch GUI (widgets nor pixmaps) in worker threads. Take a look at the mandelbrot example.

blm
25th September 2008, 13:26
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,


MainDialog::MainDialog(QWidaget *parent) : QDialog(parent)
{
...
textEdit = new QTextEdit(this);
connect(processthread, SIGNAL(newDataCome(double), this, SLOT(writeNew(double)), Qt::DirectConnection);
...
}

if i write in SLOT like this:


void MainDialog::writeNew(double point)
{
textEdit->append(QString::number(point, `g`, 6);
}


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??

jpn
25th September 2008, 14:56
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.