Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Resize QPainter
Qt 6.12 LTS is out! See what's new in the release blog

Resize QPainter

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

    Hi, i have a question. Suppose i have a MainWindow where, through paintEvent and QPainter, I draw a rectangle.
    I have noticed that if i resize the window, usinge the mouse, the rectangle doesn't change in size, it stays the same.
    How can i make the rectangle resize accordingly to the window?

    M 1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      if i resize the window, usinge the mouse, the rectangle doesn't change in size, it stays the same

      Why would it change on its own? You're drawing a rectangle by specifying the size in pixels, so you decide how big it is.

      How can i make the rectangle resize accordingly to the window?

      You can't make that rectangle resize. It's just drawn pixels. If you want a bigger rectangle you just draw a bigger rectangle. Widgets have a size(), width() and height() methods that give you their dimensions. If you want the rectangle to be, for example, half the width of the widget just pass width()/2 as the width of the rectangle when you draw it.

      1 Reply Last reply
      2
      • D Davide00

        Hi, i have a question. Suppose i have a MainWindow where, through paintEvent and QPainter, I draw a rectangle.
        I have noticed that if i resize the window, usinge the mouse, the rectangle doesn't change in size, it stays the same.
        How can i make the rectangle resize accordingly to the window?

        M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        @Davide00

        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing,true);
         
        QRect rec=contentsRect(); // takes margins into account if you set ones
        painter.drawRect(rec);
        
        1 Reply Last reply
        1

        • Login

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