简体   繁体   English

如何将QLabel添加到QGraphicsItem

[英]How to add QLabel to QGraphicsItem

I have a QGraphicsItem that has text on it. 我有一个QGraphicsItem ,上面有文字。 I want this text to be editable, so that if the user double-clicks it, it will enter an edit mode. 我希望此文本可编辑,因此如果用户双击它,它将进入编辑模式。 It seems like the easiest way to do this would be to change the text into a QLineEdit and let the user click away the focus or press enter when they're done. 似乎最简单的方法是将文本更改为QLineEdit ,让用户点击焦点或在完成后按Enter键。

How can I add a QLineEdit to a QGraphicsItem ? 如何将QLineEdit添加到QGraphicsItem I have subclassed the QGraphicsItem so I have access to its internals. 我已经将QGraphicsItem子类化,因此我可以访问其内部。

To add any QWidget based object to a QGraphicsScene , a QGraphicsProxyWidget is required. 要将任何基于QWidget的对象添加到QGraphicsScene ,需要QGraphicsProxyWidget

When you call the function addWidget on QGraphicsScene , it embeds the widget in a QGraphicsProxyWidget and returns that QGraphicsProxyWidget back to the caller. 当您在QGraphicsScene上调用函数addWidget时,它将窗口小部件嵌入到QGraphicsProxyWidget ,并将该QGraphicsProxyWidget返回给调用者。

The QGraphicsProxyWidget forwards events to its widget and handles conversion between the different coordinate systems. QGraphicsProxyWidget将事件转发到其窗口小部件,并处理不同坐标系之间的转换。

Now that you're looking at using a QLineEdit in the QGraphicsScene , you need to decide if you want to add it directly: 现在您正在考虑在QGraphicsScene中使用QLineEdit ,您需要决定是否要直接添加它:

QGraphicsScene* pScene = new QGraphicsScene;
QLineEdit* pLineEdit = new QLineEdit("Some Text");

// add the widget - internally, the QGraphicsProxyWidget is created and returned
QGraphicsProxyWidget* pProxyWidget = pScene->AddWidget(pLineEdit);

Or just add it to your current QGraphicsItem . 或者只是将其添加到当前的QGraphicsItem Here, you can either add it as a child of the QGraphicsItem : 在这里,您可以将其添加为QGraphicsItem的子项:

MyQGraphicsItem* pMyItem = new MyQGraphicsItem;
QGraphicsProxyWidget* pMyProxy = new QGraphicsProxyWidget(pMyItem); // the proxy's parent is pMyItem
pMyProxy->setWidget(pLineEdit); // adding the QWidget based object to the proxy

Or you could add the QGraphicsProxyWidget as a member of your class and call its relevant functions, but adding it as a child is probably much simpler. 或者您可以将QGraphicsProxyWidget添加为类的成员并调用其相关函数,但将其添加为子项可能要简单得多。

QGraphicsTextItem::setTextInteractionFlags (Qt::TextInteractionFlags flags)

API can be used. 可以使用API​​。 But you need to create QGraphicsTextItem inside it. 但是你需要在其中创建QGraphicsTextItem

Please check following link for details: Implementation details 请查看以下链接了解详细信息: 实施细节

You need to create a proxy widget by extending QGraphicsProxyWidget in the case you need some specific behavior or just use a QGraphicsProxyWidget . 您需要通过在需要某些特定行为或仅使用QGraphicsProxyWidget的情况下扩展QGraphicsProxyWidget来创建代理窗口小部件。 Take a look at the "Embedded Dialogs" example in your Qt SDK and the QGraphicsProxyWidget documentation. 请查看Qt SDK和QGraphicsProxyWidget文档中的“嵌入式对话框”示例。 It has been there for a long time so it should be for your version. 它已经存在了很长时间,所以它应该是你的版本。 I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM