Solved Resolution independent paintEvent
-
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?
-
@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. -
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. -
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.
-
@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. -
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 beint cols=10; //columns number
should be something like
int cols=width()/10; //columnsor similar logic.
-
@kshegunov Thank you! That's exactly what i wanted