Detecting the removal of a screen and modifying the app accordingly.



  • So far I have an application that counts how many screens are available.

    QDesktopWidget *widget = QApplication::desktop();
    qDebug() << 'Number of screens:' << widget->screenCount();
    
    if (widget->screenCount() > 1) {
        leftScreen.show();
        rightScreen.show();
        singleScreen.close();
    } else if (widget->screenCount() == 1){
        leftScreen.close();
        rightScreen.close();
        singleScreen.show();
    }
    
    

    This will debug how many screens to console and if more than 1 the 2 screens are shown and if only one then the one screen.

    If i remove a screen though nothing else changes but if i close the application and then open it again the single ui will display.

    QGuiApplication has a member called screenRemoved but im struggling to figure out how to incorporate that into my application.

    Later i will tell each screen to go to its set place but that is pretty straight forward.


  • Moderators

    You can listen to screenCountChanged() signal to know when the count changes.



  • Thanks although what arguments do i put in? The docs don't really mention much.

    Its also saying the same as before so I.m definitely going to have to use the QGuiApplication but how that i cant figure out... Yet


  • Lifetime Qt Champion

    @MrCrackPotBuilder
    Hi
    Its a signal so you would connect it up to your own slot and Qt will then call
    this slot when screen count changes.



  • ah then im going to need to make a new header and source file becuase its all being done in main.cpp

    im taking a wild guess but inside my main i could do

    QQuiApplicationt::connect(&screenRemoved, &displayChange::displayChange);
    
    

  • Moderators

    @MrCrackPotBuilder said in Detecting the removal of a screen and modifying the app accordingly.:

    Its also saying the same as before so I.m definitely going to have to use the QGuiApplication but how that i cant figure out... Yet

    you #include <QGuiApplication> in your class that than allows you access to the (unique) application instance pointer qApp than you can use that to update your Layout.

    QObject::connect(qApp, &QGuiApplication::screenCountChanged,  this,      &myClass::screenCountSlot);
    


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