Cannot access to QCustomPlot item.

  • Hello, I am new to Qt and working with QCustomPlot plotting tools and I have a problem. I cannot access the items of QCustomPlot instance from other methods in class. I don't get a compilation error, but nothing happens when I clicked the button (the method is the slot of the button). Here is the code:

    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        QCustomPlot* plot = ui->widget;
        QCPItemStraightLine* line = new QCPItemStraightLine(plot);
        line->point1->setCoords(3, 5);
        line->point2->setCoords(3, 6);
        line->setPen(QPen(QColor(0, 128, 0)));
        delete ui;
    void MainWindow::on_pushButton_clicked()
        QCustomPlot* plot = ui->widget;
        if (plot->item() == 0)
            ui->label->setText("No Item");
        QCPItemStraightLine* line = (QCPItemStraightLine*) plot->item(); //This statement doesn't seem to work.
        line->point1->setCoords(4, 5); // doesn't work
        line->point2->setCoords(4, 6); // doesn't work

    When I test the item number from the method, it correctly shows me how items I have in QCustomPlot, but the last two lines doesn't work. Nothing happens. What is the problem here?

  • @borasemiz
    I have solved it. I discovered that I needed to refresh my plot, which is to say I needed to re-plot my plot. Let me:

        //I am just skipping to the click function of my button
        QCPItemStraightLine* line = (QCPItemStraightLine) (ui->widget->item());
        ui->widget->replot(); //This line solved the problem.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.