Public Types
enum |
Priority { IdlePriority, LowestPriority, LowPriority, NormalPriority, ..., InheritPriority } |
Public Functions
|
QThread(QObject * parent = 0) |
|
~QThread() |
QAbstractEventDispatcher * |
eventDispatcher() const |
void |
exit(int returnCode = 0) |
bool |
isFinished() const |
bool |
isRunning() const |
Priority |
priority() const |
void |
setEventDispatcher(QAbstractEventDispatcher * eventDispatcher) |
void |
setPriority(Priority priority) |
void |
setStackSize(uint stackSize) |
uint |
stackSize() const |
bool |
wait(unsigned long time = ULONG_MAX) |
Reimplemented Public Functions
virtual bool |
event(QEvent * event) |
§ 31 public functions inherited from QObject
Public Slots
void |
quit() |
void |
start(Priority priority = InheritPriority) |
void |
terminate() |
§ 1 public slot inherited from QObject
Signals
void |
finished() |
void |
started() |
§ 2 signals inherited from QObject
Static Public Members
QThread * |
currentThread() |
Qt::HANDLE |
currentThreadId() |
int |
idealThreadCount() |
void |
msleep(unsigned long msecs) |
void |
sleep(unsigned long secs) |
void |
usleep(unsigned long usecs) |
void |
yieldCurrentThread() |
§ 10 static public members inherited from QObject
Protected Functions
int |
exec() |
virtual void |
run() |
§ 9 protected functions inherited from QObject
Static Protected Members
void |
setTerminationEnabled(bool enabled = true) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class
MyThread :
public
QThread
{
public
:
MyThread()
{
moveToThread(
this
);
}
void
run();
signals:
void
progress(
int
);
void
dataReady(QByteArray);
public
slots:
void
doWork();
void
timeoutHandler();
};
|
--------------------------------------------------------------------------
#-------------------------------------------------
#
# Project created by QtCreator 2014-07-18T15:41:22
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 4) {
QT += widgets serialport
} else {
include($$QTSERIALPORT_PROJECT_ROOT/src/serialport/qt4support/serialport.prf)
}
TARGET = Serial
TEMPLATE = app
SOURCES += main.cpp\
uiwindow.cpp \
serial.cpp
HEADERS += uiwindow.h \
serial.h
FORMS += uiwindow.ui
--------------------------------------------------------------------------
serial.h 文件内容如下:
--------------------------------------------------------------------------
#ifndef SERIAL_H
#define SERIAL_H
#include
#include
class Serial : public QObject
{
Q_OBJECT
public:
explicit Serial(QObject *parent = 0);
~Serial(void);
QSerialPort *port;
signals:
public slots:
void readData(void);
void threadStarted(void);
void threadFinished(void);
};
#endif // SERIAL_H
--------------------------------------------------------------------------
serial.cpp 文件内容如下:
--------------------------------------------------------------------------
#include "serial.h"
#include
#include
#include
Serial::Serial(QObject *parent) :
QObject(parent)
{
port = new QSerialPort();
port->setPortName("COM1");
if(!port->open(QSerialPort::ReadWrite))
{
QMessageBox WrrMsg;
WrrMsg.setInformativeText("无法打开该串口");
WrrMsg.show();
WrrMsg.exec();
}
port->setBaudRate(QSerialPort::Baud19200,QSerialPort::AllDirections); // 19200,N,8,1
port->setDataBits(QSerialPort::Data8);
port->setStopBits(QSerialPort::OneStop);
port->setParity(QSerialPort::NoParity);
port->setFlowControl(QSerialPort::NoFlowControl);
connect(port, SIGNAL(readyRead()), this, SLOT(readData()), Qt::DirectConnection); // 注意,真正执行时 port 与 Serial 在同一个线程中,因此使用 Qt::DirectConnection。
}
Serial::~Serial(void)
{
}
void Serial::readData(void)
{
qDebug()<< "Reading Data...ID is:" << QThread::currentThreadId();
port->clear(QSerialPort::AllDirections);
}
void Serial::threadStarted(void)
{
qDebug()<< "Thread has started...ID is:" << QThread::currentThreadId();
}
void Serial::threadFinished(void)
{
qDebug()<< "Closing COM port...ID is:" << QThread::currentThreadId();
if(port->isOpen())
{
port->close(); // 关闭串口。
}
}
--------------------------------------------------------------------------
uiwindow.h 文件内容如下:
--------------------------------------------------------------------------
#ifndef UIWINDOW_H
#define UIWINDOW_H
#include
#include
#include "serial.h"
namespace Ui {
class UIWindow;
}
class UIWindow : public QMainWindow
{
Q_OBJECT
public:
explicit UIWindow(QWidget *parent = 0);
~UIWindow();
private:
Ui::UIWindow *ui;
QThread serialThread;
Serial *serial;
};
#endif // UIWINDOW_H
--------------------------------------------------------------------------
uiwindow.cpp 文件内容如下:
--------------------------------------------------------------------------
#include "uiwindow.h"
#include "ui_uiwindow.h"
#include
UIWindow::UIWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::UIWindow)
{
ui->setupUi(this);
qDebug()<< "UI thread ID is:" << QThread::currentThreadId();
serial = new Serial();
connect(&serialThread, SIGNAL(started()), serial, SLOT(threadStarted()), Qt::QueuedConnection); // 注意,serialThread 与 serial 并不在同一个线程中,因此使用 Qt::QueuedConnection。
connect(&serialThread, SIGNAL(finished()), serial, SLOT(threadFinished()), Qt::DirectConnection); // serialThread 的 finished() 信号是在新线程中执行的,因此此处要使用 Qt::DirectConnection。
serialThread.start(QThread::HighestPriority); // 开启线程,串口接收线程的优先级较高。
serial->moveToThread(&serialThread); // 将串口接受对象移动到新线程中。
serial->port->moveToThread(&serialThread); // 用于接收的 port 一并移入新线程中。
}
UIWindow::~UIWindow()
{
if(serialThread.isRunning())
{
serialThread.exit(); // 结束该线程。
serialThread.wait();
/*while(!serialThread.isFinished())
{
;
}*/
}
delete ui;
}
--------------------------------------------------------------------------