[求助]qt多线程绘制曲线出错
小弟刚学qt多线程编程不久,写了一个多线程的程序,希望能有两个线程分别负责两个绘制曲线的操作,这两个操作由按钮触发,程序如下,但是编译能通过,运行却会报如下错误:
段错误:setPen will be reset by begin()
不知如何修改,还望各位大大能指点一二,感激不尽~
#ifndef TEST_H
#define TEST_H
#include <qdialog.h>
#include <qdatetime.h>
#include <qpainter.h>
#include <qstring.h>
#include <qthread.h>
class QPushButton;
class Draw;
class ThreadA : public QThread
{
public:
ThreadA();
void run();
private:
bool stopped;
};
class ThreadB : public QThread
{
public:
ThreadB();
void run();
private:
bool stopped;
};
class Draw : public QDialog
{
Q_OBJECT
public:
Draw(QWidget* parent = 0, const char* name = 0);
private:
QPushButton* buttonA;
QPushButton* buttonB;
QPushButton* button1;
ThreadA threadA;
ThreadB threadB;
protected:
public slots:
void drawGrid();
void startA();
void startB();
protected slots:
void languageChange();
};
#endif //TEST_H
#include "test.h"
#include <qdialog.h>
#include <qpushbutton.h>
#include <qpainter.h>
#include <qrect.h>
ThreadA::ThreadA()
{
stopped = false;
}
void ThreadA::run() //根据数组里的点绘制线
{
int a[6] = {60, 84, 50, 91, 84, 101};
pa = new QPainter();
pa->setPen(red);
int i = 0;
while(!stopped)
{
pa->drawLine(a, a[i + 1], a[i + 2], a[i + 3]);
sleep(1);
i = i + 2;
if (i == 4)
stopped = true;
}
pa->end();
}
ThreadB::ThreadB()
{
stopped = false;
}
void ThreadB::run()
{
int b[6] = {63, 207, 92, 121, 65, 214};
pb = new QPainter();
pb->setPen(blue);
int i = 0;
while(!stopped)
{
pb->drawLine(b, b[i + 1], b[i + 2], b[i + 3]);
sleep(1);
i = i + 2;
if (i == 4)
stopped = true;
}
pb->end();
}
Draw :: Draw(QWidget* parent, const char* name) : QDialog(parent, name)
{
if (!name)
setName("draw");
QPalette pal;
QColorGroup cg;
cg.setColor( QColorGroup::Background, QColor( 154, 230, 230) );
cg.setColor( QColorGroup::Button, QColor( 192, 192, 192) );
cg.setColor( QColorGroup::ButtonText, black );
pal.setActive( cg );
setPalette( pal );
buttonA = new QPushButton(this, "buttonA");
buttonA->setGeometry(5, 5, 80, 30);
buttonB = new QPushButton(this, "buttonB");
buttonB->setGeometry(5, 40, 80, 30);
button1 = new QPushButton(this, "button1");
button1->setGeometry(90, 5, 80, 30);
connect(buttonA, SIGNAL(clicked()), this, SLOT(startA()));
connect(buttonB, SIGNAL(clicked()), this, SLOT(startB()));
connect(button1, SIGNAL(clicked()), this, SLOT(drawGrid()));
languageChange();
resize(QSize(240, 320));
}
void Draw::drawGrid() //绘制背景网格
{
QPainter* painter = new QPainter(this);
QRect rect(10, 40, 200, 230);
for (int i = 0; i <= 5; i++){
int x = rect.left() + (i * (rect.width() - 1) / 5);
double label = 15 * i;
painter->setPen(black);
painter->drawLine(x, rect.top(), x, rect.bottom());
painter->drawLine(x, rect.bottom(), x, rect.bottom() + 5);
painter->setPen(yellow);
painter->drawText(x - 10, rect.bottom() + 5, 20, 20, AlignHCenter | AlignTop, QString::number(label));
}
painter->setPen(black);
painter->drawRect(rect);
}
void Draw::startA()
{
threadA.start();
threadA.wait();
}
void Draw::startB()
{
threadB.start();
threadB.wait();
}
void Draw::languageChange()
{
setCaption("It's a test");
buttonA->setText(tr("startA"));
buttonB->setText(tr("startB"));
button1->setText(tr("axis"));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论