I have used the qcustomplot to draw item.
I have two item. One is item text, another is item rect.
What I want to do is when I select the text, the item rect change the color.
I have used the itemAt
to check whether the mouse has clicked a item.
But I have encountered two problems
I don't know what item text I selected.
I don't know how to find the specific item rect by name.
Code:
//item text
QCPItemText *text= new QCPItemText(ui->customPlot);
ui->customPlot->addItem(text);
text->setSelectable(true);
text->position->setCoords(10, 30);
text->setText("text");
text->setFont(QFont(font().family(), 9));
// item rect
QCPItemRect *rect= new QCPItemRect(ui->customPlot);
ui->customPlot->addItem(rect);
rect->setPen(QPen(QColor(50, 0, 0, 100)));
rect->setSelectedPen(QPen(QColor(0, 255, 0, 100)));
rect->setBrush(QBrush(QColor(50, 0, 0, 100)));
rect->setSelectedBrush(QBrush(QColor(0, 255, 0, 100)));
rect->topLeft->setCoords(0,10);
rect->bottomRight->setCoords(10,0);
connect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(moveOver(QMouseEvent*)));
moveOver(QMouseEvent* event)
{
QPoint pos = event->pos();
QCPAbstractItem *item = ui->customPlot->itemAt(pos, true);
if(item != 0) qDebug() << "moved over";
}