Qt 在一个窗口中绘制图形

发布于 2024-11-10 05:47:28 字数 2317 浏览 0 评论 0原文

  1. 我创建类 Widget,它创建窗口,这个类在窗口上绘制一些东西(即它按我想要的方式工作)。
  2. 我创建了一个类 Circle,我想在 Widget 类的窗口上进行绘制。
  3. 我传递 Widget 的地址并尝试使用 QPainter Paint (Widget 的地址)在 Widget 上绘画; (以 Circle 为例)但我没有看到任何东西。

我试图在程序执行过程中使代码尽可能短,我键入对象 Widget 的地址。它没有改变。说明Widget的地址传递正确。

在任何地方,只要我输入 Widget 地址,我都会收到相同的地址。代码如下:

header Widget

            class Widget : public QWidget
            {
                public:
                int  mi,mcount;
                Widget(QWidget *parent = 0);
                QPaintEvent *ev;
                virtual void paintEvent(QPaintEvent *);
            void drawcircle();
            };

Widget.cpp

Widget::Widget(QWidget *parent) : QWidget(parent)
{
    QWidget::paintEvent(ev);

    qDebug()<<this<<"\n";  //
}

        void Widget::drawcircle()
        {
        QPainter paint(this);
        paint.drawEllipse(0,0,100,100);
        }


void Widget::paintEvent(QPaintEvent *ev)
{    this->drawcircle(); }

header Circle.h

        class Circle :public QWidget
        {
            public:
            Circle(Widget *widget);    // i do trick here!!!
            Widget *mwidg;
        QPaintEvent *ev;

        virtual void paintEvent(QPaintEvent *);
        void drawcircle(Widget *mwidg);
        };

Circle.cpp

    Circle::Circle(Widget *widget)
        {
        qDebug()<<"circle widget"<<widget;
        QWidget::paintEvent(ev);
        mwidg=widget;
        qDebug()<<"\n"<<mwidg;
        }



    void Circle::paintEvent(QPaintEvent *ev)
    {  qDebug()<<"circle paintEvent mwidget"<<mwidg<<"\n";
    this->drawcircle(mwidg);
        }


        void Circle::drawcircle(Widget *mwidg)
            {
                QPainter paint(mwidg);
                paint.drawEllipse(20,10,40,40);
            paint.drawLine(0,0,500,500);
            }

主程序

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget *w=new Widget;

            qDebug()<<"main address of widget"<<w<<"\n";
        Circle *f=new Circle(w);
        w->show();

        return a.exec();
        }

编译链接成功

  1. I create class Widget, it creates window, this class paints something on the window (i.e. it works as I want).
  2. I create yet one class, Circle, I want to paint on the window of class Widget.
  3. I pass adress of Widget and try to paint on Widget using QPainter paint (address of Widget); (in the instance of Circle) but i don't see anything.

I've tried to make code as shorter as possible during the execution of program I type out address of object Widget. It doesn't change. It means that the address of Widget was passed right.

Everywhere, where I type out address of Widget I receive the same address. Here is the code:

header Widget

            class Widget : public QWidget
            {
                public:
                int  mi,mcount;
                Widget(QWidget *parent = 0);
                QPaintEvent *ev;
                virtual void paintEvent(QPaintEvent *);
            void drawcircle();
            };

Widget.cpp

Widget::Widget(QWidget *parent) : QWidget(parent)
{
    QWidget::paintEvent(ev);

    qDebug()<<this<<"\n";  //
}

        void Widget::drawcircle()
        {
        QPainter paint(this);
        paint.drawEllipse(0,0,100,100);
        }


void Widget::paintEvent(QPaintEvent *ev)
{    this->drawcircle(); }

header Circle.h

        class Circle :public QWidget
        {
            public:
            Circle(Widget *widget);    // i do trick here!!!
            Widget *mwidg;
        QPaintEvent *ev;

        virtual void paintEvent(QPaintEvent *);
        void drawcircle(Widget *mwidg);
        };

Circle.cpp

    Circle::Circle(Widget *widget)
        {
        qDebug()<<"circle widget"<<widget;
        QWidget::paintEvent(ev);
        mwidg=widget;
        qDebug()<<"\n"<<mwidg;
        }



    void Circle::paintEvent(QPaintEvent *ev)
    {  qDebug()<<"circle paintEvent mwidget"<<mwidg<<"\n";
    this->drawcircle(mwidg);
        }


        void Circle::drawcircle(Widget *mwidg)
            {
                QPainter paint(mwidg);
                paint.drawEllipse(20,10,40,40);
            paint.drawLine(0,0,500,500);
            }

main

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget *w=new Widget;

            qDebug()<<"main address of widget"<<w<<"\n";
        Circle *f=new Circle(w);
        w->show();

        return a.exec();
        }

program is compiled and linked successful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

荒人说梦 2024-11-17 05:47:28

您究竟想要实现什么目标?您只能在其自己的paintEvent() 处理程序中的任何小部件上进行绘制,并且您不应该自己调用paintEvent(),它不会工作。另外,删除 QPaintEvent 成员变量。

我建议您将 Circle 作为 Widget 的子级,然后通过 Circle::paintEvent() 绘制圆。或者,使用 QGraphicsView。

What exactly are you trying to achieve? You can only paint on any widget in it's own paintEvent() handler, and you should not call paintEvent() yourself, it won't work. Also, get rid of the QPaintEvent member variables.

I would suggest that you make Circle a child of Widget, and then paint the circle from Circle::paintEvent(). Alternatively, use QGraphicsView.

君勿笑 2024-11-17 05:47:28

好的,谢谢您的帮助
但我所需要的一切:

这个 ->设置父级(小部件);

在构造函数 Circle::Circle 中,如果有人想看我的解决方案,可以看到数字被移动

源代码在这里 源代码

在此处输入图像描述

well , thank you for your attempts to help
but all that i was needing:

this -> setParent(widget);

in costructor Circle::Circle, if somebody'll want to see my solve,one can see that figures are moved

source code is here source code

enter image description here

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