Deleting rectangle drawn by QPainter



  • Hi,
    My goal is to show a message in a blue rectangle, wait 6 seconds, delete the rectangle and show again the original page (which was underneath the blue square).
    Right now I have the following code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget* parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow) {
      ui->setupUi(this);
    }
    
    MainWindow::~MainWindow() {
      delete ui;
    }
    
    void MainWindow::paintEvent(QPaintEvent* event) {
      Q_UNUSED(event);
    
      QPainter painter(this);
    
      QFont font = painter.font ();
    
      font.setFamily ("Arial");
      font.bold ();
      font.setPixelSize (25);
      painter.setFont (font);
      painter.setPen (Qt::white);
    
      QRect rect(100, 120, 200, 50);
      painter.setBrush (Qt::blue);
      painter.drawRect (rect);
    
      painter.drawText (rect, Qt::AlignCenter, tr("Record deleted."));
      painter.end ();
    
    //  ClosePaint ();
      QTimer::singleShot (600, this, SLOT(ClosePaint()));
    
    }
    
    void MainWindow::ClosePaint() {
      qDebug()   << "DeletePaint called.";
    
    
    }
    
    

    What is the best way to delete the blue square and display the original page? Thank you.


  • Qt Champions 2017

    There is no option to delete the rectangle. Screen need to repainted again. Move the timer outside the paintEvent(). You need to call update() or repaint() function.

    Place the following code in constructor.
    QTimer::singleShot (600, this, SLOT(closePaint()));
    void MyWidget::closePaint() {
    this.update()
    }

    Better inherit from QWidget and implement paintEvent() function.



  • @dheerendra
    Hi,
    I have the following now:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget* parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow) {
      ui->setupUi(this);
    }
    
    MainWindow::~MainWindow() {
      delete ui;
    }
    
    void MainWindow::paintEvent(QPaintEvent* event) {
      Q_UNUSED(event);
    
      QPainter painter(this);
    
      QFont font = painter.font ();
    
      font.setFamily ("Arial");
      font.bold ();
      font.setPixelSize (25);
      painter.setFont (font);
      painter.setPen (Qt::white);
    
      QRect rect(100, 120, 200, 50);
      painter.setBrush (Qt::blue);
      painter.drawRect (rect);
    
      painter.drawText (rect, Qt::AlignCenter, tr("Record deleted."));
      painter.end ();
    
      QTimer::singleShot (600, this, SLOT(ClosePaint()));
    }
    
    void MainWindow::ClosePaint() {
    //  qDebug()   << "DeletePaint called.";
      this->update ();
    }
    
    

    When program execution reaches QTimer it jumps to ClosePaint() and from this->update() QTimer without doing anything. What is still wrong? Thank you.


  • Moderators

    @gabor53 1. I don't see where you're deleting the rectangle

    1. Why do you call QTimer::singleShot (600, this, SLOT(ClosePaint())); inside paintEvent?! This way you will start a new singleShot() every time paintEvent is called! You need to call QTimer::singleShot (600, this, SLOT(ClosePaint())); somewhere else.


  • @gabor53
    hi, this should do the trick for you.

    void MainWindow::showRect(){
        QTimer::singleshot(600, this, &MainWindow::hideRect);
        m_showRect = true;
        update(); 
    }
    
    void MainWindow::hideRect(){
        m_showRect = false;
        update(); 
    }
    
     void MainWindow::paintEvent(QPaintEvent* event) {
     
    if(m_showRect ){//Only draw rect when you want it
       QPainter painter(this);
     
       QFont font = painter.font ();
     
       font.setFamily ("Arial");
       font.bold ();
       font.setPixelSize (25);
       painter.setFont (font);
       painter.setPen (Qt::white);
     
       QRect rect(100, 120, 200, 50);
       painter.setBrush (Qt::blue);
       painter.drawRect (rect);
     
       painter.drawText (rect, Qt::AlignCenter, tr("Record deleted."));
       painter.end ();
    }else{
        QWidget::paintEvent(event);//When no recttangle call base implementation
    }
    

  • Qt Champions 2017

    Please read my previous post. I requested you to place the singleshot code in constructor. Then it should work.



  • @J.Hilk
    Sorry for the delayed response, but I had to rebuild my computer. Currently I have the following:
    mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QPaintEvent>
    #include <QPainter>
    #include <QRect>
    #include <QTextDocument>
    #include <QFont>
    #include <QBrush>
    #include <QPen>
    #include <QTimer>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow {
      Q_OBJECT
    
     public:
      explicit MainWindow(QWidget* parent = 0);
        void showRect();
        void hideRect();
    
        bool m_showRect;
      ~MainWindow();
    
     private:
      Ui::MainWindow* ui;
    
     protected:
      void paintEvent(QPaintEvent* event);
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget* parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    void MainWindow::showRect()
    {
        QTimer::singleShot(6,this,&MainWindow::hideRect);
        m_showRect = true;
        update();
    }
    
    void MainWindow::hideRect()
    {
        m_showRect = false;
        update();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::paintEvent(QPaintEvent* event)
    {
    
        if(m_showRect)
            {
    
                QPainter painter(this);
    
                QFont font = painter.font ();
    
                font.setFamily ("Arial");
                font.setPixelSize (25);
                painter.setFont (font);
                painter.setPen (Qt::white);
    
                QRect rect(100, 120, 200, 50);
                painter.setBrush (Qt::blue);
                painter.drawRect (rect);
    
                painter.drawText (rect, Qt::AlignCenter, tr("Record deleted."));
                painter.end();
            }
        else
            {
                QWidget::paintEvent(event);
            }
    }
    
    

    It displays the blue rectangle with the text, but the timing doesn't work; it does't redraw the page. What did I do wrong?


  • Moderators

    @gabor53 Are you aware that singleShot takes time as milliseconds? You are passing 6 milliseconds - is this really what you want to do?



  • @jsulm
    Hi,
    I changed the 6 to 6000, but the page still doesn't ger redrawn.



  • @gabor53 ,

    change 6 to 6000.
    QTimer::singleShot(6000,this,&MainWindow::hideRect);

    then, call showRect() from the constructor.



  • The following solution worked:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget* parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
    
        QFont font = painter.font ();
    
        font.setFamily ("Arial");
        font.setPixelSize (25);
        painter.setFont (font);
        painter.setPen (Qt::white);
    
        QRect rect(100, 120, 200, 50);
        painter.setBrush (Qt::blue);
        painter.drawRect (rect);
    
        painter.drawText (rect, Qt::AlignCenter, tr("Record deleted."));
        painter.end();
        QTimer::singleShot(3000,this,&MainWindow::hideRect);
        update();
    }
    

    Thank you for your help.


Log in to reply
 

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