का स्क्रीन स्थिति खोजें केस का उपयोग करें: यह एक सामान्य समस्या होनी चाहिए। QMdiArea के साथ एक सामान्य QMainWindow में एक QGraphicsView के साथ एक mdiChild रहता है। यह दृश्य अंदर QGraphicsItems के साथ एक QGraphicsScene प्रदर्शित करता है। इन वस्तुओं में से किसी एक पर राइट-क्लिक आइटम (फोकस) का चयन करता है और एक संदर्भ मेनू खोलता है, जिसे आसानी से स्क्रीन पर निर्देशित किया जाता है QGraphicsSceneMouseEvent::screenPos()
। यह उम्मीद के रूप में काम कर रहा है।एक QGraphicsItem
अब जब मैं एक कुंजी दबाता हूं (उदा। Qt :: Key_Menu) मैं वही संदर्भ मेनू दिखाना चाहता हूं। मुझे चयनित (केंद्रित) आइटम पता है, मुझे दृश्य पता है जो दृश्य प्रदर्शित करता है।
तो मेरे सवाल है:
स्थान प्राप्त करने के लिए सही तरीका क्या है एक दृश्य के भीतर एक QGraphicsItem के दृश्य प्रतिनिधित्व की (वैश्विक में, स्क्रीन निर्देशांक)?
यहाँ क्या काम नहीं कर रहा है:
QGraphicsItem *item = ...; // is the currently selected item next to which the context menu shall be opened
QGraphicsScene *scene = ...; // is the scene that hosts the item
QGraphicsView *graphicsView = ...; // is the view displaying the scene, this inherits from QWidget
// get the position relative to the scene
QPointF sp = item->scenePos();
// or use
QPointF sp = item->mapToScene(item->pos());
// find the global (screen) position of the item
QPoint global = graphicsView->mapToGlobal(graphicsView->mapFromScene(sp));
// now
myContextMenu.exec(global);
// should open the context menu at the top left corner of the QGraphicsItem item, but it goes anywhere
The doc says: आप को पता है जहां व्यूपोर्ट में एक आइटम स्थित है चाहते हैं, आप QGraphicsItem :: mapToScene() मद पर, तो QGraphicsView कॉल कर सकते हैं: : mapFromScene() दृश्य पर।
जो मैं कर रहा हूं, ठीक है?
बस a thread in a german forum कि करने के लिए संकेत पर ठोकर खाई:
QGraphicsView *view = item->scene()->views().last();
या यहां तक कि अच्छे:
QGraphicsView *view;
foreach (view, this->scene()->views())
{
if (view->underMouse() || view->hasFocus()) break;
}
// (use case in the forum thread:) // QMenu *menu = new QMenu(view);
का उपयोग करना है कि मेरे सवाल का एक और अधिक सामान्यीकृत जवाब दे सकती है ...
मैं एक प्रतिक्रिया पोस्ट करने वाला था, लेकिन दस्तावेज़ों को दोबारा पढ़ने से मुझे लगता है कि मैं आपके विश्लेषण से सहमत हूं: QGraphicsView :: mapFromScene को व्यूपोर्ट निर्देशांक (लायक जांच) देना चाहिए। एमडीआई बच्चों के अंदर विजेट्स पर MapToGlobal में एक गुप्त बग है तो केवल सवाल ही होगा। –
@ जेम्ससर्नर तो आपका पहला अनुमान क्या होगा (जिसे आपने प्रतिक्रिया में लिखा होगा)? –