Qt的moveToThread式多线程实例完整代码

//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();
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值