Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Paint in a Widget.
Qt 6.11 is out! See what's new in the release blog

Paint in a Widget.

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 4.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Federico Perez
    wrote on last edited by
    #1

    Hello.
    I have 2 class

    class Window : public QWidget //Main and include renderarea.h
    class RenderArea : public QWidget

    In the window.ui I put a widget in desing mode, I want to paint in that widget and I Promote to RenderArea class. The paintEvent in that class work fine. I put a button in the window.ui, and I want to paint when the button is clicked, dosent work.

    For example:
    I have these in the RenderArea.h

    public slots:
    void setPaint(bool paint);

    and in the RenderArea.cpp in the paintEvent
    if(paint)
    painter.drawText(rect(), Qt::AlignCenter, "Qt");

    The problem consist when I call update(), the value of paint return to false, and never paint.

    I hope someone can help me.
    Sorry for my english.
    Thx.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kuschky
      wrote on last edited by
      #2

      Helle Federico,

      can you send some more code where you instantiate and initialize the Widget? And did you connect the buttons click signal with the widgets Slot which should react on the event? There must be somewhere a
      connect method. You can check if the signal/slot connection is o.k. by evaluating the connect methods return status. If it false the signal / slot connection is not valid.

      Best regards

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Federico Perez
        wrote on last edited by
        #3

        Thx for answer.

        I put a Widget in the window.ui, at Desing Mode. I make a photo and publish in weTransfer, here its the link
        http://we.tl/TZc8kvWCR4

        As you can see in that photo I wanna Paint when I click in the Paint Now Button, and Paint wherever I want, in that case just "Qt". The widget its "Promote to" RenderArea class. When I override the paintEvent in RenderArea class its paint in that widget. One of the advantage its that I can put that widget wherever I want in the window.ui at design mode, much easier than if I have to do all in code.

        window.h

        @
        #include "renderarea.h"

        namespace Ui {
        class Window;
        }

        class Window : public QWidget
        {
        Q_OBJECT
        public:
        Window();

        public slots:
        void paintNow();

        private:
        Ui::Window *ui;

        RenderArea *originalRenderArea;
        

        };
        @

        window.cpp
        @
        Window::Window() :
        ui(new Ui::Window)
        {
        ui->setupUi(this);

        originalRenderArea = new RenderArea;
        
        connect(ui->pushButton, SIGNAL(clicked()),
                this, SLOT(paintNow()));
        

        }

        void Window::paintNow()
        {
        originalRenderArea->setPaint("Yes");
        }
        @

        renderarea.h
        @
        class RenderArea : public QWidget
        {
        Q_OBJECT
        public:
        RenderArea(QWidget *parent = 0);

        QString paint;
        

        public slots:
        void setPaint(QString paint);

        protected:
        void paintEvent(QPaintEvent *);

        };
        @

        renderarea.cpp
        @
        RenderArea::RenderArea(QWidget *parent)
        : QWidget(parent)
        {
        paint = "No";
        }

        void RenderArea::setPaint(QString paint)
        {
        this->paint = paint; //At this point value change to "Yes"
        update(); //When I call update in the paintEvent the value of paint return to "No"
        }

        void RenderArea::paintEvent(QPaintEvent *)
        {
        QPainter painter(this);

        if(paint=="Yes")       //Always the value of paint its "No".
            painter.drawText(rect(), Qt::AlignCenter, "Qt");
        
        //If I said if(true) {} it works and paint inside widget "Qt".
        

        }
        @

        I hope you can help me. I cant not understand for what reason the value of paint return to "No" when I call the update().

        Thanks for answer.
        PD: forgive my English, I speak Spanish.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kuschky
          wrote on last edited by
          #4

          Hello Federico, if I understand your code correctly, you are setting a flag which tells the paintEvent if drawText should be executed or not.

          I think the problem is your connect call. I think it have to look like this:

          @
          bool success = connect(pushButton, SIGNAL( clicked() ), this , SLOT( paintNow() ) );
          @

          The return value tells you if the button click event was successful connect to the paintNow() Slot or not.

          can you check this? If connect was successful maybe set a breakpoint into paintNow to see if the debugger stops after pressing the button.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Federico Perez
            wrote on last edited by
            #5

            kuschky Thanks for answer,

            "you are setting a flag which tells the paintEvent if drawText should be executed or not."

            that is exactly what I want to do

            if(paint=="Yes) then paint.

            The debugger stops after pressing the button. I trace my code, step by step, when I press the button, its call
            @
            void Window::paintNow()
            {
            originalRenderArea->setPaint("Yes");
            }
            @

            and goes to renderarea.cpp in

            @
            void RenderArea::setPaint(QString paint)
            {
            this->paint = paint;
            update();
            }
            @

            When I pass step by step at
            @
            this->paint = paint; //At this point value change to "Yes"
            @

            Then comes
            @
            update(); //this call paintEvent();
            @

            And then in the paintEvent method, the value of paint return to "No" and dont paint nothing, I can see this in debugger time, step by step, with F10, crazy.

            Thanks.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kuschky
              wrote on last edited by
              #6

              Hello, I can't see anything wrong except that the Constructor of RenderArea is not called on the common way:

              @originalRenderArea = new RenderArea;@

              I would expect this

              @originalRenderArea = new RenderArea();@

              Maybe it results in the observed behaviour.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                The missing parenthesis as no such implication.

                Are you by any chance changing the "paint" value anywhere else ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  Federico Perez
                  wrote on last edited by
                  #8

                  Thanks for answer.
                  kuschky like SGaist said The missing parenthesis as no such implication.

                  And the code posted here, is all the code!, there its nothing more.

                  What I want to do its very simple, put a flag for paint when I press a button. Firts its change in setPaint in renderArea, but when update() and goes to paintEvent(), the paint value return to "No", incredible.

                  I honestly do not know what else to do.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    Federico Perez
                    wrote on last edited by
                    #9

                    I insert another method to verify the value of paint before I call update()

                    renderarea.h
                    @
                    void verify();
                    @

                    renderarea.cpp
                    @
                    RenderArea::RenderArea(QWidget *parent)
                    : QWidget(parent)
                    {
                    paint = "No";
                    }

                    void RenderArea::setPaint(QString paint)
                    {
                    this->paint = paint; //At this point value change to "Yes"

                    verify();    //I call verify() first to verify the value of paint
                    
                    update();  
                    

                    }

                    void RenderArea::verify()
                    {
                    if(paint=="Yes") //The value has "Yes" yet and execute a+b
                    {
                    int a, b;
                    int c = a+b;
                    }
                    }

                    //Only when update() its called the value return to the original value "No"

                    void RenderArea::paintEvent(QPaintEvent *)
                    {
                    QPainter painter(this);

                    if(paint=="Yes")
                        painter.drawText(rect(), Qt::AlignCenter, "Qt");
                    

                    }
                    @

                    As you can see the problem its in update(), is like the update refresh all variable in the class, I dont know why. Look like pass again for

                    @
                    RenderArea::RenderArea(QWidget *parent)
                    : QWidget(parent)
                    {
                    paint = "No";
                    }
                    @

                    but when I debbuger step by step, does not

                    Any ideas?

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      Federico Perez
                      wrote on last edited by
                      #10

                      I put all the source code of the project on the site wetransfer.com so if you want you can download it and will be better for you to deduce what the problem is.

                      http://we.tl/AbBn0a2Zo9

                      Thanks for all the assistance, I continued trying but can not find a solution yet.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kuschky
                        wrote on last edited by
                        #11

                        Thanks for uploading the code.

                        Your project contains a header file named window.h. In this header file a class QWindow is locally declared. This a class definition which is normaly provided by Qt itself !

                        @class QWindow : public QWidget
                        {...@

                        Also to use project specific header files named window.h can be dangerous because Windows SDK has a header file named like this.

                        I would setup a new project, without defining a class QWindow, with a main window named mywindow QtCreator produces then a mywindow.h and mywindow.cpp.

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          Federico Perez
                          wrote on last edited by
                          #12

                          Thanks for your attention
                          I upload the new code, with the change that you said. Nothing happend, its continuos without paint when I want.

                          http://we.tl/CkREoMkpsO

                          Any other ideas?

                          Thank you very much for your attention

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved