繁体   English   中英

如何将QLabel添加到QGraphicsItem

[英]How to add QLabel to QGraphicsItem

我有一个QGraphicsItem ,上面有文字。 我希望此文本可编辑,因此如果用户双击它,它将进入编辑模式。 似乎最简单的方法是将文本更改为QLineEdit ,让用户点击焦点或在完成后按Enter键。

如何将QLineEdit添加到QGraphicsItem 我已经将QGraphicsItem子类化,因此我可以访问其内部。

要将任何基于QWidget的对象添加到QGraphicsScene ,需要QGraphicsProxyWidget

当您在QGraphicsScene上调用函数addWidget时,它将窗口小部件嵌入到QGraphicsProxyWidget ,并将该QGraphicsProxyWidget返回给调用者。

QGraphicsProxyWidget将事件转发到其窗口小部件,并处理不同坐标系之间的转换。

现在您正在考虑在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);

或者只是将其添加到当前的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

或者您可以将QGraphicsProxyWidget添加为类的成员并调用其相关函数,但将其添加为子项可能要简单得多。

QGraphicsTextItem::setTextInteractionFlags (Qt::TextInteractionFlags flags)

可以使用API​​。 但是你需要在其中创建QGraphicsTextItem

请查看以下链接了解详细信息: 实施细节

您需要通过在需要某些特定行为或仅使用QGraphicsProxyWidget的情况下扩展QGraphicsProxyWidget来创建代理窗口小部件。 请查看Qt SDK和QGraphicsProxyWidget文档中的“嵌入式对话框”示例。 它已经存在了很长时间,所以它应该是你的版本。 我希望这有帮助。

暂无
暂无

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

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