从数据读取器终端获取数据到GUI(QtCreator)

时间:2016-06-13 09:15:51

标签: c++ linux qt user-interface data-distribution-service

我需要一个小GUI来显示下面数据阅读器的数据。假设控制台中的数据读取器显示了10条消息,我需要的只是在GUI中它必须在QtCreator的Textbrowser中显示第一条消息半秒钟,第二条消息显示....

感谢任何帮助。

DDS_DomainParticipant_get_current_time (participant, &ts1);

status = Chat_ChatMessageDataReader_take(
    chatAdmin,
    msgSeq,
    infoSeq,
    DDS_LENGTH_UNLIMITED,
    DDS_ANY_SAMPLE_STATE,
    DDS_ANY_VIEW_STATE,
    DDS_ALIVE_INSTANCE_STATE );

checkStatus(status, "Chat_NamedMessageDataReader_take");

for (i = 0; i < msgSeq->_length; i++) 
{
    DDS_DomainParticipant_get_current_time (participant, &ts2);
    Chat_ChatMessage *msg = &(msgSeq->_buffer[i]);
    printf ("%d: %s t1.secs %d t1.ns %u t2.secs %d t2.ns %u \n", msg->userID, msg->content, ts1.sec, ts1.nanosec,ts2.sec, ts2.nanosec);
    fflush(stdout);
}

输出:       MessageBoard已打开:发送一个用户ID = -1的ChatMessage来关闭它....

    1: Hi there, I will send you 20 more messages. t1.secs 1465476565 t1.ns 676311347 t2.secs 1465476565 t2.ns 676395551 
    1: 1 t1.secs 1465476566 t1.ns 676569175 t2.secs 1465476566 t2.ns 676596400 
    1: 2 t1.secs 1465476567 t1.ns 677616392 t2.secs 1465476567 t2.ns 677648204 
    1: 3 t1.secs 1465476568 t1.ns 678910684 t2.secs 1465476568 t2.ns 678940815 
    1: 4 t1.secs 1465476569 t1.ns 680225024 t2.secs 1465476569 t2.ns 680255376 
    1: 5 t1.secs 1465476570 t1.ns 681507684 t2.secs 1465476570 t2.ns 681534887 
    1: 6 t1.secs 1465476571 t1.ns 682657681 t2.secs 1465476571 t2.ns 682689735 
    1: 7 t1.secs 1465476572 t1.ns 683901346 t2.secs 1465476572 t2.ns 683931532 
    1: 8 t1.secs 1465476573 t1.ns 684895076 t2.secs 1465476573 t2.ns 684926328 
    1: 9 t1.secs 1465476574 t1.ns 686201827 t2.secs 1465476574 t2.ns 686231445 
    1: 10 t1.secs 1465476575 t1.ns 687286401 t2.secs 1465476575 t2.ns 687318820 
    1: 11 t1.secs 1465476576 t1.ns 687569194 t2.secs 1465476576 t2.ns 687602357 
    1: 12 t1.secs 1465476577 t1.ns 688870441 t2.secs 1465476577 t2.ns 688900707 
    1: 13 t1.secs 1465476578 t1.ns 690184430 t2.secs 1465476578 t2.ns 690213984 
    1: 14 t1.secs 1465476579 t1.ns 691483124 t2.secs 1465476579 t2.ns 691517976 
    1: 15 t1.secs 1465476580 t1.ns 692274389 t2.secs 1465476580 t2.ns 692299778 
    1: 16 t1.secs 1465476581 t1.ns 693601908 t2.secs 1465476581 t2.ns 693667232 
    1: 17 t1.secs 1465476582 t1.ns 694371847 t2.secs 1465476582 t2.ns 694395932 
    1: 18 t1.secs 1465476583 t1.ns 695229750 t2.secs 1465476583 t2.ns 695421352 
    1: 19 t1.secs 1465476584 t1.ns 696229812 t2.secs 1465476584 t2.ns 696261942 
    1: 20 t1.secs 1465476585 t1.ns 696390252 t2.secs 1465476585 t2.ns 696417861 

我尝试从文本文件中访问数据,这实际上不再是实时的。我现在想要实时访问数据(即直接从终端到GUI)。

来自文本文件:

void MainWindow::on_pushButton_clicked()
{
    QFile file("/home/akhil/test.txt");
    if(!file.open(QIODevice::ReadOnly))
        QMessageBox::information(0,"info",file.errorString());

    QTextStream in(&file);
    ui->textBrowser->setText(in.readAll());

}

1 个答案:

答案 0 :(得分:0)

粗暴的方式

好的,这是一种非常粗糙的方式来做你想做的事 - 对快速测试很有用:

void MainWindow::on_pushButton_clicked()
{
    QFile file("/home/akhil/test.txt");
    if(!file.open(QIODevice::ReadOnly))
        QMessageBox::information(0,"info",file.errorString());

    QTextStream in(&file);
    while (!in.atEnd())
    {
        ui->textBrowser->setText(in.readLine()); // read one line and display it
        QThread::msleep(500); // Pause for 500ms: #include <QThread>
    }
}

正确的方法

然而,更好的方法是将QTextStream作为成员对象打开,然后您可以使用QTimer:

创建计时器并连接它:

QTimer *m_timer = new QTimer(this);
connect(m_timer &QTimer::timeout(), this, &MainWindow::onTimer());

启动计时器(可能在您的on_pushButton_clicked()功能中):

m_timer.start(500);

在计时器插槽上:

MainWindow::onTimer()
{
    ui->textBrowser->setText(m_in.readLine()); // read one line and (in is now member variable 'm_in'
    if (m_in.atEnd())
    {
        m_timer.stop(); // stop the timer we have reached the end.
        // Close the io stream etc...
    }
}