• 7459阅读
  • 4回复

【提问】如何实现两个线程不断的向窗口发送数据 ? [复制链接]

上一主题 下一主题
离线wjydlut
 

只看楼主 倒序阅读 楼主  发表于: 2005-11-30
#ifndef THREAD1_H
#define THREAD1_H
#include <qmutex.h>
#include <qapplication.h>
#include <qthread.h>
#include <qdatetime.h>

class Thread1 : public QThread
{
  public:
    //Thread();
    //void setMessage(const QString &message);
    virtual void run();
    void stop();
    QString time( );
private :
    QMutex mutex;
    QTime tim ;
    int str ;
};
#endif

#include <stdio.h>
#include "Thread1.h"
void Thread1::run()
{
}
void Thread1::stop()
{
}

QString Thread1::time()
{
mutex.lock();
  tim = QTime::currentTime();
  str=tim.second();  
  char str1[10]={ } ;
  sprintf(str1, "%d", str);    
  sleep(1);
  qDebug( str1 );
  sleep(1);  
  return str1 ;
  mutex.unlock();
}
[ 此贴被fanyu在2005-11-30 15:21重新编辑 ]
离线wjydlut

只看该作者 1楼 发表于: 2005-11-30
Re:如何实现两个线程不断的向窗口发送数据 ?
thread2 和thread1 基本相同

#ifndef PAINT_H
#define PAINT_H

#include <qwidget.h>

#include <qlabel.h>
#include <qpushbutton.h>
#include "Thread1.h"
#include "Thread2.h"
class paint: public QWidget
{
  Q_OBJECT
  public:
    paint(QWidget *parent=0 , const char *name=0 );
  protected:
    virtual void paintEvent(QPaintEvent *);

  public slots:
    void changeA1( );
    void changeA2( );
    void changeB1( );
  private :
    QLabel *label1 ;
    QLabel *label2 ;
    QLabel *labelA1 ;
    QLabel *labelA2 ;  
    QLabel *labelB1 ;
   
    Thread1 threadA ;
    Thread2 threadB ;
    QPushButton *quitButton ;
    QTime tim ;
    int flag ;

  };

#endif
////////////////////////////////////////////////////////////////////////////////
//////paint.cpp
#include <qapplication.h>
#include <qpainter.h>
#include <qlayout.h>
#include <qtextcodec.h>
#include <qpushbutton.h>
#include <qdatetime.h>
#include "paint.h"

struct paintstruct{
  //QString   name ;
  Qt::PenStyle style ;
}
pstyle={ Qt::SolidLine};

int main(int argc ,char **argv)
{
  QApplication app(argc ,argv ,"paint");
  paint paint ;
  QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
  paint.show();
  app.setMainWidget(&paint);
  return(app.exec());
}

paint::paint(QWidget *parent ,const char *name):QWidget(parent ,name)
{
  setFixedSize(640,480);
  label1 = new QLabel(threadA.time(), this);
  label1->setGeometry(280,30,28,28);
  label1->setAlignment(AlignVCenter | AlignHCenter);
  label2 = new QLabel(threadB.time(), this);
  label2->setGeometry(380,30,28,28);
  label2->setAlignment(AlignVCenter | AlignHCenter);
 
  quitButton = new QPushButton(tr("Quit"), this);
  quitButton->setGeometry( 560, 430, 70, 30 );
  quitButton->setDefault(true);
  QObject::connect(quitButton, SIGNAL(clicked()),this, SLOT(close()));
   
  flag=0;   // 0:关 ,1:开
 
  QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
  QString stringA("A 路");
  QPushButton *buttonA = new QPushButton(stringA,this);
  buttonA->setGeometry( 560, 30, 70, 30 );
  connect(buttonA,SIGNAL(clicked()),qApp,SLOT(quit()));
 
  QString stringA1("A 1");
  QPushButton *buttonA1 = new QPushButton(stringA1,this);
  buttonA1->setGeometry( 570, 80, 50, 30 );
  labelA1 = new QLabel("关", this);
      labelA1->setGeometry(70,265,28,28);
      labelA1->setAlignment(AlignVCenter | AlignHCenter);
  QObject::connect(buttonA1,SIGNAL(clicked()),this,SLOT(changeA1()));      
 
  QString stringA2("A 2");
  QPushButton *buttonA2 = new QPushButton(stringA2,this);
  buttonA2->setGeometry( 570, 130, 50, 30 );
  labelA2 = new QLabel("开", this);
      labelA2->setGeometry(150,265,28,28);
      labelA2->setAlignment(AlignVCenter | AlignHCenter);
  QObject::connect(buttonA2,SIGNAL(clicked()),this,SLOT(changeA2()));      
   
  QString stringB("B 路");
  QPushButton *buttonB = new QPushButton(stringB,this);
  buttonB->setGeometry( 560, 250, 70, 30 );
  connect(buttonB,SIGNAL(clicked()),qApp,SLOT(quit()));
 
  QString stringB1("B 1");
  QPushButton *buttonB1 = new QPushButton(stringB1,this);
  buttonB1->setGeometry( 570, 300, 50, 30 );
  labelB1 = new QLabel("关", this);
      labelB1->setGeometry(330,265,28,28);
      labelB1->setAlignment(AlignVCenter | AlignHCenter);
  QObject::connect(buttonB1,SIGNAL(clicked()),this,SLOT(changeB1()));      
 
}

void paint::changeA1(){................}
void paint::paintEvent(QPaintEvent *)
{....................}

每次只能显示每个线程的一个数据 ,在控制台和我画的窗口中都是这样.那位做过这样的程序
帮帮忙呀, 现谢了!
离线wjydlut

只看该作者 2楼 发表于: 2005-12-02
为什么没人回答呀?没人做过吗?
离线XChinux

只看该作者 3楼 发表于: 2005-12-02
这个问题。。。。。。方法很多,你想用什么方法就用什么方法。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线wjydlut

只看该作者 4楼 发表于: 2005-12-03
老大!如果我知道我还用在这里问吗 ? 可不可以举出一种呀?
快速回复
限100 字节
 
上一个 下一个