Results 1 to 19 of 19

Thread: Switching between two ui forms / QMainWindow screens

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2017
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Switching between two ui forms / QMainWindow screens

    Hi,
    I very well know that QMainWindow is top level/parent window. That's what initially I did, when classes "RaptorLrsGUI" and "RaptorCoreGUI" are derived from QWidget and QStackedWidget is used , I don't see anything on screen, it goes blank. The below code is exactly as per suggested and it does not work.
    Also I have changed the code to remove showMaximized on widget and use single shot timer.

    Thanks for your patience..:-)

    Qt Code:
    1. Base::Base(QWidget *parent) : QMainWindow(parent, Qt::FramelessWindowHint)
    2. {
    3. qDebug() << "Base CTOR called\n";
    4.  
    5. QPalette pal = palette(); // retrieves the default palette for the widget, as defined by the app's style
    6. pal.setColor( QPalette::Window, Qt::white );
    7. setPalette( pal );
    8.  
    9. setAutoFillBackground( true );
    10.  
    11. //Show the first screen, loading raptor status
    12. lrsGUI = new RaptorLrsGUI; //RaptorLrsGUI derived from QWidget
    13. lrsGUI->setlabel(QString("Loading Raptor Status..."));
    14. coreGUI = new RaptorCoreGUI; //RaptorCoreGUI derived from QWidget
    15.  
    16. stackedWidget = new QStackedWidget();
    17. stackedWidget->addWidget(lrsGUI);
    18. stackedWidget->addWidget(coreGUI);
    19.  
    20. stackedWidget->setCurrentIndex(0);
    21. }
    22.  
    23. Base::~Base()
    24. {
    25. qDebug() << "Main Thread: destroying base objects \n";
    26. delete lrsGUI;
    27. delete coreGUI;
    28. delete stackedWidget;
    29. }
    30.  
    31.  
    32. void Base::rcvInitData()
    33. {
    34. qDebug()<< "Main Thread (CALLBACK): Display second screen\n";
    35. stackedWidget->setCurrentIndex(1);
    36. }
    To copy to clipboard, switch view to plain text mode 

    Rest of the code remain same as earlier.

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

    Default Re: Switching between two ui forms / QMainWindow screens

    You create the QStackedWidget, but you do not give it any parent. You do not tell your QMainWidget to use the QStackedWidget as its "centralWidget". Thus the QStackedWidget is never shown, so it is always invisible.

    Line 16 should be:

    Qt Code:
    1. stackedWidget = new QStackedWidget( this );
    To copy to clipboard, switch view to plain text mode 

    and add a line at the end of the constructor:
    Qt Code:
    1. setCentralWidget( stackedWidget );
    To copy to clipboard, switch view to plain text mode 

    If you want the background of the Raptor* widgets to be white, this code needs to be moved to their constructors. It does nothing if you have it in the Base constructor:

    Qt Code:
    1. QPalette pal = palette(); // retrieves the default palette for the widget, as defined by the app's style
    2. pal.setColor( QPalette::Window, Qt::white );
    3. setPalette( pal );
    To copy to clipboard, switch view to plain text mode 

    Edit: Actually, maybe this is incorrect. If you set the palette before creating the stacked and Raptor* widgets, it is possible that they will inherit the QPalette:: Window setting from the Base class.
    Last edited by d_stranz; 26th September 2017 at 18:05.
    <=== 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.

  3. #3
    Join Date
    Sep 2017
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Switching between two ui forms / QMainWindow screens

    I made the changes the but still I can see white screen with title bar (screenshot attached) , below is the complete code.

    Thanks you for all the help,

    Qt Code:
    1. /Second UI */
    2. namespace Ui {
    3. class RaptorCoreGUI;
    4. }
    5.  
    6. class RaptorCoreGUI : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. explicit RaptorCoreGUI(QWidget *parent = 0);
    12. ~RaptorCoreGUI();
    13. void configInitData(bool);
    14. void setlabel(QString);
    15.  
    16. private:
    17. Ui::RaptorCoreGUI *ui;
    18. };
    19.  
    20. /*First UI*/
    21. amespace Ui {
    22. class RaptorLrsGUI;
    23. }
    24.  
    25. class RaptorLrsGUI : public QWidget
    26. {
    27. Q_OBJECT
    28.  
    29. public:
    30. explicit RaptorLrsGUI(QWidget *parent = 0);
    31. ~RaptorLrsGUI();
    32. void setlabel(QString);
    33.  
    34. private:
    35. Ui::RaptorLrsGUI *ui;
    36. };
    37.  
    38. /*Base class derived from QMainWindow*/
    39. class Base : public QMainWindow
    40. {
    41. Q_OBJECT
    42. public:
    43. RaptorLrsGUI *lrsGUI;
    44. RaptorCoreGUI *coreGUI;
    45. QStackedWidget *stackedWidget;
    46. QVBoxLayout *layout;
    47. ~Base();
    48. explicit Base (QWidget *parent = 0);
    49.  
    50.  
    51. private slots:
    52. void rcvInitData();
    53. };
    54.  
    55. /*Second UI CPP*/
    56. RaptorCoreGUI::RaptorCoreGUI(QWidget *parent) :
    57. QWidget(parent),
    58. ui(new Ui::RaptorCoreGUI)
    59. {
    60. ui->setupUi(this);
    61. this->setAutoFillBackground(true);
    62. this->setStyleSheet("background-color:white;");
    63. }
    64.  
    65. RaptorCoreGUI::~RaptorCoreGUI()
    66. {
    67. delete ui;
    68. }
    69.  
    70.  
    71. void RaptorCoreGUI::setlabel(QString label)
    72. {
    73. qDebug() << "Main Thread(COREGUI): Setting Label\n";
    74. ui->label->setText(label);
    75. }
    76.  
    77. /*First UI cpp*/
    78. RaptorLrsGUI::RaptorLrsGUI(QWidget *parent) :
    79. QWidget(parent),
    80. ui(new Ui::RaptorLrsGUI)
    81. {
    82. ui->setupUi(this);
    83. this->setAutoFillBackground(true);
    84. this->setStyleSheet("background-color:white;");
    85. }
    86.  
    87. RaptorLrsGUI::~RaptorLrsGUI()
    88. {
    89. qDebug() << "Main Thread: destroying the ui object \n";
    90. delete ui;
    91. }
    92.  
    93. void RaptorLrsGUI::setlabel(QString label)
    94. {
    95. qDebug() << "Main Thread(LSRGUI): Setting Label\n";
    96. ui->label->setText(label);
    97. }
    98.  
    99.  
    100. /*main.cpp*/
    101. Base::Base(QWidget *parent) :
    102. QMainWindow(parent, Qt::FramelessWindowHint)
    103. {
    104. qDebug() << "Base CTOR called\n";
    105.  
    106. QPalette pal = palette(); // retrieves the default palette for the widget, as defined by the app's style
    107. pal.setColor( QPalette::Window, Qt::white );
    108. setPalette( pal );
    109.  
    110. setAutoFillBackground( true );
    111.  
    112.  
    113. //Show the first screen, loading raptor status
    114. lrsGUI = new RaptorLrsGUI;
    115. lrsGUI->setlabel(QString("Loading Raptor Status..."));
    116. coreGUI = new RaptorCoreGUI;
    117.  
    118. stackedWidget = new QStackedWidget(this);
    119. stackedWidget->addWidget(lrsGUI);
    120. stackedWidget->addWidget(coreGUI);
    121.  
    122. stackedWidget->setCurrentIndex(0);
    123. setCentralWidget(stackedWidget);
    124. }
    125.  
    126. Base::~Base()
    127. {
    128. qDebug() << "Main Thread: destroying base objects \n";
    129. delete lrsGUI;
    130. delete coreGUI;
    131. delete stackedWidget;
    132. }
    133.  
    134. void Base::rcvInitData()
    135. {
    136. qDebug()<< "Main Thread (CALLBACK): Display second screen\n";
    137. stackedWidget->setCurrentIndex(1);
    138. }
    139.  
    140. //! [1]
    141. int main(int argc, char *argv[])
    142. {
    143. QApplication app(argc, argv);
    144.  
    145. //Instantiate base class
    146. Base baseObj;
    147.  
    148.  
    149. //instantiate Client thread object
    150. ClientThread clientThread;
    151. qDebug() << "Connecting sendmsg and handle_callback1()\n";
    152. QObject::connect(&clientThread, SIGNAL(sendInitData()), &baseObj, SLOT(rcvInitData()), Qt::QueuedConnection);
    153.  
    154. qDebug() << "Main Thread: starting clockThread\n";
    155. clientThread.start();
    156.  
    157. app.exec();
    158. qDebug() << "Mian Thread: Quiting clockThread\n";
    159. clientThread.quit();
    160. qDebug() << "Main Thread: Waiting on clockThread \n";
    161. clientThread.wait();
    162. return 0;
    163. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

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

    Default Re: Switching between two ui forms / QMainWindow screens

    I think we need to start from zero, because something is clearly wrong with what you are doing now.

    Please write and run these two programs. You can simply comment out the lines in your current main.cpp and replace them with these lines:

    Qt Code:
    1. #include <QtWidgets/QApplication>
    2. #include "RaptorCoreGUI.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. RaptorCoreGUI w;
    9. w.show();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtWidgets/QApplication>
    2. #include "RaptorLrsGUI.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. RaptorLrsGUI w;
    9. w.show();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    In each case, do you see the windows you expect to see?
    <=== 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. #5
    Join Date
    Sep 2017
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Switching between two ui forms / QMainWindow screens

    Yes I can see the windows (attached)

    IMG-4835 --> coreGUI
    ING-4836 --> lrsGUI
    Attached Images Attached Images

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

    Default Re: Switching between two ui forms / QMainWindow screens

    OK, good. The problem is not with your Raptor* widgets.

    Next step. Do you see the RaptorLrsGUI window when you run this code?

    Qt Code:
    1. #include <QtWidgets/QApplication>
    2. #include <QtWidgets/QMainWindow>
    3. #include <QtWidgets/QStackedWidget>
    4. #include "RaptorLrsGUI.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10. QStackedWidget * pStack = new QStackedWidget( &w );
    11. RaptorLrsGUI * pLrs = new RaptorLrsGUI;
    12. pStack->addWidget( pLrs );
    13. w.setCentralWidget( pStack );
    14. w.show();
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    If it does, substitute "w.showMaximized()" for "w.show()". Does it still work?
    <=== 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.

Similar Threads

  1. Switching between two forms with button click
    By gfernandes in forum Newbie
    Replies: 3
    Last Post: 30th August 2013, 12:50
  2. Multiple screens navigation
    By keyga in forum Qt Quick
    Replies: 1
    Last Post: 23rd December 2012, 10:28
  3. Problem when switching between forms in QML
    By duc_bkav in forum Qt Programming
    Replies: 0
    Last Post: 24th November 2011, 02:52
  4. Switching between 2 Forms
    By strateng in forum Qt Programming
    Replies: 6
    Last Post: 4th June 2010, 08:09
  5. Multiple Forms and vertical layout in forms
    By eva2002 in forum Qt Programming
    Replies: 0
    Last Post: 13th January 2010, 05:05

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.