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) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QCustomPlot* plot = ui->widget; QCPItemStraightLine* line = new QCPItemStraightLine(plot); line->point1->setCoords(3, 5); line->point2->setCoords(3, 6); line->point1->setCoords(2,5); line->point2->setCoords(2,8); line->setPen(QPen(QColor(0, 128, 0))); plot->addItem(line); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QCustomPlot* plot = ui->widget; if (plot->item() == 0) ui->label->setText("No Item"); else ui->label->setText(QString::number(plot->itemCount())); 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()); line->point1->setCoords(5,6); line->point2->setCoords(5,7); ui->widget->replot(); //This line solved the problem.