嗯,好的,先谢谢zuogenyu了,之前只是怕贴出来太多了,看不清楚,所以就挑了几个觉得可能有问题的地方给贴出来了,完整的程序贴在了下面,目的主要是想在主界面上有两个按钮,点击其中一个打开一个线程可以在主界面绘制一条曲线,点击另一个也是同样的功能,但是他俩要可以同时运行,程序写的比较烂,希望不要见笑啊~~麻烦了
#ifndef TEST_H
#define TEST_H
#include <qdialog.h>
#include <qpainter.h>
#include <qstring.h>
#include <qthread.h>
class QPushButton;
class Draw;
class myEvent;
class ThreadA : public QThread
{
public:
ThreadA(Draw* d) : receiver(d)
{ ; }
void run();
private:
Draw *receiver;
};
class ThreadB : public QThread
{
public:
ThreadB(Draw* d) : receiver(d)
{ ; }
void run();
private:
Draw *receiver;
};
class Draw : public QDialog
{
Q_OBJECT
public:
Draw(QWidget* parent = 0, const char* name = 0);
private:
QPushButton* buttonA;
QPushButton* buttonB;
QPushButton* button1;
QPainter *pa;
QPainter *pb;
ThreadA threadA;
ThreadB threadB;
protected:
void customEvent(myEvent *);
public slots:
void drawGrid();
void startA();
void startB();
protected slots:
void languageChange();
};
#endif //TEST_H
#include "test.h"
#include <qdialog.h>
#include <qthread.h>
#include <qpushbutton.h>
#include <qpainter.h>
#include <qrect.h>
#include <qmessagebox.h>
class myEvent : public QCustomEvent
{
public:
myEvent(int i) : QCustomEvent(12345), id(i) {};
int id;
};
void ThreadA::run()
{
for(int i = 0; i < 14; i++){
myEvent *event = new myEvent(1);
QThread::postEvent(receiver, event);
sleep(1);
}
}
void ThreadB::run()
{
for(int i = 0; i < 14; i++){
myEvent *event = new myEvent(2);
QThread::postEvent(receiver, event);
sleep(1);
}
//QMessageBox::information(this, "Thread is running");
}
Draw :: Draw(QWidget* parent, const char* name) : QDialog(parent, name), threadA(this), threadB(this)
{
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);
pa = new QPainter(this);
pa->setPen(red);
pb = new QPainter(this);
pb->setPen(yellow);
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::customEvent(myEvent *event)
{
if(event->type() == 12345){
//pa->drawLine(25,25,100,100);
switch(event->id){
case 1:
{
static int a[26] = {60, 84, 50, 91, 84, 101, 95, 132, 114, 120, 85, 131, 116, 6, 121, 210, 207, 63, 65, 214, 216, 121, 92, 121, 154, 161};
static int i = 0;
pa->drawLine(a, a[i + 1], a[i + 2], a[i + 3]);
i = i + 2;
break;
}
case 2:
{
static int b[26] = {63, 207, 92, 121, 65, 214, 216, 121, 60, 121, 210, 154, 84, 50, 91, 84, 101, 95, 132, 114, 120, 85, 131, 116, 6, 161};
static int j = 0;
pb->drawLine(b[j], b[j + 1], b[j + 2], b[j + 3]);
j = j + 2;
break;
}
default: break;
}
}
}
void Draw::languageChange()
{
setCaption("It's a test");
buttonA->setText(tr("startA"));
buttonB->setText(tr("startB"));
button1->setText(tr("axis"));
}