threaddialog.cpp

来自「c++ GUI Programming with QT4书中的源码」· C++ 代码 · 共 61 行

CPP
61
字号
#include <QtGui>#include "threaddialog.h"ThreadDialog::ThreadDialog(QWidget *parent)    : QDialog(parent){    threadA.setMessage("A");    threadB.setMessage("B");    threadAButton = new QPushButton(tr("Start A"));    threadBButton = new QPushButton(tr("Start B"));    quitButton = new QPushButton(tr("Quit"));    quitButton->setDefault(true);    connect(threadAButton, SIGNAL(clicked()),            this, SLOT(startOrStopThreadA()));    connect(threadBButton, SIGNAL(clicked()),            this, SLOT(startOrStopThreadB()));    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));    QHBoxLayout *mainLayout = new QHBoxLayout;    mainLayout->addWidget(threadAButton);    mainLayout->addWidget(threadBButton);    mainLayout->addWidget(quitButton);    setLayout(mainLayout);    setWindowTitle(tr("Threads"));}void ThreadDialog::startOrStopThreadA(){    if (threadA.isRunning()) {        threadA.stop();        threadAButton->setText(tr("Start A"));    } else {        threadA.start();        threadAButton->setText(tr("Stop A"));    }}void ThreadDialog::startOrStopThreadB(){    if (threadB.isRunning()) {        threadB.stop();        threadBButton->setText(tr("Start B"));    } else {        threadB.start();        threadBButton->setText(tr("Stop B"));    }}void ThreadDialog::closeEvent(QCloseEvent *event){    threadA.stop();    threadB.stop();    threadA.wait();    threadB.wait();    event->accept();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?