Auto resizing qt application. (main window) - c++

I've recently started development in qt and so far it's going good. Now my question is : can I auto re-size qt application as per my screen resolution ? Say, I 've a main window designed for 1366 x 768. And it works for one system. While porting it to another system which supports 1280 x 800 or 800 x 600, is there an easy way to do it ? without changing UI design in creator ?
I appreciate any help regarding this.
Thanks in advance.

Try do this (for example put it in the mainWindow's constructor)
this->showMaximized();
Anyways you can simply get resolution of current screen with this code:
qDebug() << QApplication::desktop()->size();
And QWidget have resize() method. QMainWindow inherits QWidget, so you can use resize() in your mainWindow.

Qt (and many other UI toolkit) solve auto resize problem with a layout manager, it depend on how do you design your UI to choose a layout or mix them. See http://qt-project.org/doc/qt-4.8/layout.html

Related

Qt 5.3 full screen issue

I have a Qt application which runs on Ubuntu 12.04 Server with KDE plasma desktop. If I compile with Qt 4.8 full screen works as expected. With Qt 5.3, the window is getting bigger than the available resolution. If I set the resolution using the following code, it works.
QSize sz(QApplication::desktop()->size());
main_window->setFixedSize(sz.width() + 1, sz.height() + 1);
main_window->showFullScreen();
Is this the proper way to solve this issue?
Thanks in advance.
Qt is quite flexible in application sizing and provides you with a lots of informations (and options).
For what concerns QApplication you can use QDesktopWidget.
QDesktopWidget * screen = QApplication::desktop();
screen->availableGeometry();
As stated in the docs about availableGeometry:
Returns the available geometry of the screen with index screen. What
is available will be subrect of screenGeometry() based on what the
platform decides is available (for example excludes the dock and menu
bar on Mac OS X, or the task bar on Windows). The default screen is
used if screen is -1.
Read the section "Use of the Primary Screen" in QDesktopWidget docs for details about the "default screen" and the general handling of multiple screens. Using these methods you will have full control over the way your application is laid out, even with multiple screens available.
For what concerns QGuiApplication you can use QScreen:
QScreen * screen = QGuiApplication::primaryScreen();
screen->availableGeometry();
Finally, in QML it is possible (and advisable) to use Screen object which provides Screen.desktopAvailableWidth and Screen.desktopAvailableHeight which ensure proper resizing with different versions of Android/iOS.

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.

Resize size of Main Window depending on Hardware in Qt?

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.

QWidget / QWindow title bar: custom look, native feel (similar to Chrome)

I know how to make a QWidget (in Qt4 or Qt5) / QWindow (since Qt5) borderless, draw a custom title bar and manually implement mouse dragging to move the window on the screen by simply tracking the mouse position with some mouseMoveEvent and updating the window position.
However, this movement behaves different than the native one as implemented by the window manager. For example, moving the window near the screen's border can be interpreted as "fullscreen" or "split screen"; or windows snap to each other's borders, depending on the system / window manager. These things don't work if you implement the window movement like above.
Google Chrome / Chromium is only one example for an application which implements a custom window title bar and border, while still adapting to the native behavior of the window manager. I'm wondering whether Chrome implements these by itself (and detects the window manager and its configuration) or if there is some functionality in most window managers (clearly, this is still highly platform-dependent) for telling "start native window movement" and "stop native window movement" or similar.
Is something like that possible in Qt? If not, maybe using some other libraries like Qxt?
FYI: I'm mainly targeting Windows and Linux, where I see the difficulty that the user can have any window manager installed.
Based on attempting the same with Qt4 recently, I fear the answer is, you need to tune this per-platform / per-window-manager. I expect patches to QWindow to improve the behaviour in this area would be accepted, but I'm not aware of any standard hook to tell the OS/window-manager what you're trying to achieve.
Equally Qt should be not be 'getting in the way' of solving this, it's simply an area where it can't do anything to help you in a generic way.

How to set QWidget fullscreen (but "real" fullscreen, change resolution, set modal to whole system)?

I'm trying to make a game using Qt, cause it is so awesome ;) and you have all the stuff you need for free. The only problem is in changing system resolution and setting QWidget (or QGLWidget) "real" fullscreen.
Have any one of you managed to do something like this? How was the portability of such approach? I'd like to deploy my app on all desktop systems.
Maybe use SDL or something like SMFL to make it fullscreen?
Pls, share your hacks!
Cheers.
This...
my_widget->setWindowState(Qt::WindowFullScreen);
... brings your widget to a full screen resolution. Isn't that what you need?
Edit:
Alternatively you can call the slot showFullScreen.
Edit 2:
WIN API
EnumDisplaySettings
ChangeDisplaySettings
Detailed information
X11
I'm not familiar with this, you could ask a new question regarding how to change the screen resolution here
Mac
Same as X11
First you have to set corresponding window flags to make your widget modal, and also get rid of the window manager frame so it will be true fullscreen
widget->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
and after that call widget->showFullScreen(); as Exa said.
Fullscreen:
topwidget->setWindowState(topwidget->windowState() ^ Qt::Window);