Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot access to QCustomPlot item.
QtWS25 Last Chance

Cannot access to QCustomPlot item.

Scheduled Pinned Locked Moved General and Desktop
qcustomplotsignal & slot
2 Posts 1 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    borasemiz
    wrote on last edited by
    #1

    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?

    B 1 Reply Last reply
    0
    • B borasemiz

      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?

      B Offline
      B Offline
      borasemiz
      wrote on last edited by
      #2

      @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.
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved