//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "myobject.h"
#include <QPushButton>
#include <QMainWindow>
namespace Ui{
class MainWindow;
}
class MainWindow:public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void onFirstPushed();
void onSecondPushed();
void onSelfPushed();
void onExitPushed();
private:
Ui::MainWindow *ui;
MyObject *my;
QPushButton *firstButton;
QPushButton *secondButton;
QPushButton *threeButton;
QPushButton *selfButton;
QPushButton *exitButton;
};
#endif
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),ui(new Ui::MainWindow)
{
ui->setupUi(this);
my = new MyObject;
firstButton = new QPushButton(tr("first"), 0);
connect(firstButton, SIGNAL(clicked()), my, SLOT(first()), Qt::QueuedConnection);
secondButton = new QPushButton(tr("second"), 0);
connect(secondButton, SIGNAL(clicked()), my, SLOT(second()), Qt::QueuedConnection);
threeButton = new QPushButton(tr("three"), 0);
connect(threeButton, SIGNAL(clicked()), my, SLOT(three()), Qt::QueuedConnection);
selfButton = new QPushButton(tr("self"), 0);
connect(selfButton, SIGNAL(clicked()), this, SLOT(onSelfPushed()));
exitButton = new QPushButton(tr("exit"), 0);
connect(exitButton, SIGNAL(clicked()), this, SLOT(onExitPushed()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(firstButton);
layout->addWidget(secondButton);
layout->addWidget(threeButton);
layout->addWidget(selfButton);
layout->addWidget(exitButton);
QWidget *p = new QWidget;
p->setLayout(layout);
QThread *thread = new QThread;
my->moveToThread(thread);
thread->start();
connect(thread, SIGNAL(started()), my, SLOT(first()));
setCentralWidget(p);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onFirstPushed()
{
my->first();
}
void MainWindow::onSecondPushed()
{
my->second();
}
void MainWindow::onThreePushed()
{
my->three();
}
void MainWindow::onSelfPushed()
{
qDebug() << QThread::currentThreadId();
}
void MainWindow::onExitPushed()
{
close();
}
//myobject.h
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
#include <QDebug>
#include <QThread>
class MyObject:public QObject{
Q_OBJECT
public:
MyObject(){}
~MyObject(){}
public slots:
void first();
void second();
void three();
};
#endif
//myobject.cpp
#include "myobject.h"
void MyObject::first()
{
qDebug() << QThread::currentThreadId();
}
void MyObject::second()
{
qDebug() << QThread::currentThreadId();
}
void MyObject::three()
{
qDebug() << QThread::currentThreadId();
}