Qt World Summit: Submit your Presentation

dual screen Qml, QWidget application

  • hi everyone,

    requirement is to run multi-display application using qt4.8.6 on ubuntu14.04.

    on my primary display i am using qml,
    on secondary diaplay i am running qwidget (by adjusting its x and y points).

    i need help on two doubts i am having right now.

    1. is my approach running secondary display is correct or not? can i evoke one more qml file from my main application. any more correct way to do same?
        QmlApplicationViewer viewer;
    1. currently when i pressed "Alt+F4" or "crash" happens, my main application and primary qml will close properly but secondary display keep running.

    I figured out that "alt+f4" affect where i am having current focus. it surely close qml or qwidget only ONE at time.

    kindly help me to write correct demo code. :)

    /// secondaryDisplay.h /// 
    #include <QtCore>
    #include <QDebug>
    #include <QLabel>
    #include <QGraphicsView>
    #include <QGraphicsPixmapItem>
    class secondaryDisplay: public QObject
    //    QWidget * displayWidg//et = new QWidget();
        QLabel * tariffLabel;
        QString tariffStringAmount;
        QLabel *labelTariffAmount;// =  new QLabel(displayWidget);//("Total Tariff :", displayWidget);
        QString greetingMsg;
    public :
        Q_INVOKABLE void showSecondaryDisplay();
    public Q_SLOTS:
        Q_INVOKABLE void setDisplayTariffAmount(QString amount);
        Q_INVOKABLE void setGreetingMsg();
    /// secondarydisplay.cpp //
    #include "secondarydisplay.h"
    void secondaryDisplay::setDisplayTariffAmount(QString amount)
        qDebug() << "secondaryDisplay :: slot called..";
    void secondaryDisplay::setGreetingMsg()
        this->greetingMsg="Welcome\nTo Secure Park.";
        qDebug() << "greetingMsg :  " << greetingMsg;
    void secondaryDisplay::showSecondaryDisplay()
        QWidget *displayWidget = new QWidget();
        displayWidget->move(QPoint(1366, 0)); /// x cord. shift for second display
    //    displayWidget->resize(400,250);
    /// image label
        QLabel *imageLabel = new QLabel(displayWidget);
        imageLabel->setGeometry(10, 10, 1000, 150);
     // set a scaled pixmap to a w x h window keeping its aspect ratio
        QPixmap logo("/home/user/bundle/secure.jpg");
        int w = imageLabel->width();
        int h = imageLabel->height();
    ///Greeting label
    //    QLabel *greetinLabel = new QLabel(displayWidget);
    //    greetinLabel->setText(this->greetingMsg);
    //    greetinLabel->setGeometry(10, 10, 1000, 150);
    //    greetinLabel->setStyleSheet("font: 40pt;");
    ///tariff label
        QLabel *labelTariffText =  new QLabel(displayWidget);//("Total Tariff :", displayWidget);
        labelTariffText->setText("TOTAL :");
    //    label.setText("welcome");
        labelTariffText->setGeometry(10, 585, 400, 150);
        labelTariffText->setStyleSheet("font: 70pt;");
    ///tariff amount label
        labelTariffAmount =  new QLabel(displayWidget);//("Total Tariff :", displayWidget);
    //    label.setText("welcome");
        labelTariffAmount->setGeometry(380, 580, 600, 200);
    //    labelTariffAmount->setStyleSheet("background-color:white;");
        labelTariffAmount->setStyleSheet("font: 80pt;");
    // main.cpp ///
    #include <QApplication>
    #include "qmlapplicationviewer.h"
    #include "secondarydisplay.h"
    #include <QSound>
    #include <QtMultimedia>
    #include <QAudio>
    Q_DECL_EXPORT int main(int argc, char *argv[])
        QScopedPointer<QApplication> app(createApplication(argc, argv));
        QmlApplicationViewer viewer;
        secondaryDisplay secondaryDisplayObj;
        return app->exec();

  • @Anas_Deshmukh said in dual screen Qml, QWidget application:

    currently when i pressed "Alt+F4" or "crash" happens, my main application and primary qml will close properly but secondary display keep running.

    I think that is not possible, as you open your secondary display with widget->show().
    So leaving the message loop in main application will close also your secondary display.

    I assume your application will not be terminating after Alt+F4.

  • Moderators


    You''ll have to get into your destructor functions, of the qml file and the QWidget, and call QApplication::quit manually.

    Component.onDestruction: {
         delete ui;

  • Lifetime Qt Champion

    @J-Hilk It should happen automatically unless the quitOnLastWindowClosed property has been changed.

  • Moderators

    Maybe I missread the question, but I thought the goal was it to close the whole application when either one one of the Windows (primary or secondary screen) is closed

    currently when i pressed "Alt+F4" or "crash" happens, my main application and primary qml will close properly but secondary display keep running.

    Target: alt+F4 -> close whole app

    You're right however, that my post doesn't quite target the situation.
    The Qt.Quit() should be in a Shortcut sequence and the widget should have an event filter listening for the alt+f4 key combo.

  • Hi everyone,

    i am sorry for late reply. At the moment i am using 'QApplication::destroyed()' signal to close my extra widget.
    some how its working for me, but i am still doubtful in terms of standard coding.

    thank you for your help. :)

    // main.cpp
    Q_DECL_EXPORT int main(int argc, char *argv[])
        QScopedPointer<QApplication> app(createApplication(argc, argv));
        secondaryDisplay secondaryDisplayObj ;//= new secondaryDisplay;
        viewer.rootContext()->setContextProperty("secondaryDisplayObj",&secondaryDisplayObj); /// qml acess
        secondaryDisplayObj.showSecondaryDisplay(); /// initiate my widget 
        QObject::connect(app.data(),SIGNAL(destroyed()), &secondaryDisplayObj, SLOT(deletSecondaryDisplay()));
    // deletSecondaryDisplay :: my customised slot

Log in to reply