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

How to update QCustomPlot Data in thread?

我想在 c++ 中的 QCustomPlot 的帮助下显示实时数据。所以我这样做:

在头文件中:

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();

};

并且在源文件中,我使用一个按钮来启动线程并且...像这样:

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();

   }

  }

但是当我启动代码时出现这个错误:

"cannot send events to objects owned by a different thread"

我该如何解决?

将此函数设为静态并尝试 void QtGuiApplication::thread_Displayer_fn()

  static void QtGuiApplication::thread_Displayer_fn()

因为从 class QtGuiApplication 创建的对象已经被主线程拥有,并且您正在尝试使用上述语句从另一个线程(子线程)调用它的方法。

您需要(或者至少这对我来说是这样)创建一个信号,该信号将在线程中的 for 循环的每次迭代中发出。然后,将此信号连接到将进行数据实际更新的插槽

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
    }
}

注意我还在emmit后面加了一个usleep函数。如果你不放它你将无法再次按下按钮并停止,我认为这是因为数据发送的速率。

Here你可以找到整个例子。