Passing QString to PaintEvent



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



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



  • Hi @pie62 ,
    Thank you. It worked perfectly.


Log in to reply
 

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