无法从 void 函数内向 UI 显示 - QT



我正在创建一个应用程序,该应用将显示从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
}

qwidgets之类的(您的MainWindow(仅在处理Qpaintevent后才在屏幕上重新粉刷自己。从您的描述来看,这听起来像事件循环(Qeventloop(没有运行。

本身并不是任何限制的空隙函数。问题是QT事件循环是否自由运行。假设您使用qcoreapplication :: exec((启动它,则在处理串行端口信号后应返回它。

最新更新