Paint in a Widget.



  • 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.



  • 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



  • 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.



  • 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.



  • 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.



  • 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.


  • Lifetime Qt Champion

    Hi,

    The missing parenthesis as no such implication.

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



  • 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.



  • 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?



  • 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.



  • 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.



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.