QT C++マルチスレッド通信の実装--サンプルコード

3456 ワード

main.cpp
#include "mainwindow.h"
#include 
//#include
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

 //   mythreaad my;
 //   my.start();
  //  QObject::connect(&my,SIGNAL(MySignal(QString)),&w,SLOT(myreceiveslots(QString)));



 //   QObject::connect(&w,SIGNAL(mainSignal(int)),&my,SLOT(mythreadrec(int)));
    return a.exec();
}



mainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include
#include

//#include

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void generateData();

signals:
    void mainSignal(mydataTrans);




private:
    mythreaad *tre;
    Ui::MainWindow *ui;
	mydataTrans *mydata;
	
};



#endif // MAINWINDOW_H



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tre=new mythreaad();
    tre->start();
	
	QObject::connect(this, SIGNAL(mainSignal(mydataTrans)), tre, SLOT(mythreadrec(mydataTrans)));
//    QObject::connect(tre,SIGNAL(MySignal(int )),this,SLOT(MainRecei(int)));
	this->generateData();

   // this->moveToThread(tre);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::generateData()
{
	mydata = new mydataTrans();
	mydata->id = 9;  
	mydata->name = "chenshuyi ";
	
	//mydata->age = 26;
	emit(mainSignal(*mydata));
   
}


#ifndef MYTHREAAD_H
#define MYTHREAAD_H

#include
#include
#include"mydataTrans.h"

class mythreaad:public QThread
{
    Q_OBJECT
public:
    mythreaad();
    void run();
    int handleInt(int);
signals:
   void MySignal(int );
public slots:
   void mythreadrec(mydataTrans);
public:
	mydataTrans mydata2;
};

#endif // MYTHREAAD_H

#include "mythreaad.h"
#include"qstring.h"
#include

mythreaad::mythreaad()
{

}

void mythreaad::run()
{

//   qDebug()<


#pragma once
#include
using namespace std;
class mydataTrans
{
public:
	mydataTrans();
	~mydataTrans();
	int id;
	string name;
};




#include "mydataTrans.h"


mydataTrans::mydataTrans()
{
}


mydataTrans::~mydataTrans()
{
}



, , , , , ( )。