我正在创建一个应用程序,它将在UI中显示来自Arduino的传感器读数。我通过串口接收串行数据,并希望将其显示在UI中的LCD编号。但是,我似乎无法通过我的void函数修改LCD上的显示。 这是我的mainwindow.cpp中的部分:
void MainWindow::updateLCD(QString sensor) {
qDebug() << "What's going on?";
qDebug() << sensor;
ui->label_5->setText(sensor);
ui->lcdNumber_TMP->display(sensor);
}
以下是我头文件中的部分:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void updateLCD(QString);
我一直在与此作斗争。我没有收到任何代码错误,它只是没有在UI上显示传感器输出。有什么想法吗?
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mydialog.h"
#include "mainwindow.h"
#include <QTimer>
#include <QDateTime>
#include <QSerialPort>
#include <string>
#include <QDebug>
#include <QSerialPortInfo>
#include <QList>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);
//digital clock
QTimer *timer=new QTimer(this); //for clock
connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
timer->start();
//TEMP DISPLAY
ui->lcdNumber_TMP->display("------");
//serial port
device = new QSerialPort(this);
serialBuffer = "";
line = "";
//Identify available ports
bool device_available = false;
QString device_port;
foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) {
if(serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier()) {
if((serialPortInfo.productIdentifier() == device_product_id) && (serialPortInfo.vendorIdentifier() == device_vendor_id)) {
device_available = true; //device is available on this port
device_port = serialPortInfo.portName();
}
}
}
//Open port, if available
if(device_available) {
qDebug() << "Found the device port...\n";
device -> setPortName(device_port);
device -> open(QSerialPort::ReadOnly);
device -> setBaudRate(QSerialPort::Baud9600);
device -> setDataBits(QSerialPort::Data8);
device -> setFlowControl(QSerialPort::NoFlowControl);
device -> setParity(QSerialPort::NoParity);
device -> setStopBits(QSerialPort::OneStop);
QObject::connect(device, SIGNAL(readyRead()),this,SLOT(readSerial()));
} else {
qDebug() << "Couldn't find the correct port for the device.\n";
}
}
MainWindow::~MainWindow() {
if(device -> isOpen()) {
device -> close(); //close serial port if it is open
}
delete ui;
}
void MainWindow::readSerial() {
serialData = device -> readAll();
serialBuffer += QString::fromStdString(serialData.toStdString());
QStringList bufferSplit = serialBuffer.split(",");
serialBuffer = "";
line=bufferSplit[0];
MainWindow::updateLCD(bufferSplit[0]);
}
void MainWindow::updateLCD(QString sensor) {
qDebug() << "What's going on?";
qDebug() << sensor;
ui->label_5->setText(sensor);
ui->lcdNumber_TMP->display(sensor);
}
void MainWindow::showTime() {
QTime time=QTime::currentTime(); //create time
QString time_text=time.toString("hh : mm : ss"); //format time
ui->DigitalClock->setText(time_text); //make label display time
}
答案 0 :(得分:2)
像(您的MainWindow)的QWidgets只在处理QPaintEvent时才在屏幕上重新绘制自己。从你的描述判断,它听起来像是没有运行事件循环(QEventLoop)。
虚函数本身并不以任何方式限制。问题是Qt事件循环是否自由运行。假设你使用QCoreApplication :: exec()启动它,你应该在处理串口信号后返回它。