QMouseEvent
.h
void mousePressEvent(QMouseEvent *event);
.cpp
bool pressed; //global
void MainWindow::mousePressEvent(QMouseEvent *event){if (event->button()==Qt::RightButton){qDebug() << "right button is pressed";pressed=true;}}
.h
void mousePressEvent(QMouseEvent *event);
.cpp
bool pressed; //global
void MainWindow::mousePressEvent(QMouseEvent *event){if (event->button()==Qt::RightButton){qDebug() << "right button is pressed";pressed=true;}}
quint16 serial_usb_vendor_id;quint16 serial_usb_product_id;QString vendor = "D:/vendor.txt";bool chk_status_comport=true;
ui->setup
timer_A = new QTimer(this);
connect(timer_A,SIGNAL(timeout()),this,SLOT(check_usb_comport()));timer_A->start(100);serial = new QSerialPort(this);connect(serial,SIGNAL(readyRead()),this,SLOT(readData()));ui->bt_connect->click();
void MainWindow::check_usb_comport()
{
// if(serial->isOpen())
// {
// serial->close();
// ui->bt_connect->setText("Connect");
// }
// else
// {
// serial->setPortName("COM21");
// serial->setBaudRate(QSerialPort::Baud115200);
// serial->setDataBits(QSerialPort::Data8);
// serial->setFlowControl(QSerialPort::NoFlowControl);
// serial->setParity(QSerialPort::EvenParity);
// serial->setStopBits(QSerialPort::OneStop);
// if( serial->open(QIODevice::ReadWrite))
// {
// ui->bt_connect->setText("Disconnect");
// }
// else
// {
// // QMessageBox::critical(this,tr("Error"),serial->errorString());
// QMessageBox::information(this,"Wrong com port" ,"Please check comport");
// }
// }
if ( chk_status_comport == true)
{
bool serial_is_available = false;
ui->btn_read_vendor_file->click();
QString serial_uno_port_name;
foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{
if (serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier()) {
if ((serialPortInfo.productIdentifier() == serial_usb_product_id)
&& (serialPortInfo.vendorIdentifier() == serial_usb_vendor_id)) {
serial_is_available = true; // serial uno is available on this port
serial_uno_port_name = serialPortInfo.portName();
}
}
}
if (serial_is_available)
{
//qDebug() << "++++++++++++++++++++++++++++++++++";
serial->setPortName(serial_uno_port_name);
serial->open(QSerialPort::ReadWrite);
serial->setBaudRate(QSerialPort::Baud115200);
serial->setDataBits(QSerialPort::Data8);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setParity(QSerialPort::EvenParity);
ui->label_status_comport->setText("Comport Connected");
}
else {
qDebug() << "------------------------------------------";
ui->label_status_comport->show();
ui->label_status_comport->setText("Unable to Commicate with PLC");
//QMessageBox::information(this, "Serial Port Error", "Unable to Commicate with MCU.");
chk_status_comport = false;
}
}
else {
ui->btn_read_vendor_file->click();
QString serial_uno_port_name;
foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{
// check if the serialport has both a product identifier and a vendor identifier
if (serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier()) {
if ((serialPortInfo.productIdentifier() == serial_usb_product_id)
&& (serialPortInfo.vendorIdentifier() == serial_usb_vendor_id)) { // serial_is_available = true;
serial_uno_port_name = serialPortInfo.portName();
}
}
}
if (serial->isOpen())
{
serial->close();
}
else
{
serial->setPortName(serial_uno_port_name);
serial->setBaudRate(QSerialPort::Baud115200);
serial->setDataBits(QSerialPort::Data8);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setParity(QSerialPort::EvenParity);
if ( serial->open(QIODevice::ReadWrite))
{
//qDebug() << "==========================";
chk_status_comport = true;
ui->label_status_comport->show();
ui->label_status_comport->setText("Comport Connected");
}
else
{
ui->label_status_comport->show();
ui->label_status_comport->setText("Unable to Commicate with PLC");
//QMessageBox::information(this, "Serial Port Error", "Unable to Commicate with MCU.");
}
}
}
}
.pro#-------------------------------------------------
#
# Project created by QtCreator 2015-12-10T22:35:58#
#-------------------------------------------------
QT += core gui serialportgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = SerialPlayqtTEMPLATE = appSOURCES += main.cpp\mainwindow.cpp
HEADERS += mainwindow.hFORMS += mainwindow.ui
*****************************************************************
.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QtSerialPort/QSerialPort>#include <QDebug>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:void on_bt_connect_clicked();void readData();void on_bt_connect_2_clicked();private:
Ui::MainWindow *ui;QSerialPort *serial;};#endif // MAINWINDOW_H
*****************************************************************
ui->setupUi(this);
//---- COMPORT ------------------------------------------------foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {QStringList list;list << info.portName();ui->portnamebox->addItem(list.first(),list);}
*****************************************************************
void MainWindow::on_bt_connect_clicked(){if(serial->isOpen()){
serial->close();ui->bt_connect->setText("Connect");}
else{
serial->setPortName(ui->portnamebox->currentText());serial->setBaudRate(QSerialPort::Baud115200);serial->setDataBits(QSerialPort::Data8);serial->setFlowControl(QSerialPort::NoFlowControl);serial->setParity(QSerialPort::EvenParity);serial->setStopBits(QSerialPort::OneStop);if( serial->open(QIODevice::ReadWrite)){
ui->bt_connect->setText("Disconnect");}
else{
// QMessageBox::critical(this,tr("Error"),serial->errorString());QMessageBox::information(this,"Wrong com port" ,"Please check comport");}
}
}void MainWindow::readData(){responseData.append(serial->readAll());ui->logstext->append(responseData.toHex().toUpper());ReadTruck01(responseData);
qDebug() << "recive = " << responseData.append(serial->readAll()) ;}void MainWindow::on_bt_connect_2_clicked(){qDebug() << "click <----";QByteArray data;data[0] = 0x01;data[1] = 0x03;data[2] = 0x00;data[3] = 0x00;data[4] = 0x00;data[5] = 0x5F;serial->write(data);}
//-------------------- .h -----------------------------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>#include <QTimer>#include <QDebug>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:void updates();private:
Ui::MainWindow *ui;QTimer *timer;};#endif // MAINWINDOW_H
//------------------------ .cpp ----------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
int cnt =0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updates()));
timer->start(300);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updates(){
cnt++;
qDebug() << "Timer = " << cnt;
}
#include <QString>#include <QStringRef>#include <QFile>#include <QStringList>#include <QRegExp>
QString vendor = "/home/pi/hino/hino/vendor.txt";void MainWindow::on_btn_write_vendor_file_clicked()
{
QFile vendorFile(vendor);
if(!vendorFile.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "could not open file vendor for write";
return;
}
QTextStream data(&vendorFile);
QString data1 = ui->lineEdit_vendor_file->text();
QString data2 = "," ;
QString data3 = ui->lineEdit_product_file->text();
QString datas = +"," +data1+data2+data3;
data << datas;
vendorFile.flush();
vendorFile.close();
}
void MainWindow::on_btn_read_vendor_file_clicked()
{
QFile vendorFile(vendor);
if(!vendorFile.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "could not open file vendor for Read";
return;
}
QTextStream data(&vendorFile);
QString datas = data.readAll(); // 1234 , 5678
QRegExp tagExp(",");
QStringList firstList = datas.split(tagExp);
QString requiredPart1 = firstList.value(firstList.length()-2);
QString requiredPart2 = firstList.value(firstList.length()-1);
qDebug() << " requiredPart1 = " << requiredPart1;
qDebug() << " requiredPart2 = " << requiredPart2;
bool ok1,ok2;
serial_uno_vendor_id = requiredPart1.toInt(&ok1,10);
serial_uno_product_id = requiredPart2.toInt(&ok2,10);
ui->lineEdit_vendor_file->setText( requiredPart1);
ui->lineEdit_product_file->setText(requiredPart2);
vendorFile.flush();
vendorFile.close(); }
void rl_write_file::WriteFile(QString data) { qDebug() << "WriteFile"; QFile gpsLog(pathFile); //if(!gpsLog.open(QFile::WriteOnly | QFile::Text)) if (!gpsLog.open(QIODevice::WriteOnly | QIODevice::Append)) { qDebug() << "could not open file vendor for write"; return; } QTextStream save(&gpsLog); save << data; qDebug() << data; gpsLog.flush(); gpsLog.close(); }
Dialog
==============================
void checkconcurrent::on_btn_export_clicked(){QString textData;int rows = model->rowCount();int columns = model->columnCount();for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
textData += model->data(model->index(i,j)).toString();textData += ", " ; // for .csv file format}
textData += "\n"; // (optional: for new line segmentation)}
// [Save to file] (header file <QFile> needed)// .csvQString fileName = QFileDialog::getSaveFileName(this, tr("Save Address Book"), "", tr("Address Book (*.csv);;All Files (*)"));
QFile csvFile(fileName);if(csvFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {QTextStream out(&csvFile);out << textData;csvFile.close();}}