定时发送 和 发送新行
This commit is contained in:
+54
-10
@@ -36,7 +36,7 @@ MainWidget::MainWidget(QWidget *parent)
|
|||||||
RecvArea->setPalette(pal);
|
RecvArea->setPalette(pal);
|
||||||
SendArea = new QTextEdit();
|
SendArea = new QTextEdit();
|
||||||
RecvArea->setFont(QFont(tr("Microsoft YaHei UI"), 10));
|
RecvArea->setFont(QFont(tr("Microsoft YaHei UI"), 10));
|
||||||
SendArea->setFont(QFont(tr("Microsoft YaHei UI Light"), 13));
|
SendArea->setFont(QFont(tr("Microsoft YaHei UI Light"), 12));
|
||||||
SendArea->installEventFilter(this);
|
SendArea->installEventFilter(this);
|
||||||
|
|
||||||
//按钮
|
//按钮
|
||||||
@@ -50,28 +50,56 @@ MainWidget::MainWidget(QWidget *parent)
|
|||||||
connect(SendButton, QPushButton::clicked, this, SendContent);
|
connect(SendButton, QPushButton::clicked, this, SendContent);
|
||||||
connect(ClearButton, QPushButton::clicked, this, ClearRecv);
|
connect(ClearButton, QPushButton::clicked, this, ClearRecv);
|
||||||
|
|
||||||
|
//复选框
|
||||||
|
NewLineBox = new QCheckBox(tr("发送新行"));
|
||||||
|
TimerBox = new QCheckBox(tr("定时发送"));
|
||||||
|
|
||||||
|
connect(NewLineBox, QCheckBox::stateChanged, this, detNewLine);
|
||||||
|
connect(TimerBox, QCheckBox::stateChanged, this, ControlSendTimer);
|
||||||
|
|
||||||
|
//spinbox
|
||||||
|
TimerSpin = new QSpinBox();
|
||||||
|
TimerSpin->setSuffix(tr(" ms"));
|
||||||
|
TimerSpin->setMaximum(1000000);
|
||||||
|
TimerSpin->setValue(1000);
|
||||||
|
|
||||||
//初始化布局
|
//初始化布局
|
||||||
|
vlayout = new QVBoxLayout();
|
||||||
|
vlayout->addWidget(RecvArea, 7);
|
||||||
|
vlayout->addWidget(SendArea, 2);
|
||||||
|
hlayout = new QHBoxLayout();
|
||||||
|
hlayout->addWidget(NewLineBox, Qt::AlignRight);
|
||||||
|
hlayout->addWidget(TimerBox, Qt::AlignRight);
|
||||||
|
hlayout->addWidget(TimerSpin);
|
||||||
|
hlayout->addWidget(SendButton);
|
||||||
layout = new QGridLayout(this);
|
layout = new QGridLayout(this);
|
||||||
|
layout->addWidget(COMBox, 0, 0, 1, 2);
|
||||||
layout->addWidget(BaudrateLabel, 1, 0);
|
layout->addWidget(BaudrateLabel, 1, 0);
|
||||||
layout->addWidget(StopbitsLabel, 2, 0);
|
layout->addWidget(StopbitsLabel, 2, 0);
|
||||||
layout->addWidget(DatabitsLabel, 3, 0);
|
layout->addWidget(DatabitsLabel, 3, 0);
|
||||||
layout->addWidget(ParityLabel, 4, 0);
|
layout->addWidget(ParityLabel, 4, 0);
|
||||||
layout->addWidget(COMBox, 0, 0, 1, 2);
|
layout->addWidget(BaudrateBox, 1, 1);
|
||||||
layout->addWidget(BaudrateBox, 1,1);
|
|
||||||
layout->addWidget(StopbitsBox, 2, 1);
|
layout->addWidget(StopbitsBox, 2, 1);
|
||||||
layout->addWidget(DatabitsBox, 3, 1);
|
layout->addWidget(DatabitsBox, 3, 1);
|
||||||
layout->addWidget(ParityBox, 4, 1);
|
layout->addWidget(ParityBox, 4, 1);
|
||||||
layout->addWidget(OpenButton, 5, 1);
|
layout->addWidget(OpenButton, 5, 1);
|
||||||
layout->addWidget(SendButton, 5, 2, Qt::AlignRight);
|
layout->addLayout(hlayout, 5, 2);
|
||||||
layout->addWidget(RecvArea, 0, 2, 3, 1);
|
// layout->addWidget(SendButton, 5, 2, Qt::AlignRight);
|
||||||
layout->addWidget(SendArea, 3, 2, 2, 1);
|
layout->addLayout(vlayout, 0, 2, 5, 1);
|
||||||
layout->addWidget(ClearButton, 0, 3);
|
layout->addWidget(ClearButton, 0, 3, Qt::AlignLeft);
|
||||||
|
layout->setColumnStretch(0, 1);
|
||||||
|
layout->setColumnStretch(1, 1);
|
||||||
|
layout->setColumnStretch(2, 4);
|
||||||
|
layout->setColumnStretch(3, 1);
|
||||||
|
|
||||||
setLayout(layout);
|
setLayout(layout);
|
||||||
|
|
||||||
//计时器 初始化
|
//计时器 初始化
|
||||||
CheckTimer = new QTimer(this);
|
CheckTimer = new QTimer(this);
|
||||||
connect(CheckTimer, QTimer::timeout, this, CheckSerials);
|
|
||||||
CheckTimer->start(1000);
|
CheckTimer->start(1000);
|
||||||
|
SendTimer = new QTimer(this);
|
||||||
|
connect(CheckTimer, QTimer::timeout, this, CheckSerials);
|
||||||
|
connect(SendTimer, QTimer::timeout, SendButton, QPushButton::click);
|
||||||
|
|
||||||
//初始化串口列表
|
//初始化串口列表
|
||||||
CheckSerials();
|
CheckSerials();
|
||||||
@@ -146,7 +174,6 @@ void MainWidget::serialOpened()
|
|||||||
|
|
||||||
void MainWidget::serialNotOpened()
|
void MainWidget::serialNotOpened()
|
||||||
{
|
{
|
||||||
qDebug()<<"打开失败";
|
|
||||||
emit sendStatus(tr("串口打开失败"));
|
emit sendStatus(tr("串口打开失败"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,7 +192,6 @@ void MainWidget::getRecv(QByteArray recv)
|
|||||||
RecvArea->moveCursor(QTextCursor::End);
|
RecvArea->moveCursor(QTextCursor::End);
|
||||||
RecvArea->textCursor().insertText(recv);
|
RecvArea->textCursor().insertText(recv);
|
||||||
RecvArea->selectionChanged();
|
RecvArea->selectionChanged();
|
||||||
// RecvArea->moveCursor(QTextCursor::End);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWidget::OpenSerial()
|
void MainWidget::OpenSerial()
|
||||||
@@ -189,9 +215,27 @@ void MainWidget::ClearRecv()
|
|||||||
RecvArea->clear();
|
RecvArea->clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWidget::detNewLine(int state)
|
||||||
|
{
|
||||||
|
if(state==2)
|
||||||
|
isSendNewLine = true;
|
||||||
|
else if(state == 0)
|
||||||
|
isSendNewLine = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWidget::ControlSendTimer(int state)
|
||||||
|
{
|
||||||
|
if(state == 0)
|
||||||
|
SendTimer->stop();
|
||||||
|
else if(state == 2)
|
||||||
|
SendTimer->start(TimerSpin->value());
|
||||||
|
}
|
||||||
|
|
||||||
void MainWidget::SendContent()
|
void MainWidget::SendContent()
|
||||||
{
|
{
|
||||||
QString content = SendArea->toPlainText();
|
QString content = SendArea->toPlainText();
|
||||||
|
if(isSendNewLine)
|
||||||
|
content += "\r\n";
|
||||||
emit sendData(content);
|
emit sendData(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
-1
@@ -4,12 +4,16 @@
|
|||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
#include <QComboBox>
|
#include <QComboBox>
|
||||||
#include <QGridLayout>
|
#include <QGridLayout>
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
#include <QHBoxLayout>
|
||||||
#include <QTextEdit>
|
#include <QTextEdit>
|
||||||
#include <QPlainTextEdit>
|
#include <QPlainTextEdit>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
#include <QSerialPortInfo>
|
#include <QSerialPortInfo>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
#include <QCheckBox>
|
||||||
|
#include <QSpinBox>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include "serialcontroller.h"
|
#include "serialcontroller.h"
|
||||||
|
|
||||||
@@ -45,18 +49,26 @@ public slots:
|
|||||||
void OpenSerial();
|
void OpenSerial();
|
||||||
void CloseSerial();
|
void CloseSerial();
|
||||||
void ClearRecv();
|
void ClearRecv();
|
||||||
|
void detNewLine(int state);
|
||||||
|
void ControlSendTimer(int state);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QComboBox *COMBox, *BaudrateBox, *StopbitsBox, *DatabitsBox, *ParityBox;
|
QComboBox *COMBox, *BaudrateBox, *StopbitsBox, *DatabitsBox, *ParityBox;
|
||||||
QStringList COMList, BaudrateList, StopbitsList, DatabitsList, ParityList;
|
QStringList COMList, BaudrateList, StopbitsList, DatabitsList, ParityList;
|
||||||
QLabel *BaudrateLabel, *StopbitsLabel, *DatabitsLabel, *ParityLabel;
|
QLabel *BaudrateLabel, *StopbitsLabel, *DatabitsLabel, *ParityLabel;
|
||||||
QGridLayout *layout;
|
QGridLayout *layout;
|
||||||
|
QVBoxLayout *vlayout;
|
||||||
|
QHBoxLayout *hlayout;
|
||||||
QTextEdit *RecvArea, *SendArea;
|
QTextEdit *RecvArea, *SendArea;
|
||||||
QPushButton *OpenButton, *SendButton, *ClearButton;
|
QPushButton *OpenButton, *SendButton, *ClearButton;
|
||||||
QTimer *CheckTimer;
|
QCheckBox *NewLineBox, *TimerBox;
|
||||||
|
QSpinBox *TimerSpin;
|
||||||
|
QTimer *CheckTimer, *SendTimer;
|
||||||
SerialController *serialController;
|
SerialController *serialController;
|
||||||
QThread SerialThr;
|
QThread SerialThr;
|
||||||
|
|
||||||
|
bool isSendNewLine = false;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MAINWIDGET_H
|
#endif // MAINWIDGET_H
|
||||||
|
|||||||
+1
-1
@@ -8,7 +8,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
|
|||||||
setCentralWidget(widget);
|
setCentralWidget(widget);
|
||||||
statusbar->showMessage(tr("准备就绪"));
|
statusbar->showMessage(tr("准备就绪"));
|
||||||
connect(widget, MainWidget::sendStatus, this, setNewMsg);
|
connect(widget, MainWidget::sendStatus, this, setNewMsg);
|
||||||
resize(640, 500);
|
resize(900, 600);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::setNewMsg(QString msg)
|
void MainWindow::setNewMsg(QString msg)
|
||||||
|
|||||||
Reference in New Issue
Block a user