Qt:在子部件上使用 QPainter

发布于 2024-08-09 04:52:11 字数 1004 浏览 6 评论 0原文

我在使用一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生一个溪 2024-08-16 04:52:11

我尝试的第一件事是在子小部件上安装事件过滤器(请参阅 QObject::installeEventFilter()),然后,在父窗口小部件中,捕获 QEvent::Paint 事件,并在那里进行绘制。

创建子小部件的位置:

// ...
    childWidget = new QWidget(this);
    childWidget->installEventFilter(this);
// ...

然后在父小部件中:

bool Draw::eventFilter(QObject* watched, QEvent* event)
{
    if (watched == childWidget && event->type() == QEvent::Paint) {
        QPainter painter;
        painter.begin(childWidget);
        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();
        return true; // return true if you do not want to have the child widget paint on its own afterwards, otherwise, return false.
    }
    return false;
}

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 :

// ...
    childWidget = new QWidget(this);
    childWidget->installEventFilter(this);
// ...

Then in the parent :

bool Draw::eventFilter(QObject* watched, QEvent* event)
{
    if (watched == childWidget && event->type() == QEvent::Paint) {
        QPainter painter;
        painter.begin(childWidget);
        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();
        return true; // return true if you do not want to have the child widget paint on its own afterwards, otherwise, return false.
    }
    return false;
}
我喜欢麦丽素 2024-08-16 04:52:11

正如 QPainter 文档中所述

警告:当paintdevice是一个widget时,QPainter只能在paintEvent()函数内部或由paintEvent()调用的函数中使用;除非设置了 Qt::WA_PaintOutsidePaintEvent 小部件属性。在 Mac OS X 和 Windows 上,无论此属性的设置如何,您都只能在 PaintEvent() 函数中进行绘制。

如果您想在该小部件上绘图,您将需要从它自己的paintEvent() 中进行操作。

As stated in the QPainter documentation

Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting.

If you want to draw on that widget you will need to do so from it's own paintEvent().

白日梦 2024-08-16 04:52:11

您可以在像素图上绘画并在小部件绘画事件中绘制像素图。它可以是任何函数或插槽,不一定是绘画事件,例如您可以有多个用于绘制不同的对象。您可以从像素图上的任何位置进行绘制,使用绘制事件的要求仅适用于将绘制像素图的小部件。如果场景很复杂,您甚至可以在另一个线程中绘制,并且只更新主线程中像素图的结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文