How to do QPainter paint in the Widget?



  • Hey guys,

    I would like to paint in the Qt Widget of dialog.ui or mainwindow.ui. How can I set my paint canvas as where I want it to be?? Should I create a new h and cpp file for Widget and promote to it?


  • Qt Champions 2016

    hi
    the mainwindow class
    do have a paintevent you can override
    but it often easier to use a plain Widget
    with a
    void MyWidget::paintEvent(QPaintEvent *)
    {
    QPainter painter(this);
    ....
    }
    function.
    and YES, you then place a plain widget and promote it - to your widget.

    Other way is to use a QLabel and draw on a pixmap and then let it show that pixmap.

    void MainForm::slot_buttonClick()
    {
        QPixmap pixmap(100,100);
        pixmap.fill(QColor("transparent"));
    
        QPainter painter(&pixmap);
        painter.setBrush(QBrush(Qt::black));
        painter.drawRect(10, 10, 100, 100);
    
       ui->someQlabel->setPixmap(pixmap);
    }
    

Log in to reply
 

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