Displaying a QLabel on a second screen



  • Hello,

    I have a weird problem while trying to display in fullscreen a QLabel on a second monitor.

    I have a class CustomLabel which inherits from QLabel, where I just redefined mousePressEvent and keyPressEvent, just to set it to not visible when one of these two events occurs. This CustomLabel is initially not visible and there is two situations where I want to display it in fullscreen:

    • When a second screen is detected, this CustomLabel should become visible, and be displayed in fullscreen on this second screen.

    • When there is one screen, the user can click on a button to set the CustomLabel to fullscreen on this screen.
      I have no problem when I don't mix these two things.

    However, let's say I launch the application with one screen and press the fullscreen button, the CustomLabel become visisble and fullscreen on my screen, normal. I then close it (set it not visible actually) by clicking anywhere on the screen. But then, if I plug a new screen, the CustomLabel may still appear on my first screen (usually, I plug the new screen once, it's working, I unplug it, the QLabel disappears, and when I replug the screen, the QLabel is displayed on my first screen...)

    This bug doesn't seem to be very deterministic, but can't be sure.... So I guess I'm missing something... But what?

    Here is the code I use (just to reproduce the problem):

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <iostream>
    #include <QDesktopWidget>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    _label = new CustomLabel();
    _label->setVisible(false);
    _label->setMinimumSize(200, 200);
    
    QPushButton *button = new QPushButton(tr("fullscreen"));
    setCentralWidget(button);
    
    QDesktopWidget *desktop = QApplication::desktop();
    connect(desktop, SIGNAL(screenCountChanged(int)), this, SLOT(screenCountChanged(int)));
    connect(button, SIGNAL(clicked()), this, SLOT(fullScreen()));
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::screenCountChanged(int count) {
    if (count == 1) {
    _label->setVisible(false);
    } else if (count == 2){
    QDesktopWidget *desktop = QApplication::desktop();
    QRect screenres = desktop->screenGeometry(1);
    _label->move(QPoint(screenres.x(), screenres.y()));
    _label->showFullScreen();
    }

    }

    void MainWindow::fullScreen() {
    if (!_label->isVisible()) {
    _label->move(0, 0);
    _label->showFullScreen();
    }
    }@

    and customlabel.cpp (not much in this one)
    @#include <QKeyEvent>

    #include "customlabel.h"

    CustomLabel::CustomLabel(QWidget * parent): QLabel(parent)
    {
    }

    CustomLabel::~CustomLabel()
    {
    }

    void CustomLabel::mousePressEvent(QMouseEvent * event)
    {
    Q_UNUSED(event);
    setVisible(false);
    }

    void CustomLabel::keyPressEvent(QKeyEvent *event) {
    if (event->key() == Qt::Key_Escape) {
    setVisible(false);
    }
    }@

    Thanks for your help :)


  • Lifetime Qt Champion

    Hi,

    Why not check the screen count in fullScreen ? Then you can place the label accordingly



  • Thanks for your message,
    For this method, I assume I just use it when I only have one screen.
    The problem can also be reproduced without using this button, if I launch the application, then plug a new monitor, the QLabel will be displayed in fullscreen on this second monitor.
    However, if I unplug it and then replug it, the QLabel is displayed on my first screen...


  • Lifetime Qt Champion

    Can you compare the geometries of all screens before and after you plug/replug your second screen ?



  • Okay, well I have no problem of monitor geometries, but I found this:
    I changed this method:
    @void MainWindow::screenCountChanged(int count) {
    if (count == 1) {
    _label->setVisible(false);
    } else if (count == 2){
    QDesktopWidget *desktop = QApplication::desktop();
    QRect screenres = desktop->screenGeometry(1);
    std::cerr << "second screen resolution: " << screenres.width() << " x " << screenres.height() << std::endl;
    _label->move(QPoint(screenres.x(), screenres.y()));
    std::cerr << "_label moved to: " << screenres.x() << " x " << screenres.y() << std::endl;
    std::cerr << "_label size before being displayed" << _label->width() << " x " << _label->height() << std::endl;
    _label->showFullScreen();
    std::cerr << "_label size after being displayed" << _label->width() << " x " << _label->height() << std::endl;
    }
    }@

    And the output:
    When I first plug the second monitor:
    second screen resolution: 1920 x 1080
    _label moved to: 1366 x 768
    _label size before being displayed640 x 480
    _label size after being displayed1920 x 1080
    (Everything is okay)

    When I unplug it and plug it back:
    second screen resolution: 1920 x 1080
    _label moved to: 1366 x 768
    _label size before being displayed1366 x 768
    _label size after being displayed1366 x 768

    My second monitor has a resolution of 19201080, while my main monitor has 1366768...


  • Lifetime Qt Champion

    Can you show each screenres values ?


Log in to reply
 

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