Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ScreenAdded signal does not work



  • Hello everyone,

    I found this topic that has not been answered yet: QGuiApplication::screenAdded signal never emitted

    My question is simpler: How to correctly use screenAdded signal? I do not receive such signal in my class.

    I am using QT 5.14.1 on windows (specifically on MSVC 2017) and the important code is as follows:

    The .h:

    class QtGuiMyClass : public QWidget
    {
    Q_OBJECT
    
    public:
    	QtGuiMyClass (QGuiApplication * qga, QWidget *parent = Q_NULLPTR);
    private:
    	QGuiApplication * m_qga;
    private slots:
    	
    	void screenChange(QScreen *screen);
    };
    

    The .cpp

    QtGuiMyClass ::QtGuiMyClass (QGuiApplication * qga,QWidget *parent)
    	: QWidget(parent)
    	, ui(new Ui::QtGuiMyClass )
    {
            ui->setupUi(this);
    	m_qga = qga;
            connect(m_qga, &QGuiApplication::screenAdded, this, &QtGuiMyClass ::screenChange);
    }
    void QtGuiMyClass ::screenChange(QScreen *screen) {
    	std::cout << "screen changed!" << std::endl; //<-This is never executed 
    }
    

    The main.cpp

            QApplication a(argc, argv);	
    	QtGuiMyClass w(&a);
    	w.show();
    	return a.exec();
    

    Thank you in advanced,



  • @Siset

    You need to add a screen to your system in order to emit this signal.

    https://doc.qt.io/qt-5/qguiapplication.html#screenAdded



  • @Pl45m4
    Thanks for the answer. Do you mean that I need to declare a new Screen? Then this signal is emitted? I understood that this signal would fire everytime a new screen is added to the system (connected via HDMI for example or turned on).
    Is that not the case?

    Thanks,



  • @Siset

    I was expecting that too. Isnt this working?
    Try to change the display settings on your OS while running your app (Screen modes: clone, extend, change primary screen, etc.)

    You could also try this example to check if Qt or your app knows about your second screen (when plugged-in or activated by OS' window manager)
    Screen settings example



  • @Pl45m4

    Thank you for your time.

    No, it does not fire when I change the display settings. But yes, once turned on all the screens()[1] functions work (except for the serial number,name,model...). The problem is that the signal seems to never reach the screenChange (i do not even know if it is emitted in the first place).

    Any other suggestion?

    Thanks,



  • @Siset
    According to my test, QGuiApplication always keep a primary screen, and QGuiApplication::screens() keep 1 when there is not a screen plug to your machine; you must plug a screen to another HMI port, then screenAdded signal will be fired, QGuiApplication::screens() will be increase to 2.



  • This post is deleted!


  • @soyoo
    Thank you very much!

    I've just found the problem. If I turn off/on the screen it does not detect anything, in fact, with the screen switched off qApp->screens().size() is 2!! This happens in a TV, in my monitor screen this behaviour does not appear.

    Thanks,



  • Yup, as long as the underlying system thinks the number of screens is the same, the signal won't fire, even if the user might expect it to. A turned off monitor can still be detected. Config can be hard-coded instead of set to detect changes. A new monitor can be captured by the driver as part of an existing "virtual" screen that just got bigger, instead of an actual new screen.

    My personal computer has two screens, and when I forget to turn one on before I boot, it still detects it just fine so when I turn it on, that desktop is already there, the system doesn't have to blip and reconfigure like when I plug my laptop into a TV. It can be counterintuitive.


Log in to reply