#include "mainwindow.h"
#include <QTextEdit>
#include <QDockWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QLayout>
#include <QGridLayout>
#include <QtGui>
#include <QLabel>
#include <QFileDialog>
#include <QAction>
#include <QDebug>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QMessageBox>
#include <QTimer>
void MainWindow::createActions()
{
OpenFileAction = new QAction(QIcon("Open.png"),tr("Open"),this);
OpenFileAction->setShortcut(tr("Ctrl+O"));
OpenFileAction->setStatusTip(tr("Open one file"));
connect(OpenFileAction,&QAction::triggered,[=] ()
{
QString path = QFileDialog::getOpenFileName(
this,"open","../",
"souce(*.cpp *.h);;Text(*.txt);;All(*.*)");
}
);
NewFileAction = new QAction(QIcon("Open.png"),tr("New"),this);
NewFileAction->setShortcut(tr("Ctrl+N"));
NewFileAction->setStatusTip(tr("Build one new file"));
connect(NewFileAction,SIGNAL(triggered()),this,SLOT(showNewFile()));
ExitAction = new QAction(QIcon("Open.png"),tr("Exit"),this);
ExitAction->setShortcut(tr("Ctrl+N"));
ExitAction->setStatusTip(tr("Exit"));
CopyAction = new QAction(QIcon("Open.png"),tr("Copy"),this);
CopyAction->setShortcut(tr("Ctrl+Q"));
CopyAction->setStatusTip(tr("Exit"));
//connect(CopyAction,SIGNAL(triggered()),showWidget->text,SLOT(copy()));
CutAction = new QAction(QIcon("Open.png"),tr("Cut"),this);
CutAction->setShortcut(tr("Crtl+C"));
//connect(CutAction,SIGNAL(triggered()),showWidget->text,SLOT(cut()));
PasteAction = new QAction(QIcon("Open.png"),tr("Paste"),this);
PasteAction->setShortcut(tr("Ctrl+V"));
//connect(PasteAction,SIGNAL(triggered()),showWidget->text,SLOT(paste()));
ZoomInAction = new QAction(QIcon("Open.png"),tr("ZoomIn"),this);
connect(ZoomInAction,SIGNAL(triggered()),this,SLOT(showZoomIn()));
ZoomOutAction = new QAction(QIcon("Open.png"),tr("ZoomOut"),this);
connect(ZoomOutAction,SIGNAL(triggered()),this,SLOT(showZoomOut()));//图标未设置,均采用open.png
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("File"));
fileMenu->addAction(OpenFileAction);
fileMenu->addAction(NewFileAction);
fileMenu->addSeparator();
fileMenu->addAction(ExitAction);
}
void MainWindow::createToolBars()
{
Tool = addToolBar("Tool");
Tool->addAction(CopyAction);
Tool->addAction(CutAction);
Tool->addAction(PasteAction);
Tool->addAction(ZoomInAction);
Tool->addAction(ZoomOutAction);
Tool->setAllowedAreas(Qt::TopToolBarArea);
}
void MainWindow::createStatusBars()//状态栏,最下面一行,显示状态
{
Status = statusBar();
Status->addWidget(new QLabel("2",this));
}
void MainWindow::showNewFile()//新建文件,这里需要修改
{
MainWindow *newFile = new MainWindow;
newFile->show();
}
void MainWindow::showZoomIn()//下面这两个要修改,能够将实时图像放大或者缩小
{
}
void MainWindow::showZoomOut()//
{
}
void MainWindow::bluetooth()//蓝牙模块
{
QDockWidget *dock1 = new QDockWidget(tr("DockWindow1"));
dock1->setFeatures(QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetFloatable); //窗口可移动
dock1->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea,dock1);
dock1->setStyleSheet("QPushButton{color:white;background-color:black}");//按钮样式设置
BlueToothLabel = new QLabel(tr("Port name: "));
ConnectBtn = new QPushButton("Connect");
BreakBtn = new QPushButton(tr("Break"));
Stop1Btn = new QPushButton(tr("Stop"));
BlueToothPortComboBox = new QComboBox;
BaudRateLabel = new QLabel(tr("BaudRate: "));
BaudRateComBox = new QComboBox;
BaudRateComBox->addItem(tr("11520"));
BaudRateComBox->addItem(tr("9600"));
DateRateLabel = new QLabel(tr("DateRate: "));
DateRateComBox = new QComboBox;
DateRateComBox->addItem("5");
DateRateComBox->addItem("6");
DateRateComBox->addItem("7");
DateRateComBox->addItem("8");
DateRateComBox->setCurrentIndex(3);//设置下拉选择默认为第4个
ParityLabel = new QLabel("Parity");
ParityComBox = new QComboBox;
ParityComBox->addItem("On");
ParityComBox->addItem("Off");
ParityComBox->setCurrentIndex(1);
StopBitsLabel = new QLabel("StopBits: ");
StopBitsComBox = new QComboBox;
StopBitsComBox->addItem("1");
StopBitsComBox->addItem("2");
// QTextEdit *StatusOfBT = new QTextEdit;
//进行布局,先横向再纵向
QHBoxLayout *Dock1Layout1 = new QHBoxLayout();
Dock1Layout1->addWidget(BlueToothLabel);
Dock1Layout1->addWidget(BlueToothPortComboBox);
QHBoxLayout *Dock1Layout2 = new QHBoxLayout();
Dock1Layout2->addWidget(BaudRateLabel);
Dock1Layout2->addWidget(BaudRateComBox);
QHBoxLayout *Dock1Layout3 = new QHBoxLayout();
Dock1Layout3->addWidget(DateRateLabel);
Dock1Layout3->addWidget(DateRateComBox);
QHBoxLayout *Dock1Layout4 = new QHBoxLayout();
Dock1Layout4->addWidget(ParityLabel);
Dock1Layout4->addWidget(ParityComBox);
QHBoxLayout *Dock1Layout5 = new QHBoxLayout();
Dock1Layout5->addWidget(StopBitsLabel);
Dock1Layout5->addWidget(StopBitsComBox);
QHBoxLayout *Dock1Layout6 = new QHBoxLayout();
Dock1Layout6->addWidget(ConnectBtn);//点击后就开始寻找设备进行连接
Dock1Layout6->addWidget(BreakBtn);//点击后断开串口连接
// Dock1Layout6->addWidget(Stop1Btn);//暂时未用到
QVBoxLayout *Dock1Layout = new QVBoxLayout();
Dock1Layout->setAlignment(Qt::AlignCenter);
Dock1Layout->addLayout(Dock1Layout1);
Dock1Layout->addLayout(Dock1Layout2);
Dock1Layout->addLayout(Dock1Layout3);
Dock1Layout->addLayout(Dock1Layout4);
Dock1Layout->addLayout(Dock1Layout5);
Dock1Layout->addLayout(Dock1Layout6);
QWidget *Dock1Widget = new QWidget();
Dock1Widget->setLayout(Dock1Layout);
dock1->setWidget(Dock1Widget);
ConnectBtn->setMinimumWidth(70);
BreakBtn->setMaximumWidth(50);
Stop1Btn ->setMaximumWidth(50);
//搜索串口,并添加到选项上供使用者选择
foreach(const QSerialPortInfo &Info,QSerialPortInfo ::availablePorts())
{
QSerialPort CurrentPort;
CurrentPort.setPort(Info);
if(CurrentPort.open(QIODevice::ReadWrite))
{
BlueToothPortComboBox->addItem(CurrentPort.portName());//插入串口的名字
CurrentPort.close(); //先开再关,把串口名称先导入
}
}
connect(ConnectBtn, SIGNAL(clicked()),this,SLOT(on_connectButton_clicked()));
connect(BreakBtn, SIGNAL(clicked()),this,SLOT(on_breakButton_clicked()));
}
void MainWindow::on_clearButton_clicked()//清空发送与接收窗口信息
{
SendInfo->clear();
ReceiveInfo->clear();
}
void MainWindow::on_sendButtton_clicked()//发送数据
{
QByteArray SendBytes = SendInfo->toPlainText().toLatin1();//toPlainText(将文本编辑的文本转换为纯文本)
if(SendBytes.isEmpty())//判断发送数据是否为空
{
StatusOfDock3->append("No message can be sent, Please write something");
}
CurrentPort->write(SendBytes);
}
void MainWindow::Read_Data()//读取接收到的数据
{
QByteArray buf;
buf = CurrentPort->readAll();//Qbytearray类提供一个字节数组,buf这里应该是缓冲数据的功能
if(!buf.isEmpty())
{
QString str = this->ReceiveInfo->toPlainText().toUtf8();
str += tr(buf);//???
ReceiveInfo->clear();
ReceiveInfo->append(str);
}
buf.clear();
}
void MainWindow::on_connectB
评论0