1
我想創建一個只包含撥號的簡單Qwt小部件。撥號盤上顯示的值(即撥號盤的位置)應根據我從單獨線程收集的一些輸入數據進行更新。Qwt實時撥號
我可以在主窗口類中生成我的錶盤,我可以創建和主窗口類中啓動一個單獨的線程我的數據採集,就像這樣:
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
{
// create the dial
QDial* pDial = new QDial;
// create the data class to capture data from an external source
DataClass* pData = new DataClass;
// start the data class thread
pData->Start();
// I can get the latest value of data at any instant like this:
int x = pData->GetData();
// I need to connect the data value to the dial, so that the
// dial always displays the value of the data capture device.
}
我能插入使不斷調用GetData()來更新刻度盤上顯示的值?
***我沒有爲pDial指針設置互斥鎖等,但這些都是必要的。***我會避免使用信號和插槽的互斥鎖。 – drescherjm
怎麼回事?我不想保護共享變量,而我在另一個線程上寫入它們? – user12066
使用信號和插槽可以消除跨線程共享的變量。在這種情況下,您將使'DataClass :: SetValue(int x)'成爲一個插槽。而更新該值的類將發出一個信號(連接到該SetValue(int))來設置該值。 Qt會檢測到涉及多於1個線程並使用排隊連接發佈信號。 – drescherjm