- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的 QGraphicswidget 中嵌入一个 QWidget,例如按钮或进度条,但我只看到了将 QWidget 添加到 QGraphicsScene 的示例,即
m_scene->addWidget(new QPushButton("Test Test"));
在我的自定义图形小部件中,我在绘画功能中绘制文本和其他自定义形状。我认为您需要在此处添加 QWidget,但我可能错了。有谁知道如何做到这一点?
这是我重载的绘画函数:
void TestWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem
*option, QWidget *widget /*= 0*/)
{
Q_UNUSED(widget);
Q_UNUSED(option);
QRectF frame(QPointF(0,0), geometry().size());
QGradientStops stops;
//Draw border
painter->drawRoundedRect(boundingRect(), 5.0, 5.0);
//Name of the test
painter->drawText(40, 20, m_name);
//Status of test
QFont font = painter->font() ;
font.setPointSize(14);
painter->setFont(font);
painter->drawText(600, 20, m_status);
//Arrow button
QPolygonF poly;
poly << QPointF(5, 10) << QPointF(25, 10) << QPointF(15, 20 )<<
QPointF(5,10);
painter->setBrush(Qt::black);
painter->drawPolygon(poly, Qt::OddEvenFill);
}
最佳答案
为了嵌入一个小部件,例如QPushButton,在您的 QGraphicsWidget 子类中使用 QGraphicsProxyWidget像这样:
#include "TestWidget.h"
#include <QGraphicsProxyWidget>
#include <QPushButton>
TestWidget::TestWidget(QGraphicsItem *parent) :
QGraphicsWidget(parent)
{
...
auto *proxy = new QGraphicsProxyWidget(this);
proxy->setWidget(new QPushButton(tr("CLick me")));
proxy->moveBy(20, 40);
...
}
如果使用m_scene->addWidget(new QPushButton("Test Test"));
,就是essentially the same作为:
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
proxy->setWidget(new QPushButton("Test Test"));
m_scene->addItem(proxy);
您将QPushButton(通过代理)直接添加到场景。
如果您想让QPushButton成为您的自定义QGraphicsWidget的部分,请设置QGraphicsProxyWidget的父级> 到自定义 QGraphicsWidget 的实例。
注意:无需调用QGraphicsScene::addItem,因为(由于父/子关系)代理将与您的自定义 QGraphicsWidget。
使用您的paint
方法,结果类似于:
关于c++ - 是否可以在自定义 QGraphicsWidget 中嵌入 QWidget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51993096/
在我的应用程序中,我继承了 QGraphicsWidget 在绘画中,我画了一条笔宽为 4 的线。 我重新实现了 boundingRect() 和 shape()。 但是每次单击鼠标右键时我都无法捕获
在我的应用程序中,我有两种对象类型。一个是字段项,另一个是复合项。复合项可能包含两个或更多字段项。这是我的复合项目实现。 #include "compositeitem.h" CompositeIte
我是第一次使用 QGraphicsView/Scene。这是在 PyQt 中,但除了 Python 与多重继承不同之外,我认为这是一个通用的 Qt 问题。 我首先创建了几个 QGraphicsItem
我有一个名为“TestContentArea”的自定义 QGraphicsWidget,它已添加到 QGraphicsScene。在我的自定义 QGraphicsWidget 中,我添加了一些添加到其
我正在制作简单的 2D 游戏(迷宫),我正在使用 QGraphicsView、QGraphicsScene 和 QGraphicsGridLayout 来管理网格中的图 block 。 map 用字符
我试图在 QGraphicsWidget 中显示 QGraphicsView,但我似乎无法让它工作。 这就是我想要实现的目标,其中QGraphicsWidgets全部添加到QGraphicsScene
我正在尝试在 qgraphicswidget 中绘制文本。场景的水平比例为 -180 到 180,垂直比例为 -90 到 +90(这是一张世界地图)。 当我放大 map 上的单个项目时,我希望显示一些
我想在我的 QGraphicswidget 中嵌入一个 QWidget,例如按钮或进度条,但我只看到了将 QWidget 添加到 QGraphicsScene 的示例,即 m_scene->addWi
首先我应该说我已经尝试从所有 POV 来看这个问题。我也在 freenode 上询问过,有人向我提出了一些建议,但最终证明这些建议不可能奏效。 在我看来,那些提出建议的人似乎并不真正了解类层次结构和概
有没有可能 QGraphicsWidget成为 QWidget 的 parent ?我有一个 QGraphicsItem我想添加一个 QWidget在此项目中,如何设置 QWidget在QGraphi
这是否可能具有 QObjects 和(例如)QGraphicsWidgets 的树状结构?我的意思是,我不能像这样编写初始化列表: class MyButton : public QGraphicsW
我通过 QGraphicsProxyWidget 向图形场景 (QGraphicScene) 添加了一个小部件。问题是当我选择项目时它被选中,但选择边框不可见。 代码如下: QDial *di
我已经建立了一个自定义 QGraphicsView为某些按键事件定义了行为。 View 显示 QGraphicsScene与 QGraphicsProxyWidgets . 问题是,一旦我覆盖了 ke
我希望我的 QGraphicsWidget 根据场景的大小来缩放它的大小。我目前拥有的 QGraphicsWidget 是固定大小,具体取决于 sizeHint 的返回值(QGraphicsWidge
我有一个相当简单的 PyQt 应用程序,我在其中将 QGraphicsWidget 的实例放置在 QGraphicsGridLayout 中,并希望将小部件与用 QGraphicsPath 绘制的线连
我正在将更大的 QGraphicsItems 程序转换为 QGraphicsWidgets(为了打字,我们称它们为 item 和 widget)。鼠标悬停现在失败,因为小部件的位置和/或矩形与旧项目不
我是一名优秀的程序员,十分优秀!