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

The way to display a window using "virtualGeometry" of Qscreen in Qt Widget



  • Hello. I have three screens. And I try to display a window using virtualGeometry of QScreen in Qt Widget.

    But I can not correctly display a window .Because a widnow is displayed on only a primary screen.

    Do you know how to solve it ?

    For example, in case of QML , If I use the following code , I can display a window on a proper screen.

    /////////////////////////////////////////////////
    Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    screen:Qt.application.screens[0] //screen number
    x:screen.virtualX
    y:screen.virtualY
    }
    /////////////////////////////////////////////////

    In accordance with the above way, I tried to display a window using "virtualGeometry" of Qscreen in Qt Widget.

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

    //Set Top-Level Window
    QWidget::setWindowFlag(Qt::FramelessWindowHint);
    QWidget::setWindowFlag(Qt::WindowStaysOnTopHint);
    
    //Set window Color
    QPalette Pal(palette());
    Pal.setColor(QPalette::Background, "#444343");
    this->setAutoFillBackground(true);
    this->setPalette(Pal);
    
    QScreen *screen = QGuiApplication::screens()[1]; //→Screen numbers 0,1,2
    QRect desktop = screen->virtualGeometry();
    this->setGeometry(desktop);
    

    // this->move(screen->geometry().x(), screen->geometry().y());
    this->move(screen->virtualGeometry().x(), screen->virtualGeometry().y());

    qDebug() << "screen->availableVirtualGeometry().x():" << screen->availableVirtualGeometry().x();
    this->resize(800, 400);
    ui->frame->setStyleSheet("background-color:green;");
    this->show();
    

    }
    ///////////////////////////////////

    If I use geometry of QScreen , I can display it .


  • Lifetime Qt Champion

    Hi
    virtualGeometry normally returns the extended desktop
    like 0,0-5770,1080
    (for 3 screens)

    So i wonder if you want to use screen->availableGeometry() instead ?
    or maybe screen->availableSize()

    or you can try like ( include #include <QWindow> )
    which have worked fine for me on windows.

    QWidget * widget = new QWidget();
    widget->show();
    widget->windowHandle()->setScreen(qApp->screens()[1]);
    widget->showFullScreen();



  • @mrjj
    Thank you for replying . I may misunderstand virtualGeometry().

    I guess a same way to get x or y coordinate using screen and screen.virtualX from screen in QML , is the way to use screen->geometry().x() in Widget.

    Is it correct ?? Because I thought the virtualGeometry() in Widget is same as the screen,virtualX in QML...


Log in to reply