Qt:在子部件上使用 QPainter
我在使用一个简单的 QWidget
程序时遇到 Qt/C++ 问题,该程序在子 QWidget
内绘制椭圆。
该计划由以下部分组成:
(1) 父QWidget
(2) 子QWidget
(用作椭圆的绘图表面)
(3)绘制QPushButton
这里是部分代码(为简单起见,省略了QPushButton Slot和Signal代码)
void Draw::paintEvent(QPaintEvent *event) {
QPainter painter;
painter.begin(child_Widget); //The line with the problem
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap));
painter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
painter.drawEllipse(50, 50, 100, 100);
painter.end();}
第2行painter.begin(child_Widget) ;
不执行任何操作。仅当我将第 2 行替换为 Painter.begin(this); 时,程序才会绘制椭圆,但这会在父 QWidget
上绘制,而不是在子 QWidget< 上绘制。 /code> 根据需要。
PS 我将 child_Widget 放置在 GroupBox 内,而 GroupBox 又放置在 QVBoxLayout 内。
有什么建议吗?
谢谢。
I'm having a Qt/C++ problem with a simple QWidget
program that draws an ellipse inside a child QWidget
.
The program is composed of:
(1) A parent QWidget
(2) A child QWidget
(used as the drawing surface for an ellipse)
(3) A draw QPushButton
Here is part of the code (QPushButton Slot and Signal code omitted for simplicity)
void Draw::paintEvent(QPaintEvent *event) {
QPainter painter;
painter.begin(child_Widget); //The line with the problem
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap));
painter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
painter.drawEllipse(50, 50, 100, 100);
painter.end();}
Line 2 painter.begin(child_Widget);
doesn't do anything. The program draws the ellipse only if I replace line 2 with painter.begin(this);
but that draws on the parent QWidget
and not on the child QWidget
as desired.
P.S. I have the child_Widget housed inside a GroupBox which in turn is housed inside a QVBoxLayout
.
Any Suggestion?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我尝试的第一件事是在子小部件上安装事件过滤器(请参阅 QObject::installeEventFilter()),然后,在父窗口小部件中,捕获 QEvent::Paint 事件,并在那里进行绘制。
创建子小部件的位置:
然后在父小部件中:
First thing I'd try would be to install an event filter on the child widget (see QObject::installeEventFilter()), then, in the parent widget, grap the QEvent::Paint event, and do the painting there.
Where you create the child widget :
Then in the parent :
正如 QPainter 文档中所述
如果您想在该小部件上绘图,您将需要从它自己的paintEvent() 中进行操作。
As stated in the QPainter documentation
If you want to draw on that widget you will need to do so from it's own paintEvent().
您可以在像素图上绘画并在小部件绘画事件中绘制像素图。它可以是任何函数或插槽,不一定是绘画事件,例如您可以有多个用于绘制不同的对象。您可以从像素图上的任何位置进行绘制,使用绘制事件的要求仅适用于将绘制像素图的小部件。如果场景很复杂,您甚至可以在另一个线程中绘制,并且只更新主线程中像素图的结果。
You could paint on a pixmap and draw the pixmap in the widget paint event. And it can be any function or slot, not necessarily a paint event, e.g. you can have multiple for drawing different objects. You can draw from anywhere on a pixmap, the requirement that the paint event is used is just for the widget that will draw the pixmap. You can even draw in another thread if it is a complex scene and only update the result from the pixmap in the main thread.