How can I find the screen/desktop size in Qt, so I can display a desktop notification?

Qt

Qt Problem Overview


I want to code a notification window at the bottom right corner of the desktop, so it works like Skype notifications when someone comes online. Can someone help me get the screen size of the desktop, so I can place my notification window at the bottom right corner using my Qt Application?

Qt Solutions


Solution 1 - Qt

You can use the QDesktopWidget

 QRect rec = QApplication::desktop()->screenGeometry();
 height = rec.height();
 width = rec.width();

Solution 2 - Qt

QScreen class (since Qt 5.0) provide information about size of screen (logical and physical), orientation and signals for changes.

QScreen *screen = QGuiApplication::primaryScreen();
QRect  screenGeometry = screen->geometry();
int height = screenGeometry.height();
int width = screenGeometry.width();

upd:

  • for multiscreen configuration please use QList <QScreen*> screens = QGuiApplication::screens();
  • for orientation (including multiscreen) please use screen->orientation() and (since Qt 5.2)screen->nativeOrientation()

Solution 3 - Qt

You haven't stated which platform you're developing for, but some platforms have API for showing notifications. It's best to use those, when available, because the bottom right if the screen may not be appropriate. You can fall back on krammer's answer.

In GNOME, for example, the user can make notifications appear on a particular screen, or elect to not show them at all, until they click on a system tray icon. The libnotify API gives the developer access to this feature.

Speaking of system trays, you may want to consider using a QSystemTrayIcon instead. It would make UI a bit different, but has a platform-independent Qt API, which works for GNOME, KDE, MacOS, Windows and probably other platforms. It also has a showMessage method for showing messages in a manner native for the platform, such as in a balloon with a cute popping sound for some versions of Windows.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSanath ReddyView Question on Stackoverflow
Solution 1 - QtkrammerView Answer on Stackoverflow
Solution 2 - QtAndrey Azazello YaromenokView Answer on Stackoverflow
Solution 3 - QtMichael ScheperView Answer on Stackoverflow