【问题标题】:Qwt Realtime DialQwt 实时拨号
【发布时间】:2015-07-26 07:03:32
【问题描述】:

我想创建一个仅包含表盘的简单 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() 来更新表盘上显示的值?

【问题讨论】:

    标签: multithreading qt plot real-time qwt


    【解决方案1】:

    我想出了一个答案 - 不知道这是否是最好的方法。

    只需将指针传递给 DataClass 的构造函数即可:

    DataClass* pData = new DataClass(pDial);
    

    在 DataClass 类中,包含一个 QDial* 成员和 SetDialValue 方法:

    class DataClass
    {
    public:
        Position(QDial* pDial);
        .
        .
        .
        void SetValue(int x);
    
    private:
    
        QDial* _pDial;
        int _val;
    }
    

    将_pDial设置为构造函数中传递的指针,然后每当收到新数据时,通过SetValue方法更新表盘:

    void DataClass::SetValue(int x)
    {
        _pDial->setValue(x);
    
        return;
    }
    

    我为 pDial 指针省略了互斥锁等,但这些当然是必要的。

    【讨论】:

    • 我已经为 pDial 指针省略了互斥锁等,但这些当然是必要的。我会避免使用信号和互斥锁插槽。
    • 怎么回事?我不想在单独的线程上写入共享变量时保护它们吗?
    • 使用信号和槽可以消除跨线程共享的变量。在这种情况下,您可以将 DataClass::SetValue(int x) 设为一个插槽。并且更新值的类会发出一个信号(连接到这个设置值的SetValue(int))。Qt 会检测到涉及多个线程并使用排队连接发布信号。
    猜你喜欢
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    相关资源
    最近更新 更多