Results 1 to 12 of 12

Thread: Dynamically loading and unloading 2 qml files from cpp

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamically loading and unloading 2 qml files from cpp

    If I understand your code correctly, your MainWindow and BaseWindow are both top-level windows (they have no "parent" - they are children of the desktop window). This makes them "sibling" windows, and so you may be able to use the QWidget::stackUnder() method. When you make BaseWindow visible, call MainWindow->stackUnder( BaseWindow ) and vice-versa. I don't know if it will work, but it is worth trying.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    gowreesh (2nd June 2020)

  3. #2
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically loading and unloading 2 qml files from cpp

    Thank you for the reply.

    I am using QQuickWindow and i am not using QWidget.
    I have created a custom window i.e "class CustomWindow : public QQuickWindow {...}"
    This CustomWindow only i have used in QML side. and sorry in my first thread i have mentioned ApplicationWindow
    So only I am not able to access or pass QQuickWindow to QWidget::stackUnder(QWidget *w).
    Requesting you to please let me know if there is any other solution.

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamically loading and unloading 2 qml files from cpp

    Sorry, I don't have any other ideas. I do not have much experience with QWindow and QML, so maybe someone else here can give some advice.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    gowreesh (3rd June 2020)

  6. #4
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically loading and unloading 2 qml files from cpp

    its OK and thank you very much for replying.
    If i don't find any solution i need to go back to that previous method of loading and unloading concept even if it consumes more memory since I don't have any other option.

    Could anyone please help me out for the issue I have mentioned above.?????

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamically loading and unloading 2 qml files from cpp

    Could anyone please help me out for the issue I have mentioned above.?????
    Maybe you should post your current code for loading the windows and switching between them. I am sure it is not the same as the code in your first post and might contain some clues about why it isn't working correctly.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. The following user says thank you to d_stranz for this useful post:

    gowreesh (4th June 2020)

  9. #6
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically loading and unloading 2 qml files from cpp

    Ya sure , I will put my latest code here.
    I have designed this below code like if the user launches the application for the first time , then the application will load Mainwindow.qml or basic.qml based on the flag "bSettingMainWindow" .
    Next time based on the switch button click from application will load another window (other than first one) and making the first launched qml window visible false.
    Once both of them launched once, then 2 windows will play with visibility true or false on click of switch button or signal is emitted.
    This below code is working perfectly fine except the window is not coming on top after making visible true. I have tried using raise() and activatewindow() even after that i am facing the same issue.
    Please find the code below


    Qt Code:
    1. //CustomWindow.h
    2. class CustomWindow : public QQuickWindow {
    3. //Custom QQuick window used in QML
    4. }
    5.  
    6.  
    7.  
    8. //** windowLoader.hpp **//
    9. class WindowLoader : public QObject{
    10. Q_OBJECT
    11. QQmlApplicationEngine loadQMlEngine;
    12.  
    13. public:
    14. explicit WindowLoader(QObject * parent = 0);
    15. void loadWindow();
    16.  
    17.  
    18. public slots:
    19. void loadMainWindow();
    20. void loadBasicWindow();
    21. void connectToMain(QObject *object = nullptr, const QUrl &url = QUrl(""));
    22. void connectToBasic(QObject *object = nullptr, const QUrl &url = QUrl(""));
    23.  
    24. private:
    25. };
    26.  
    27.  
    28.  
    29. //** windowLoader.cpp **//
    30. WindowLoader::WindowLoader(QObject *parent) : QObject(parent) {
    31.  
    32. }
    33.  
    34. //This function will be called only one time during launch
    35. void WindowLoader::loadWindow() {
    36. if(bSettingMainWindow){ //this will be from a internal flag, this check is only one time during launch
    37. connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToBasic(QObject *, const QUrl &)),Qt::QueuedConnection);
    38. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
    39. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/mainWindow.qml")));
    40. m_iMainWindowIndex = 0;
    41. m_iBasicWindowIndex = 1;
    42. } else {
    43. connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToMain(QObject *, const QUrl &)),Qt::QueuedConnection);
    44. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
    45. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/basic.qml")));
    46. m_iMainWindowIndex = 1;
    47. m_iBasicWindowIndex = 0;
    48. }
    49. }
    50.  
    51. void WindowLoader::connectToBasic(QObject *object, const QUrl &url) {
    52. if(object){
    53. connect(object, SIGNAL(switchToBasicSignal()), this, SLOT(loadBasicWindow()));
    54. }
    55. }
    56.  
    57. void WindowLoader::connectToMain(QObject *object, const QUrl &url) {
    58. if(object){
    59. connect(object, SIGNAL(switchToMainSignal()), this, SLOT(loadMainWindow()));
    60. }
    61. }
    62.  
    63. void WindowLoader::loadBasicWindow() {
    64.  
    65. if(loadQMlEngine.rootObjects().size() <= 2) {
    66. disconnect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToBasic(QObject *, const QUrl &)));
    67. }
    68.  
    69. if(loadQMlEngine.rootObjects().size() < 2) {
    70. connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToMain(QObject *, const QUrl &)),Qt::QueuedConnection);
    71. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
    72. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/basic.qml")));
    73. loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",false);
    74. } else {
    75. //QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iBasicWindowIndex],"show");
    76. //QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iMainWindowIndex],"hide");
    77. loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",true);
    78. loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",false);
    79. }
    80. }
    81.  
    82. void WindowLoader::loadMainWindow() {
    83.  
    84. if(loadQMlEngine.rootObjects().size() <= 2) {
    85. disconnect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToMain(QObject *, const QUrl &)));
    86. }
    87.  
    88. if(loadQMlEngine.rootObjects().size() < 2) {
    89. connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToBasic(QObject *, const QUrl &)),Qt::QueuedConnection);
    90. loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
    91. loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/mainWindow.qml")));
    92. loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",false);
    93. } else {
    94. //QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iBasicWindowIndex],"hide");
    95. //QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iMainWindowIndex],"show");
    96. loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",false);
    97. loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",true);
    98. }
    99. }
    100.  
    101.  
    102. //** main.cpp **//
    103. int main( int argc, char *argv[] ) {
    104. QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    105. QApplication app(argc, argv);
    106. qmlRegisterType<CustomWindow>("CNQml", 1, 0, "CustomWindow");
    107. WindowLoader root;
    108. root.loadWindow();
    109. return app.exec();
    110. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. // ** mainWindow.qml **//
    2. CustomWindow {
    3. visible: true
    4. width: 1200
    5. height: 800
    6. title: qsTr("MainWindow")
    7.  
    8. signal switchToBasicSignal()
    9.  
    10. Rectangle {
    11. anchors.fill: parent
    12. MouseArea{
    13. anchors.fill: parent
    14. onClicked: {
    15. switchToBasicSignal()
    16. }
    17. }
    18. }
    19. }
    20.  
    21. //** basic.qml **//
    22. CustomWindow {
    23. visible: true
    24. width: 640
    25. height: 480
    26. title: qsTr("basicwindow")
    27.  
    28. signal switchToMainSignal()
    29.  
    30. Rectangle {
    31. anchors.fill: parent
    32. MouseArea{
    33. anchors.fill: parent
    34. onClicked: {
    35. switchToMainSignal()
    36. }
    37. }
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Active Qt: crash while unloading Qt Core DLL
    By ivigasin in forum Qt Programming
    Replies: 4
    Last Post: 2nd April 2015, 11:58
  2. Dynamically loading libraries
    By januszmk in forum Newbie
    Replies: 5
    Last Post: 4th February 2013, 16:44
  3. Remove library after unloading
    By mvw in forum Qt Programming
    Replies: 1
    Last Post: 23rd April 2010, 10:06
  4. Dynamically Loading UI With Unspecified Slots
    By spraff in forum Qt Programming
    Replies: 2
    Last Post: 11th April 2010, 10:46
  5. Dynamically Loading a QMainWindow?
    By natron in forum Newbie
    Replies: 10
    Last Post: 21st July 2006, 01:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.