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. Resolution independent paintEvent
Forum Updated to NodeBB v4.3 + New Features

Resolution independent paintEvent

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 486 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.
  • P Offline
    P Offline
    Peter_Dev
    wrote on last edited by aha_1980
    #1

    Hi everyone, i made a grid in my window with paintEvent and have a qlabel in one of it's cells. Problem is that when i open my app on screen with lower resolution, my grid becomes bigger than screen itself. How can i make my grid resizeable, so my app will look good in any computer screen resolution?

    kshegunovK 1 Reply Last reply
    0
    • P Peter_Dev
      void Game::paintEvent(QPaintEvent *e)
      {
          QPainter painter(this);
      
          QPen framepen(Qt::black);
          framepen.setWidth(2);
      
          painter.setPen(framepen);
          int startPoint=20; //indent
          int squaresNum=100;
          int side=80;
          int cols=10; //columns number
          int y=startPoint;
          int x=startPoint;
          for(int i=0; i<squaresNum; i++)
          {
              if(((i%cols) == 0) && (i>0))
              {
                  x=startPoint;
                  y+=side;
              }
              QRect rec(x+(i%cols)*side,y,side,side);
              painter.drawRect(rec);
          }
      }
      

      @kshegunov Unfortunately, i cant make a picture because i don't have that computer with lower resolution right now. I understand that i can change size of single square's side according to resolution. I just was wondering if there was any way such as adding my paintEvent to layout, so resizing could be done automatically.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #4

      @Peter_Dev said in Resolution independent paintEvent:

      I just was wondering if there was any way such as adding my paintEvent to layout, so resizing could be done automatically.

      There's no layout for the painter, however you can use QPainter::scale to rescale what you paint based on the size you need. Try it out and see if it works for your case. I'm not sure (I don't remember) but I think the scale is going to affect the pen width as well, so experiment and see.

      Read and abide by the Qt Code of Conduct

      P 1 Reply Last reply
      1
      • P Peter_Dev

        Hi everyone, i made a grid in my window with paintEvent and have a qlabel in one of it's cells. Problem is that when i open my app on screen with lower resolution, my grid becomes bigger than screen itself. How can i make my grid resizeable, so my app will look good in any computer screen resolution?

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #2

        Provide some code of what you do (a picture illustrating the issue would be appreciated, as well).
        As a note, it's your responsibility to draw the contents with appropriate sizes, so you should get the size from the widget and then calculate what and where to paint.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        3
        • P Offline
          P Offline
          Peter_Dev
          wrote on last edited by Peter_Dev
          #3
          void Game::paintEvent(QPaintEvent *e)
          {
              QPainter painter(this);
          
              QPen framepen(Qt::black);
              framepen.setWidth(2);
          
              painter.setPen(framepen);
              int startPoint=20; //indent
              int squaresNum=100;
              int side=80;
              int cols=10; //columns number
              int y=startPoint;
              int x=startPoint;
              for(int i=0; i<squaresNum; i++)
              {
                  if(((i%cols) == 0) && (i>0))
                  {
                      x=startPoint;
                      y+=side;
                  }
                  QRect rec(x+(i%cols)*side,y,side,side);
                  painter.drawRect(rec);
              }
          }
          

          @kshegunov Unfortunately, i cant make a picture because i don't have that computer with lower resolution right now. I understand that i can change size of single square's side according to resolution. I just was wondering if there was any way such as adding my paintEvent to layout, so resizing could be done automatically.

          kshegunovK 1 Reply Last reply
          0
          • P Peter_Dev
            void Game::paintEvent(QPaintEvent *e)
            {
                QPainter painter(this);
            
                QPen framepen(Qt::black);
                framepen.setWidth(2);
            
                painter.setPen(framepen);
                int startPoint=20; //indent
                int squaresNum=100;
                int side=80;
                int cols=10; //columns number
                int y=startPoint;
                int x=startPoint;
                for(int i=0; i<squaresNum; i++)
                {
                    if(((i%cols) == 0) && (i>0))
                    {
                        x=startPoint;
                        y+=side;
                    }
                    QRect rec(x+(i%cols)*side,y,side,side);
                    painter.drawRect(rec);
                }
            }
            

            @kshegunov Unfortunately, i cant make a picture because i don't have that computer with lower resolution right now. I understand that i can change size of single square's side according to resolution. I just was wondering if there was any way such as adding my paintEvent to layout, so resizing could be done automatically.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #4

            @Peter_Dev said in Resolution independent paintEvent:

            I just was wondering if there was any way such as adding my paintEvent to layout, so resizing could be done automatically.

            There's no layout for the painter, however you can use QPainter::scale to rescale what you paint based on the size you need. Try it out and see if it works for your case. I'm not sure (I don't remember) but I think the scale is going to affect the pen width as well, so experiment and see.

            Read and abide by the Qt Code of Conduct

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

              Hi
              If game is a widget and placed in a layout then its size will change when mainwindow is resized.
              It then means you must use it geometry to calculate how many cols you can have/ how big each col can be

              int cols=10; //columns number
              should be something like
              int cols=width()/10; //columns

              or similar logic.

              1 Reply Last reply
              0
              • kshegunovK kshegunov

                @Peter_Dev said in Resolution independent paintEvent:

                I just was wondering if there was any way such as adding my paintEvent to layout, so resizing could be done automatically.

                There's no layout for the painter, however you can use QPainter::scale to rescale what you paint based on the size you need. Try it out and see if it works for your case. I'm not sure (I don't remember) but I think the scale is going to affect the pen width as well, so experiment and see.

                P Offline
                P Offline
                Peter_Dev
                wrote on last edited by
                #6

                @kshegunov Thank you! That's exactly what i wanted

                1 Reply Last reply
                0

                • Login

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