2014-02-20 39 views
0

對不起,基本問題。我試圖在QPlainTextWidget中顯示json。我有api函數,它具有控制檯輸出幷包含所有需要的數據。看起來像這樣:如何顯示api函數的輸出?

int iperf_run_server(struct iperf_test *test) 
{ 
int result, s, streams_accepted; 
fd_set read_set, write_set; 
struct iperf_stream *sp; 
struct timeval now; 
struct timeval* timeout; 
...... 
if (test->json_output) 
    if (iperf_json_start(test) < 0) 
     return -1; 

if (test->json_output) { 
    cJSON_AddItemToObject(test->json_start, "version", cJSON_CreateString(version)); 
    cJSON_AddItemToObject(test->json_start, "system_info",  cJSON_CreateString(get_system_info())); 
} else if (test->verbose) { 
    iprintf(test, "%s\n", version); 
    iprintf(test, "%s", ""); 
    fflush(stdout); 
    printf("%s\n", get_system_info()); 
} 
..... 
cleanup_server(test); 

if (test->json_output) { 
    if (iperf_json_finish(test) < 0) 
     return -1; 
} 

.... 
return 0; 
} 

現在,我有我的GUI第一線,及第二線程,包含在信號運行該功能類。所有的東西都能正常工作,但我並不完全理解,我怎樣才能「停止」iperf_run_server來「讀取/緩衝」輸出,而不需要對api進行任何修改。

回答

0

最簡單的做法是收集字符串中的每條消息,並從第二個線程中運行的對象發出信號。您可以將該信號連接到GUI線程中某個對象的插槽。每當事件循環完成處理其他事件時,都會調用零超時定時器 - 這是利用「連續」運行事件的有用機制。

例如:

screenshot

#include <QApplication> 
#include <QPlainTextEdit> 
#include <QThread> 
#include <QBasicTimer> 
#include <QTextStream> 

//! A thread that's always safe to destruct. 
class Thread : public QThread { 
private: 
    // This is a final class. 
    using QThread::run; 
public: 
    Thread(QObject * parent = 0) : QThread(parent) {} 
    ~Thread() { 
     quit(); 
     wait(); 
    } 
}; 

class IperfTester : public QObject { 
    Q_OBJECT 
    struct Test { int n; Test(int n_) : n(n_) {} }; 
    QList<Test> m_tests; 
    QBasicTimer m_timer; 
public: 
    IperfTester(QObject * parent = 0) : QObject(parent) { 
     for (int i = 0; i < 50; ++i) m_tests << Test(i+1); 
    } 
    //! Run the tests. This function is thread-safe. 
    Q_SLOT void runTests() { 
     QMetaObject::invokeMethod(this, "runTestsImpl"); 
    } 
    Q_SIGNAL void message(const QString &); 
private: 
    Q_INVOKABLE void runTestsImpl() { 
     m_timer.start(0, this); 
    } 
    void timerEvent(QTimerEvent * ev) { 
     if (ev->timerId() != m_timer.timerId()) return; 
     if (m_tests.isEmpty()) { 
     m_timer.stop(); 
     return; 
     } 
     runTest(m_tests.first()); 
     m_tests.removeFirst(); 
    } 
    void runTest(Test & test) { 
     // do the work 
     QString msg; 
     QTextStream s(&msg); 
     s << "Version:" << "3.11" << "\n"; 
     s << "Number:" << test.n << "\n"; 
     emit message(msg); 
    } 
}; 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    QPlainTextEdit log; 
    // This order is important: the thread must be defined after the object 
    // to be moved into the thread. 
    IperfTester tester; 
    Thread thread; 
    tester.moveToThread(&thread); 
    thread.start(); 
    log.connect(&tester, SIGNAL(message(QString)), SLOT(appendPlainText(QString))); 
    log.show(); 
    tester.runTests(); 
    return a.exec(); 
    // Here, the thread is stopped and destructed first, following by a now threadless 
    // tester. It would be an error if the tester object was destructed while its 
    // thread existed (even if it was stopped!). 
} 

#include "main.moc" 
+0

感謝您的幫助,我已經試過你的算法。但是在textwidget中仍然沒有未分解的json。 – MikSer

+0

@MikSer首先,您需要驗證未更改的代碼適合您。然後逐步介紹這些更改,並在每次小改動後驗證事情是否正常。你確定你的未解析的JSON存在並且不是空字符串嗎? –

+0

我發現我有一個錯誤,一個字符串是空的,當我試圖把它放在小部件中。謝謝。 – MikSer