How do I get the QScreen my widget is on? QApplication::desktop()->screen() returns a QWidget* and qobject_cast<QScreen*> returns NULL



  • Hi,

    I'm trying to use grabWindow() from a widget that could be on a screen other than the primaryScreen. I can find the index of the screen the widget is on, but casting that widget to a QScreen fails (returns NULL.) Also, inherits("QScreen") is false...the widget is a QDesktopScreenWidget, something for which there is very little documentation that I can find.

      // find out which screen we're on
      int thisScreen = QApplication::desktop()->screenNumber(this);
      QWidget* thisQWidget = QApplication::desktop()->screen(thisScreen);
      QScreen* thisQScreen = qobject_cast<QScreen*>(thisQWidget);  // returns NULL
    

    There is a list of QScreens returned by QGuiApplication::screens() but no indication whether that list is in the same order as the index returned by QApplication::desktop()->screenNumber().

    So how do I get the QScreen that a given widget is on?

    thanks.



  • @GeorgePTVS said:

    QScreen

    I am not sure about Qt 5, but in Qt 4 QScreen is a base class for screen drivers in Qt for Embedded Linux.
    It means it does not exist on all platforms and more of that it is not even QObject derived class, so qobject_cast can't return you anything but NULL.

    About how... I dod not think I can give you an answer.

    I do not think that you can find what is "primary" screen for system and at least there is contradiction in documentation.
    Even though in one place it states that primaryScreen
    " holds the index of the screen that is configured to be the primary screen on the system"
    in another it says
    "For an application, the screen where the main widget resides is the primary screen."

    I assume primaryScreen changes its value depending on where your main window is moved.
    All you can get is index of screen main widget part resides on and number of screens available.
    I do not think Qt can tell you which screen is considered primary.



  • Thanks for the input. For what it's worth, on Windows 7 (embedded, but I imagine it would operate similarly on full Windows), I can use the index obtained from QApplication::desktop()->screenNumber() to find the QScreen from the list given by QGuiApplication::screens().

    It works in my limited use, but unfortunately there's no guarantee that it will work into the future or across platforms.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.