Resize size of Main Window depending on Hardware in Qt? - c++

I have developed my Application with most of the Widgets in Qt Creator's Designer module.
I have hard coded the sizes of my widgets depending on how they appeared on my laptop. Yesterday when I ran the application on my Desktop (lower screen resolution than laptop) some of the Main Window widgets were not visible & on startup there was horizontal scrollbar & I had to scroll to see those widgets; which is not the case on my laptop.
I have 2 problems:
Is it possible to resize all of my widgets (even those which are added run time by code) by using some resize factor? Maybe at startup I will get the screen resolution of Hardware in which application is running & then create a ratio of that with resolution of my laptop. Is it possible to multiply this factor to all widgets without adding code for each widget?
How do I get the resolution of the Screen in which my Application is running?
PS: I would like to know the defacto method of solving this problem...
Thank You.

You could try a function like this:
resize(theDesktop->screenGeometry().width()*PERCENTAGE_OF_MONITOR_WIDTH_FOR_SCREEN, theDesktop->screenGeometry().height()*PERCENTAGE_OF_MONITOR_HEIGHT_FOR_SCREEN);
Resize is a member function of the QWidget class and the PERCENTAGE_OF_MONITOR variables would be whatever percentage of the monitor you want your application to take up.
theDesktop is of the type QDesktopWidget.

You should use Layouts to manage the size policy of your widgets.
Qt layouts automatically position and resize widgets when the amount of space
available for them changes, ensuring that they are consistently
arranged and that the user interface as a whole remains usable."
You could also check this question for more information regarding layout machanisms in Qt.
Qt website has got excelent documentation on the subject. You can start here for more information on working with layouts in Qt Designer.

Related

How to align the button in Gtkmm?

I am using Ubuntu OS.
I am new to GTKMM GUI building application.
I am following the tutorials which they have on their websites.
https://developer.gnome.org/gtkmm-tutorial/stable/sec-helloworld.html.en
I can resize the window with helloworld.set_default_size(500,500);
but I cannot move the position of button("hello world").
It is always aligned at center.
I tried with m_button.set_alignment(0.0,0.0) but nothing seems to be working.
Why it is that much difficult to move button to any position.?
kindly suggest some solution if available.
If you want to place a widget like a button, you have to use some container widget like Gtk::Boxor Gtk::Grid. Inside such container widgets you can place you child widget as you like, but typically not directly per point position.
The container widgets use some internal logic to order and resize the contained widgets.
From the documentation:
Many GUI toolkits require you to precisely place widgets in a window, using absolute positioning, often using a visual editor. This leads to several problems: ...
gtkmm uses the packing system to solve these problems.
You can find the full documentation here:
Multi-item widgets
I did not know if there is a container widget which allows direct positioning by giving a point position. If someone has an additional hint, let us know!
Sometimes automatic is better than manual handling, but sometimes it is not :-)

Strange delayed painting with Qt5 on Windows

I'm using Qt5 in Windows. I just created a simple little widgets project in Qt Creator. I have a QMainWindow with a text edit widget inside. I enabled vertical layout, so the text edit consumes the full size of the inside of the main window (which is what I want, I'm trying to create a small notepad app).
As I drag the bottom right corner of the main window during the preview (I click the green triangle in the bottom left) I'm seeing a slight delay in the child widget's resizing. It doesn't exactly resize with the parent window on the same render frame (it seems like it is 1-2 render frames behind).
I remember years ago dealing with render lag like this in old school Win32 API. I remember I had to do double-buffered rendering into an offscreen bitmap to fix it (or something along those lines; been a long time).
Is there a way to fix this "render lag" in Qt? I can only imagine this is specific to Windows and might not impact other platforms, although I have not tested. If I need to provide more information let me know.
It is likely a Windows problem, not Qt's. The more GUI-heavy your window is the more noticeable it is.
I investigated the same issue a while ago. We had a rather GUI-heavy window, with several widgets displaying 2D and 3D views of data. Resizing the window using lower-right corner resulted in a resize-redraw horror. Unfortunately it looks like the problem is not Qt related but rather the way that Windows handles redrawing a resized window. I was able to notice the problem even in the file explorer on Windows 7. Qt is indeed using double buffering by default (as mentioned in the comment by #Bim). I also tried explicitly triggering Qt's repaint events. This helped a little, but did not solve the problem. So after many efforts we just decided to live with it.

without much change set MFC application to maximum resolution

I have an MFC application which is used frequently and works well, it has become an important part of client work and downtime is not at all tolerable.
Problem is that there is change in hardware and old monitors are replaced by LCD monitors so MFC application which is not fitting into size of all monitors, is there a way where in I can simply change MFC settings and recompile without much code change as that will trigger a lengthy test-fix-test cycle.
I would be happy to use a third party tools which would act as an container to this MFC application needing fixed resolution and give me scroll-bars like virtual monitor
Thanks
Two ideas without code change:
1) Just set the resolution of the desktop to another value, such that it's the same as on older monitors.
2) Change the font size in the dialog resources. This will change the size of the whole dialog.
With code change:
1) Use CDC::SetWorldTransform(const XFORM& rXform) to scale the CDC before you paint onto the CDC.
2) Use CDC::SetViewportExt(..), enable the scrollbars in CreateWindow(..), and handle the scroll events by using CDC::SetViewportOrg(..) to move the content of the window.

Making Qt Widgets Stretch and Scale w/ Main Window

I'm wondering if there is a way to make Qt widgets within a QMainWindow widget stretch and scale as the size of the main window is stretched and shrunk. Basically, I'd like the entire window and it's contents to have the same appearance except for size:
At the only way I can think to accomplish this is to get the size of the main window, do the math for every single widget, and reset their sizes any time the main window size is changed, but I'm thinking there's an easier way.
I like this video tutorial on youtube. I'll help you create a layout using QLayout classes. If you use QtCreator/Designer, you can also take a look at the auto-generated ui_MainWindow.XXX file that will clue you in on how it uses some of the UI classes.
As always, QtCreator/Designer has a bunch of hints and tips so you should be able to dig up from the documentation that's embedded in the application.

Qt frames disappears after a while

I have done a Windows form application in Qt and I have some QPushButton, QGroupBox and so on and the application is working great.
But when i leave the application to the next day, i see that all frames around buttons, groupboxes and all widgets that have frames dissapears. But the application still working as it should.
Here i have edited a picture how it looks.
Application sets some stylesheets on some widgets in other dialogs, and sets some widget enabled on and off.
But is there anyone that has got same problems? And maybe have an explaining why it happens?
Perhaps, your program leak GDI Objects.
A windows application can only use 9999 GDI Objects.
when your application leak too many GDI Objects, your application will looks look as your picture.
You can press Ctrl+Shift+Esc to see how many GDI Objects your application used.