简体   繁体   English

如何在线程中更新QCustomPlot数据?

[英]How to update QCustomPlot Data in thread?

I want to display real time data whit help of QCustomPlot in c++ . 我想在c ++中显示QCustomPlot的实时数据。 so I do this: 所以我这样做:

in header file: 在头文件中:

class QtGuiApplication : public QMainWindow
{
Q_OBJECT

public:
QtGuiApplication(QWidget *parent = Q_NULLPTR);


private:
Ui::QtGuiApplicationClass ui;

//plot
QCustomPlot*        plot_;
std::thread thread_Displayer_;
bool thread_run_flag_ = false;
void thread_Displayer_fn();

};

and in source file I use a push Button to start thread and ... like this: 在源文件中,我使用按钮启动线程,并...像这样:

void     QtGuiApplication::btn_Display_clicked()
{

if (thread_run_flag_)
{
    ui.btn_Dispaly->setText("Display");
    thread_run_flag_ = false;
    if (thread_Displayer_.joinable())
    {
        thread_Displayer_.join();
    }
}
else
{
    thread_run_flag_ = false;
    if (thread_Displayer_.joinable())
    {
        thread_Displayer_.join();
    }
    ui.btn_Dispaly->setText("Stop");
    thread_run_flag_ = true;
    thread_Displayer_ = std::thread(&QtGuiApplication::thread_Displayer_fn, 
      this);
}


}

void     QtGuiApplication::thread_Displayer_fn()
{
double y_max_ = 0;
double y_min_ = 0;
while (thread_run_flag_)
{
    QVector<double> x(16384), y(16384); 
    for (int i = 0; i<16384; ++i)
    {
        x[i] = i; 
        y[i] = x[i]; 
        if (y[i] > y_max_)
            y_max_ = y[i];
        if (y[i] < y_min_)
            y_min_ = y[i];
    }

    plot_->yAxis->setRange(y_min_, y_max_);
    plot_->graph(0)->setData(x, y);
    plot_->replot();

   }

  }

but This error occurs when I start the code: 但是当我启动代码时会发生此错误:

"cannot send events to objects owned by a different thread" “无法将事件发送到其他线程拥有的对象”

How can I solve it? 我该如何解决?

You need to (or at least this is how it works for me) create a signal that will be emitted every iteration of the for loop in your thread. 您需要(或者至少这就是它的工作方式)创建一个信号,该信号将在线程中的for循环的每次迭代中发出。 Then, connect this signal to a slot that will do the actual update of the data 然后,将此信号连接到将实际更新数据的插槽

class QtGuiApplication : public QWidget
{
    Q_OBJECT

public:
    explicit QtGuiApplication(QWidget *parent = 0);
    ~QtGuiApplication();
private slots:
    void setPlotData(QVector<double> x,QVector<double> y);
    void pushButtonClicked();
signals:
    void newData(QVector<double> x, QVector<double> y);
private:
    Ui::QtGuiApplication *ui;
    QCustomPlot* plot_;
    std::thread thread_Displayer_;
    bool thread_run_flag_ = false;
    void thread_Displayer_fn();
};

double fRand(double fMin, double fMax){
    double f = (double)rand() / RAND_MAX;
    return fMin + f * (fMax - fMin);
}

QtGuiApplication::QtGuiApplication(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QtGuiApplication)
{
    // Set up UI
    ui->setupUi(this);
    plot_ = ui->qcustomplot;

    // Register data type for signals
    qRegisterMetaType<QVector<double>>("QVector<double>");

    // Prepare graph
    plot_->addGraph();

    // Connect
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(pushButtonClicked()));
    connect(this,SIGNAL(newData(QVector<double>,QVector<double>)),this,SLOT(setPlotData(QVector<double>,QVector<double>)));
}

QtGuiApplication::~QtGuiApplication()
{
    delete ui;
}

void QtGuiApplication::pushButtonClicked()
{
    if (thread_run_flag_)
    {
        ui->pushButton->setText("Display");
        thread_run_flag_ = false;
        if (thread_Displayer_.joinable())
        {
            thread_Displayer_.join();
        }
    }
    else
    {
        thread_run_flag_ = false;
        if (thread_Displayer_.joinable())
        {
            thread_Displayer_.join();
        }
        ui->pushButton->setText("Stop");
        thread_run_flag_ = true;
        thread_Displayer_ = std::thread(&QtGuiApplication::thread_Displayer_fn,
          this);
    }
}

void QtGuiApplication::setPlotData(QVector<double> x, QVector<double> y)
{
    plot_->graph(0)->setData(x, y);
    plot_->rescaleAxes();
    plot_->replot();
}

void QtGuiApplication::thread_Displayer_fn()
{
    while (thread_run_flag_)
    {
        QVector<double> x(ui->spinBox->value()), y(ui->spinBox->value());
        for (int i = 0; i<ui->spinBox->value(); ++i)
        {
            x[i] = i;
            y[i] = fRand(0,10);
        }
        emit newData(x,y);
        usleep(ui->doubleSpinBox->value()*1000);// convert to us
    }
}

Note that I also added a usleep function after the emmit. 请注意,我在发射后还添加了usleep函数。 If you do not put it you will not be able to push the button again and stop, I think it is because of the rate at which the data is sent. 如果您不放置它,您将无法再次按下该按钮并停止,我认为这是因为数据发送的速率很高。

Here you can find the whole example. 在这里您可以找到整个示例。

make this function static and try void QtGuiApplication::thread_Displayer_fn() 使此函数静态并尝试void QtGuiApplication::thread_Displayer_fn()

  static void QtGuiApplication::thread_Displayer_fn()

Because object created from class QtGuiApplication already own by main thread and your are trying to call a method of it from another thread (child thread) with the above statement. 因为从类QtGuiApplication创建的对象已经由主线程拥有,并且您正在尝试使用上述语句从另一个线程(子线程)中调用该对象的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM