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

    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);
    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);
        QPainter painter(&pixmap);
        painter.drawRect(10, 10, 100, 100);

Log in to reply

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