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. Deleting rectangle drawn by QPainter

Deleting rectangle drawn by QPainter

Scheduled Pinned Locked Moved Solved General and Desktop
qpainter
11 Posts 5 Posters 8.6k Views
  • 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.
  • G Offline
    G Offline
    gabor53
    wrote on 15 Nov 2017, 03:36 last edited by
    #1

    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.

    J 1 Reply Last reply 16 Nov 2017, 06:10
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 15 Nov 2017, 03:55 last edited by dheerendra
      #2

      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
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      G 1 Reply Last reply 16 Nov 2017, 03:47
      2
      • D dheerendra
        15 Nov 2017, 03:55

        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.

        G Offline
        G Offline
        gabor53
        wrote on 16 Nov 2017, 03:47 last edited by
        #3

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

        J 1 Reply Last reply 16 Nov 2017, 05:34
        0
        • G gabor53
          16 Nov 2017, 03:47

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 16 Nov 2017, 05:34 last edited by
          #4

          @gabor53 1. I don't see where you're deleting the rectangle
          2. 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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • G gabor53
            15 Nov 2017, 03:36

            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.

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 16 Nov 2017, 06:10 last edited by
            #5

            @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
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            G 1 Reply Last reply 30 Nov 2017, 04:15
            5
            • D Offline
              D Offline
              dheerendra
              Qt Champions 2022
              wrote on 17 Nov 2017, 03:16 last edited by
              #6

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

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • J J.Hilk
                16 Nov 2017, 06:10

                @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
                }
                
                G Offline
                G Offline
                gabor53
                wrote on 30 Nov 2017, 04:15 last edited by
                #7

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

                J 1 Reply Last reply 30 Nov 2017, 05:26
                0
                • G gabor53
                  30 Nov 2017, 04:15

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

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 30 Nov 2017, 05:26 last edited by
                  #8

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

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  G 1 Reply Last reply 1 Dec 2017, 03:26
                  0
                  • J jsulm
                    30 Nov 2017, 05:26

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

                    G Offline
                    G Offline
                    gabor53
                    wrote on 1 Dec 2017, 03:26 last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • Vinod KuntojiV Offline
                      Vinod KuntojiV Offline
                      Vinod Kuntoji
                      wrote on 1 Dec 2017, 03:53 last edited by
                      #10

                      @gabor53 ,

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

                      then, call showRect() from the constructor.

                      C++, Qt, Qt Quick Developer,
                      PthinkS, Bangalore

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        gabor53
                        wrote on 7 Dec 2017, 03:32 last edited by
                        #11

                        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.

                        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