免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4088 | 回复: 0
打印 上一主题 下一主题

[求助]qt多线程绘制曲线出错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-24 15:05 |只看该作者 |倒序浏览
小弟刚学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"));
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP