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. Passing QString to PaintEvent
Forum Updated to NodeBB v4.3 + New Features

Passing QString to PaintEvent

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 641 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by gabor53
    #1

    Hi,
    I have the following code to display a rectangle with a message for 3 seconds:

    #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)
    {
        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 (QColor(65, 163, 255));
                painter.drawRect (rect);
    
                painter.drawText (rect, Qt::AlignCenter, tr("Record deleted."));
                painter.end();
                QTimer::singleShot(3000,this,&MainWindow::hideRect);
            }
        else
            {
                QWidget::paintEvent(event);
            }
    }
    
    void MainWindow::hideRect()
    {
        m_showRect = false;
        update();
    }
    

    I'd like to rewrite it in a way to be able to pass different messages in a QString so I would be able to use it at several places in the program. I'm thinking of something like this:

    painter.drawText(rect,Qt::AlignCenter,tr(msg));
    

    where msg is the QString containing the message.
    Please show me the best way to pass msg to drawText. I tried

    
    void MainWindow::paintEvent(QPaintEvent *event, msg)
    

    but that doesn't work. Thank you.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pie62
      wrote on last edited by pie62
      #2

      @gabor53 said in Passing QString to PaintEvent:
      You can try look like this.

      // mainwindow.h
      class MainWindow(QWidget *parent = 0)
      {
      public:
          .......
          void setMsg(const QString &msg);
          .......
      private:
          QString msg;
      }
      
      //mainwindow.cpp
      #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 MainWondow::setMsg(const QString &msg)
      {
          this.msg = msg;
          update();
          QTimer::singleShot(3000,this,&MainWindow::hideRect);
      }
      
      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 (QColor(65, 163, 255));
                  painter.drawRect (rect);
      
                  painter.drawText (rect, Qt::AlignCenter, msg);
                  painter.end();  
              }
          else
              {
                  QWidget::paintEvent(event);
              }
      }
      
      void MainWindow::hideRect()
      {
          //m_showRect = false;
          //update();
          // doing
      }
      

      And now you can call MainWindow::setMsg("You text"); for change msg.

      G 1 Reply Last reply
      5
      • P pie62

        @gabor53 said in Passing QString to PaintEvent:
        You can try look like this.

        // mainwindow.h
        class MainWindow(QWidget *parent = 0)
        {
        public:
            .......
            void setMsg(const QString &msg);
            .......
        private:
            QString msg;
        }
        
        //mainwindow.cpp
        #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 MainWondow::setMsg(const QString &msg)
        {
            this.msg = msg;
            update();
            QTimer::singleShot(3000,this,&MainWindow::hideRect);
        }
        
        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 (QColor(65, 163, 255));
                    painter.drawRect (rect);
        
                    painter.drawText (rect, Qt::AlignCenter, msg);
                    painter.end();  
                }
            else
                {
                    QWidget::paintEvent(event);
                }
        }
        
        void MainWindow::hideRect()
        {
            //m_showRect = false;
            //update();
            // doing
        }
        

        And now you can call MainWindow::setMsg("You text"); for change msg.

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        Hi @pie62 ,
        Thank you. It worked perfectly.

        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