QT中QGraphicsItem 獲取點(diǎn)坐標(biāo)的幾種方法
在 Qt 圖形視圖框架中,從 QGraphicsItem(特別是點(diǎn)圖元)獲取坐標(biāo)有多種方式,取決于你需要的是場(chǎng)景坐標(biāo)、視圖坐標(biāo)還是圖元自身的局部坐標(biāo)。
1. 獲取圖元在場(chǎng)景中的位置
對(duì)于 QGraphicsEllipseItem(點(diǎn)圖元)
// 假設(shè) item 是你的 QGraphicsEllipseItem 指針
if (QGraphicsEllipseItem* pointItem = qgraphicsitem_cast<QGraphicsEllipseItem*>(item)) {
// 獲取圖元中心點(diǎn)在場(chǎng)景中的坐標(biāo)
QPointF scenePos = pointItem->scenePos();
// 或者獲取包圍矩形的中心
QRectF rect = pointItem->rect();
QPointF centerInItem = rect.center();
QPointF centerInScene = pointItem->mapToScene(centerInItem);
qDebug() << "場(chǎng)景坐標(biāo):" << scenePos;
qDebug() << "精確中心坐標(biāo):" << centerInScene;
}2. 從 QMap<int, QGraphicsEllipseItem*> 獲取所有點(diǎn)坐標(biāo)
QMap<int, QGraphicsEllipseItem*> pointMap; // 你的點(diǎn)圖元容器
// 遍歷獲取所有點(diǎn)坐標(biāo)
for (auto it = pointMap.constBegin(); it != pointMap.constEnd(); ++it) {
int id = it.key();
QGraphicsEllipseItem* item = it.value();
QPointF scenePos = item->scenePos();
QRectF rect = item->rect();
QPointF center = item->mapToScene(rect.center());
qDebug() << "點(diǎn)ID:" << id << "場(chǎng)景坐標(biāo):" << center;
// 如果需要視圖坐標(biāo)(像素坐標(biāo))
QPoint viewPos = mGraphicsView->mapFromScene(center);
qDebug() << "視圖坐標(biāo):" << viewPos;
}3. 響應(yīng)點(diǎn)擊事件獲取坐標(biāo)
// 在場(chǎng)景子類中重寫鼠標(biāo)事件
void MyGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* event) {
QGraphicsScene::mousePressEvent(event); // 先讓基類處理選擇邏輯
// 獲取點(diǎn)擊位置的圖元
QGraphicsItem* item = itemAt(event->scenePos(), QTransform());
if (item && (item->type() == QGraphicsEllipseItem::Type)) {
QGraphicsEllipseItem* ellipse = static_cast<QGraphicsEllipseItem*>(item);
QPointF center = ellipse->mapToScene(ellipse->rect().center());
qDebug() << "點(diǎn)擊了點(diǎn)圖元,中心坐標(biāo):" << center;
}
}4. 獲取選中點(diǎn)的坐標(biāo)
// 獲取當(dāng)前選中的所有點(diǎn)圖元坐標(biāo)
QList<QPointF> getSelectedPointsCoordinates() {
QList<QPointF> coordinates;
foreach (QGraphicsItem* item, mScene->selectedItems()) {
if (item->type() == QGraphicsEllipseItem::Type) {
QGraphicsEllipseItem* ellipse = static_cast<QGraphicsEllipseItem*>(item);
coordinates.append(ellipse->mapToScene(ellipse->rect().center()));
}
}
return coordinates;
}5. 坐標(biāo)轉(zhuǎn)換說明
Qt 圖形視圖框架有三級(jí)坐標(biāo)系:
- 圖元局部坐標(biāo):相對(duì)于圖元自身的坐標(biāo)系
- 場(chǎng)景坐標(biāo):所有圖元共用的全局坐標(biāo)系
- 視圖坐標(biāo):窗口部件的像素坐標(biāo)系
常用轉(zhuǎn)換方法:
// 局部坐標(biāo) → 場(chǎng)景坐標(biāo) QPointF scenePos = item->mapToScene(localPos); // 場(chǎng)景坐標(biāo) → 視圖坐標(biāo) QPoint viewPos = graphicsView->mapFromScene(scenePos); // 視圖坐標(biāo) → 場(chǎng)景坐標(biāo) QPointF scenePos = graphicsView->mapToScene(viewPos);
注意事項(xiàng)
- 如果圖元有旋轉(zhuǎn)或縮放,直接使用
scenePos()可能不準(zhǔn)確,建議使用mapToScene(rect.center()) - 確保在獲取坐標(biāo)前圖元已被添加到場(chǎng)景中
- 對(duì)于非常精確的坐標(biāo)需求,考慮設(shè)置圖元的
CacheMode為DeviceCoordinateCache
到此這篇關(guān)于QT中QGraphicsItem 獲取點(diǎn)坐標(biāo)的幾種方法的文章就介紹到這了,更多相關(guān)QT QGraphicsItem 獲取點(diǎn)坐標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)基于時(shí)序公平的讀寫鎖詳解
讀寫鎖與普通的互斥鎖的區(qū)別在于有兩種上鎖方式:讀鎖和寫鎖,不用的用戶對(duì)同一個(gè)讀寫鎖獲取讀鎖是非互斥的,其他情況則是互斥的,本文小編將給大家詳細(xì)介紹C++實(shí)現(xiàn)基于時(shí)序公平的讀寫鎖,需要的朋友可以參考下2023-10-10
MFC中動(dòng)態(tài)創(chuàng)建控件以及事件響應(yīng)實(shí)現(xiàn)方法
這篇文章主要介紹了MFC中動(dòng)態(tài)創(chuàng)建控件以及事件響應(yīng)實(shí)現(xiàn)方法,詳細(xì)講解了MFC中動(dòng)態(tài)創(chuàng)建控件以及事件響應(yīng)的概念與實(shí)現(xiàn)方法,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
C/C++中static,const,inline三種關(guān)鍵字詳細(xì)總結(jié)
以下是對(duì)C/C++中static,const,inline的三種關(guān)鍵字進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-09-09
C語(yǔ)言如何實(shí)現(xiàn)循環(huán)輸入
這篇文章主要介紹了C語(yǔ)言如何實(shí)現(xiàn)循環(huán)輸入問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
C語(yǔ)言中十六進(jìn)制轉(zhuǎn)十進(jìn)制兩種實(shí)現(xiàn)方法
這篇文章主要介紹了C語(yǔ)言中十六進(jìn)制轉(zhuǎn)十進(jìn)制兩種實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-01-01
C++中將Char轉(zhuǎn)換成String的4種方法
本文主要介紹了C++中將Char轉(zhuǎn)換成String的4種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

