2016-11-07 46 views
0

我正在將QGraphicsPixmapItems添加到我的場景中,並且我可以看到,當我選擇該項目時,它將獲得白色虛線選擇矩形,但我正在努力從該選擇中獲取任何數據。這是我將它添加到場景的方式。確定選擇了哪個QPixmapItem

void MainWindow::drawImage(curTarget *newTarget){ 
    QGraphicsPixmapItem *tgt = new QGraphicsPixmapItem;//new pixmap 
    tgt = scene->addPixmap(newTarget->myIcon);//assign pixmap image 
    tgt->setFlag(QGraphicsItem::ItemIsSelectable, true); 
    scene->addItem(tgt); 

}

每個PixmapItem我添加到場景中有與之關聯結構數據,我需要能夠在我的QGraphicsScene內對QGraphicsPixmapItem選擇檢索數據。如果在選擇pixmapitem時顯示選擇矩形,是否有一種簡單的方法可以根據這個事實向我返回信息?一個指向可能選擇什麼的指針?

我確實實施了一個mousePressEvent方法,但我正在努力獲取與此相關的任何內容。

void MainWindow::mousePressEvent(QMouseEvent *event){ 
qDebug() << "Clicked" << endl; 
} 

當我運行應用程序時,我看到到處點擊的在我的場景,除了當我點擊我的pixmapitems。

我已經嘗試了每個版本的mousePressEvents可用和那些實際上做了什麼,只做一些事情,只要我不按我的pixmapitems。

+1

QGraphicsScene :: selectedItems() ? – jpnurmi

+0

'mousePressEvent(QGraphicsSceneEvent * event){ qDebug()<< scene-> selectedItems(); }' 回報而是覆蓋'mousePressEvent'創建檢查當前選擇的功能,並將其連接到['QGraphicsScene :: selectionChanged']沒什麼 – bauervision

+1

(http://doc.qt.io/qt-5/ qgraphicsscene.html#selectionChanged)信號。 –

回答

0

感謝我在評論中收到的幫助,我將發佈最終爲我工作的內容。

 void MainWindow::drawImage(curTarget *newTarget) 
    { 
     QGraphicsPixmapItem *tgt = new QGraphicsPixmapItem 
     tgt = scene->addPixmap(newTarget->myIcon); 
     tgt->setFlag(QGraphicsItem::ItemIsSelectable, true); 
     scene->addItem(tgt); 
    } 

隨着添加的新功能...

void MainWindow::whatIsSelected(){ 
QDebug() <<scene->selectedItems() << endl;} 

然後我做了現場別處窗口的連接...

QObject::connect(scene, SIGNAL(selectionChanged()), this, SLOT(whatIsSelected); 
+0

如果您只調用一次drawImage,但是每次調用「connect」,它都會創建一個新的連接而不刪除舊連接。如果添加大量圖像,則每次選擇更改時,「whatIsSelected」方法將開始多次調用。要麼在其他地方移動「連接」調用,以便只執行一次,要麼將「Qt :: UniqueConnection」連接類型添加到「連接」調用。 – goug

+0

我絕對需要它添加在我創建的每個項目上。我的用戶需要能夠根據需要隨時選擇/取消選擇,以便瀏覽相關數據。並且會有大量的圖像被添加。 – bauervision

+0

編輯我的答案,包括你的 – bauervision

相關問題