2016-05-10 3 views
1

qcustomplotを使用して項目を描画しました。qcustomplot:名前で項目を設定して検索します。

私は2つのアイテムを持っています。 1つは項目テキストであり、もう1つは項目矩形です。

私がしたいことは、テキストを選択すると、アイテムの色が変わります。

私はitemAtを使用して、マウスがアイテムをクリックしたかどうかを確認しました。

しかし、私は、私が選択したどのような項目のテキストを知らない二つの問題

  1. に遭遇しました。

  2. 特定の項目を名前で検索する方法がわかりません。

コード:

//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"; 
} 
+0

あなたのコードの関連部分を追加してください。 –

答えて

0

はまず、あなたのmoveOverイベント内rect色を変更するためには、クラスのデータメンバとして保存することができます。

第2に、QCPItemRectQCPItemTextの両方がQCPAbstractItemから継承されているため、dynamic_castを使用できます。 QCPItemTextにキャストしようとすることができます。キャストが失敗した場合、ポインタはnullになります。またthisポストを見てください。

だから、あなたのコードは次のようになります。

moveOver(QMouseEvent* event) 
{ 
    QPoint pos = event->pos(); 
    QCPAbstractItem *item = ui->customPlot->itemAt(pos, true); 
    textItem = QCPItemText* dynamic_cast<QCPItemText*> (item); 
    if (textItem == 0){ 
     //item is not a QCPItemText 
     **do something** 
    } 
    else{ 
     //item is a QCPItemText - change rect color 
     rect->setBrush(QBrush(QColor(50, 0, 0, 100))); 
    } 
} 
関連する問題