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

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?


Solution 1:

You can use the QDesktopWidget

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

Solution 2:

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:

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.