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. How to do QPainter paint in the Widget?
Forum Updated to NodeBB v4.3 + New Features

How to do QPainter paint in the Widget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 3.4k 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.
  • Macive XiongM Offline
    Macive XiongM Offline
    Macive Xiong
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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);
      }
      
      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved