QCustomPlot Discussion and Comments

Drawing a rectangleReturn to overview

I apologize in advance for my English, I write through Google translate.
Trying to do the scaling rectangle, there was a problem with the rendering of the rectangle.
Do so:

rect = new QCPItemRect(ui->customPlot);
rect ->setVisible(false);
ui->customPlot->addItem(rect );
rect ->setPen(QPen(Qt::black));
rect ->topLeft->setType(QCPItemPosition::ptAxisRectRatio);
rect ->bottomRight->setType(QCPItemPosition::ptAxisRectRatio);

void MainWindow::mousePress(QMouseEvent *event)
	p1 = event->pos();

void MainWindow::mouseMove(QMouseEvent *event)
	if (rect ->visible()){
		rect ->topLeft->setPixelPoint(p1);
		rect ->bottomRight->setPixelPoint(event->pos());

void MainWindow::mouseRelease(QMouseEvent *event)
	rect ->setVisible(false);

Rectangle is not displayed.
Tell me what I'm doing wrong?


connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress(QMouseEvent*)));
	connect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));	
	connect(ui->customPlot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(mouseRelease(QMouseEvent*)));

А с областью выделения разобрался уже? Чего с позицией то не так было?
А здесь не пойму где у тебя ui-customPlot->replot(); при отрисовке области выделения


Are you able to add this QCPItemRect on QcustomPlot?

I am also trying to add QCPItemRect on QCustomPlot But I am getting error that addItem is not a member of QCustomPlot.

rect = new QCPItemRect(ui->customPlot);
rect ->setVisible(false);
ui->customPlot->addItem(rect ); // Getting error here. I am using 2.0 version

Please let me know if you have done this.


according to the docs it is now

bool QCustomPlot::registerItem ( QCPAbstractItem * item)

In the mousePress() you set rubber.visible(true), while in mouseMove() you check for rect.isVisible(). That is why it is not being drawn

Since it's a very old thread, the info here is outdated: Rubber band selection for either zooming the axis rect, selecting data points, or any custom interaction is now supported by QCustomPlot out-of-the-box. Have a look at the selection mechanism documentation and setSelectionRectMode.

(also registerItem must not be called by an external user, it is (and was) an internal function. QCP Items automatically register themselves with the QCustomPlot when they are created)