【问题标题】:Text display on GUIGUI 上的文本显示
【发布时间】:2016-12-15 13:49:32
【问题描述】:

在我的 Qt GUI 应用程序中,有 2 个线程。

非 GUI 线程非常频繁地在串行端口上接收数据。这些数据需要显示在作为主线程的 GUI 上。还需要实现滚动。

我该如何实现呢?应该使用哪些 Qt 类?

【问题讨论】:

  • 您需要添加有关您的代码的更多信息,为什么您的串口使用单独的线程?作为一般规则,更新 GUI 应该只从主线程完成,您可能想要切换到单线程设计或使用跨线程信号来更新 GUI。你可能想看看Qt Terminal Example

标签: qt text display scrollable


【解决方案1】:

您需要从线程发送一个包含 QString 变量的信号,并在包含标签的 Widget 中创建一个槽以接收该数据。

文档:http://doc.qt.io/qt-5.7/signalsandslots.html

这里有一个满足您需求的基本原型:

在您的 customthread.h 中

signals:
    portRead(QString text);

在您的 customthread.cpp 中

void process() //Your process function
{
    QString text = readFromSerialPort(); // Your function that reads the SP

    emit portRead(text)
}

在你的 mainwindow.h 中

slots:
    void setLabelText(QString text);

在你的 mainwindow.cpp 中

Widget::Widget(QWidget *parent)
{
    CustomThread *thread = new CustomThread();
    //Some code

    connect(thread,SIGNAL(portRead(QString)),this,SLOT(setLabelText(QString)));
}

void setLabelText(QString text)
{
    this->label->setText(text);
}

【讨论】:

  • 谢谢@Florent Uguet。由于串行数据会被频繁接收,所有来自那里的数据都需要显示在主窗口上,这也需要“滚动”。如何实现滚动?
  • @Aham 这是另一个问题,您必须单独提出。不过看看doc.qt.io/qt-5.7/qscrollarea.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
相关资源
最近更新 更多