Results 1 to 19 of 19

Thread: Switching between two ui forms / QMainWindow screens

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

    Default Switching between two ui forms / QMainWindow screens

    Hi,

    I am building a QT application (QT4.8), where it has two ui forms, two threads one is main/GUI thread and other is worker thread with timer running.

    It has one Base class derived from QObject, and two other classes for ui screens derived from QMainWindow. Base class contains the pointers for ui classes.


    What is required is when application loads it should display the first screen , QMainWindow form , for that I am using show() function with CTOR of base class (shown below).

    Now when timer in the thread goes off it calls the SIGNAL function in the base class and at that time it should switch to second ui or replace with second screen.

    Issue:
    First screen is shown properly but when it tries to switch to second screen within SIGNAL function, display becomes blank i.e. it goes off.

    Any kind of help/suggestion is appreciated,

    Thank you,


    Code:
    //CTOR
    Base::Base()
    {
    qDebug() << "Base CTOR called\n";

    //Show the first screen, loading raptor status
    this->lrsGUI = new RaptorLrsGUI;
    this->lrsGUI->setlabel(QString("Loading Status..."));
    this->lrsGUI->showMaximized();
    this->lrsGUI->show();

    this->coreGUI = new RaptorCoreGUI;
    }

    /*SIGNAL function*/
    void Base::rcvInitData()
    {
    qDebug()<< "Main Thread (CALLBACK): Swtch screen\n";

    this->lrsGUI->hide();
    this->coreGUI->setlabel(QString("Second Screen..."));
    this->coreGUI->showMaximized();
    this->coreGUI->show();
    bootInitData = 1;

    qDebug()<< "Main Thread (CALLBACK): EXiting Display initial screen\n";
    }

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,022
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Switching between two ui forms / QMainWindow screens

    You are not creating either of these widgets with parents, therefore they will act as top-level windows. I assume that is what you want.

    Calling both showMaximized() and show() is redundant. Both of these methods show the window, so if what you want is for the window to appear maximized on screen then call showMaximized() only.

    This is not the way I would do it, however. You do not need the Base class at all. You probably don't need the worker thread either if all it is doing is firing a timer.

    I would make one QMainWindow-based class, and use a QStackedWidget as its centralWidget. Add each of your GUI classes as pages in the stack. When the QMainWidget is constructed, set the current index for the stack to page 0 (your RaptorLrsGUI, derived from QWidget). Start the timer as a single shot in the MainWindow constructor and connect its timeout() signal to a MainWindow slot that switches the stack widget to index 1 (your RaptorCoreGUI class, also derived form QWidget).

    display becomes blank i.e. it goes off.
    Then you are probably doing something wrong in the code you haven't shown us.
    <=== 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

    Quote Originally Posted by d_stranz View Post
    You are not creating either of these widgets with parents, therefore they will act as top-level windows. I assume that is what you want.

    Calling both showMaximized() and show() is redundant. Both of these methods show the window, so if what you want is for the window to appear maximized on screen then call showMaximized() only.

    This is not the way I would do it, however. You do not need the Base class at all. You probably don't need the worker thread either if all it is doing is firing a timer.

    I would make one QMainWindow-based class, and use a QStackedWidget as its centralWidget. Add each of your GUI classes as pages in the stack. When the QMainWidget is constructed, set the current index for the stack to page 0 (your RaptorLrsGUI, derived from QWidget). Start the timer as a single shot in the MainWindow constructor and connect its timeout() signal to a MainWindow slot that switches the stack widget to index 1 (your RaptorCoreGUI class, also derived form QWidget).



    Then you are probably doing something wrong in the code you haven't shown us.
    Hi,

    Thank you for reply, I am new to QT and this is kind of a PoC I am doing, this is what I want to achieve:
    1) Keep two separate GUIs (ui forms).
    2) When application starts, it should spawn a thread which will run timer and at the same time display first GUI on screen (happens in C'TOR).
    3) When the timer of the thread goes off, it will emit its SIGNAL and SLOT function of main will get called which will stop the first GUI and replace with second one.

    Below is the complete code(bit messy), I would appreciate if you could help in designing the above,

    Files:
    clientthread.h (worker thread)
    coregui.h (second ui form/screen)
    lsrgui.h (first ui form/screen)
    base.h

    clientthread.cpp
    coregui.cpp
    lsrgui.cpp
    main.cpp

    /clientthread.h/
    class ClientThread : public QThread
    {
    Q_OBJECT

    signals:
    void sendInitData();
    private:
    void run();
    QString m_lastTime;
    private slots:
    void timerHit();

    };

    /coregui.h/
    namespace Ui {
    class RaptorCoreGUI;
    }

    class RaptorCoreGUI : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit RaptorCoreGUI(QWidget *parent = 0);
    ~RaptorCoreGUI();
    void setlabel(QString);

    private:
    Ui::RaptorCoreGUI *ui;
    };

    /lsrgui.h/
    namespace Ui {
    class RaptorLrsGUI;
    }

    class RaptorLrsGUI : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit RaptorLrsGUI(QWidget *parent = 0);
    ~RaptorLrsGUI();
    void setlabel(QString);

    private:
    Ui::RaptorLrsGUI *ui;
    };

    /base.h/
    class Base : public QObject
    {
    Q_OBJECT
    public:
    RaptorLrsGUI *lrsGUI;
    RaptorCoreGUI *coreGUI;
    QStackedWidget *stackedWidget;
    Base();

    private slots:
    void rcvInitData();
    void rcvLPNotif();
    void rcvErrCounterInfo();
    void rcvGenNotif();
    };

    **/clientthread.c /
    void ClientThread::run()
    {
    QTimer timer;
    connect(&timer, SIGNAL(timeout()), this, SLOT(timerHit()), Qt:irectConnection);
    timer.setInterval(5000);
    timer.start(); // puts one event in the threads event queue
    exec();
    timer.stop();
    }

    void ClientThread::timerHit()
    {
    qDebug() << "Client Thread: Emitting sendMsg signal\n";
    emit sendInitData();
    }

    /coregui.cpp/
    RaptorCoreGUI::RaptorCoreGUI(QWidget *parent) :
    QMainWindow(parent, Qt::FramelessWindowHint),
    ui(new Ui::RaptorCoreGUI)
    {
    ui->setupUi(this);
    this->setAutoFillBackground(true);
    this->setStyleSheet("background-color:white;");
    }

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

    void RaptorCoreGUI::setlabel(QString label)
    {
    ui->label->setText(label);
    }

    /lsrgui.cpp/
    RaptorLrsGUI::RaptorLrsGUI(QWidget *parent) :
    QMainWindow(parent, Qt::FramelessWindowHint),
    ui(new Ui::RaptorLrsGUI)
    {

    ui->setupUi(this);
    this->setAutoFillBackground(true);
    this->setStyleSheet("background-color:white;");
    }

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

    void RaptorLrsGUI::setlabel(QString label)
    {
    ui->label->setText(label);
    }

    /base.cpp/
    Base::Base()
    {
    qDebug() << "Base CTOR called\n";

    //Show the first screen, loading raptor status
    this->lrsGUI = new RaptorLrsGUI;
    this->lrsGUI->setlabel(QString("Loading Raptor Status..."));
    this->lrsGUI->showMaximized();
    this->lrsGUI->show();

    //this->coreGUI = coreGUI;
    this->coreGUI = new RaptorCoreGUI;
    }

    void Base::rcvInitData()
    {
    this->lrsGUI->hide();
    this->coreGUI->setlabel(QString("Raptor Status..."));
    this->coreGUI->setGeometry(480,272,-0,+128);
    this->coreGUI->showMaximized();
    this->coreGUI->show(); // ISSUE: DOES NOT SHOW THE SECOND GUI, BUT IF WE COMMENT OUT THE SHOW OF FIRST GUI (this->lrsGUI->show()),
    THEN SECOND ONE IS SHOWN PROPERLY

    qDebug()<< "Main Thread (CALLBACK): EXiting Display initial screen\n";
    }

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    RaptorLrsGUI LrsGui;
    RaptorCoreGUI CoreGui;

    Base baseObj;

    //instantiate Client thread object
    ClientThread clientThread;

    QObject::connect(&clientThread, SIGNAL(sendInitData()), &baseObj, SLOT(rcvInitData()), Qt::QueuedConnection);

    qDebug() << "Main Thread: starting clockThread\n";
    clientThread.start();

    app.exec();
    qDebug() << "Mian Thread: Quiting clockThread\n";
    clientThread.quit();
    qDebug() << "Main Thread: Waiting on clockThread \n";
    clientThread.wait();

    return 0;
    }

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,022
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Switching between two ui forms / QMainWindow screens

    You seem to be stuck on this design, which I have told you already is overly complicated for what you have said you want to do.

    - You do not need a separate thread simply to send a timeout signal to switch UIs. All you need is a simple QTimer in QMainWindow.
    - You should not have two different instances of QMainWindow. If you want to switch UIs, do that within a single QMainWindow instance using something like a QStackedWidget.
    - Get rid of your Base class. It serves no purpose except to make your design more complex.

    The simplified design should look like this:

    - Derive a QMainWindow class.
    - Derive two QWidget classes, one for each of your UIs.
    - In the MainWindow constructor, create a QStackedWidget instance.
    - call QMainWindow::setCentralWidget() with the QStackedWIdget pointer
    - Create an instance of each of your UI classes.
    - Add each instance to the QStackedWidget as pages 0 and 1
    - Set the current stack widget index to 0
    - Create a single-shot QTimer with a 5000 ms timeout, connect it to a MainWindow slot, and start the timer
    - Exit the constructor
    - In the timeout slot, set the stack widget index to 1 to show the other UI

    That's all you need to do. No client thread, no Base class, no duplicate main windows, no calls to show(), showMaximized() or anything else.

    Please see my signature below, and learn how to use CODE tags when posting source code. Your source code is unreadable otherwise.
    <=== 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

    Hi,

    Thank you very much for reply, I know that it can be done in a single shot thread , but as I told you this is not final design its just for PoC, main idea is to know how to switch screens.

    I have changed the code as per your suggestion, but still I see only one ui screen all the time, below is the code:


    Qt Code:
    1. Base::Base()
    2. {
    3. qDebug() << "Base CTOR called\n";
    4.  
    5. this->lrsGUI = new RaptorLrsGUI;
    6. this->lrsGUI->setlabel(QString("Loading Status..."));
    7. this->coreGUI = new RaptorCoreGUI;
    8.  
    9. this->stackedWidget = new QStackedWidget();
    10. this->setCentralWidget(this->stackedWidget);
    11. this->stackedWidget->addWidget(this->lrsGUI);
    12. this->stackedWidget->addWidget(this->coreGUI);
    13.  
    14. this->stackedWidget->setCurrentIndex(0);
    15.  
    16. }
    17.  
    18. /* SLOT Fn */
    19. void Base::rcvInitData()
    20. {
    21. this->stackedWidget->setCurrentIndex(1);
    22. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,022
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Switching between two ui forms / QMainWindow screens

    And what is "Base"? Is it now a QMainWindow class? You haven't called the QMainWindow constructor from your Base class constructor.

    Have you run your program in the debugger? Did you set a breakpoint on line 21 to see if this slot is ever called?
    <=== 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.

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

    Default Re: Switching between two ui forms / QMainWindow screens

    Base class is derived from QMainWindow as below:

    Qt Code:
    1. class Base : public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. RaptorLrsGUI *lrsGUI;
    6. RaptorCoreGUI *coreGUI;
    7. QStackedWidget *stackedWidget;
    8. Base();
    9.  
    10. private slots:
    11. void rcvInitData();
    12.  
    13. };
    To copy to clipboard, switch view to plain text mode 

    I have put the log in the SLOT function and I can see it getting printed:

    Qt Code:
    1. void Base::rcvInitData()
    2. {
    3. qDebug()<< "Main Thread (CALLBACK): Display second screen\n";
    4. this->stackedWidget->setCurrentIndex(1);
    5. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. Base CTOR called
    2.  
    3. Client Thread: Connecting timer object timeout signal with threads timerhit slot
    4.  
    5. Client Thread: Starting timer objecti
    6.  
    7. Client Thread: executing clock thread
    8.  
    9. Client Thread: Emitting sendMsg signal
    10.  
    11. Main Thread (CALLBACK): Display second screen
    12.  
    13. Client Thread: Emitting sendMsg signal
    14.  
    15. Main Thread (CALLBACK): Display second screen
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,022
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Switching between two ui forms / QMainWindow screens

    Client Thread: Emitting sendMsg signal

    Main Thread (CALLBACK): Display second screen

    Client Thread: Emitting sendMsg signal

    Main Thread (CALLBACK): Display second screen
    This is the thing that doesn't make any sense. Why would you have a QTimer emit a signal to show the same window more than once?

    If this second window is not being shown, then you are doing something else wrong somewhere, and you haven't posted enough code to demonstrate what that is.
    <=== 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.

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

    Default Re: Switching between two ui forms / QMainWindow screens

    I actually made it work by adding "stackedWidget->showMaximized();" after setCurrentIndex(0) in constructor. But still I have couple of issues

    1) I could not set the backgroung color of widgets to white , even after doing "setStyleSheet("background-color:white;");".
    This I temp resolved by deriving the ui classes from QMainWindow instead of QWidget.

    2) I am not able to remove the titlebar from the window.

    Below is the complete code:

    Qt Code:
    1. /*First UI header*/
    2. namespace Ui {
    3. class RaptorLrsGUI;
    4. }
    5.  
    6. class RaptorLrsGUI : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. explicit RaptorLrsGUI(QWidget *parent = 0);
    12. ~RaptorLrsGUI();
    13. void setlabel(QString);
    14.  
    15. private:
    16. Ui::RaptorLrsGUI *ui;
    17. };
    18.  
    19. /*Second UI header */
    20. namespace Ui {
    21. class RaptorCoreGUI;
    22. }
    23.  
    24. class RaptorCoreGUI : public QMainWindow
    25. {
    26. Q_OBJECT
    27.  
    28. public:
    29. explicit RaptorCoreGUI(QWidget *parent = 0);
    30. ~RaptorCoreGUI();
    31. void configInitData(bool);
    32. void setlabel(QString);
    33.  
    34. private:
    35. Ui::RaptorCoreGUI *ui;
    36. };
    37.  
    38.  
    39. /*Base class header*/
    40. class Base : public QMainWindow
    41. {
    42. Q_OBJECT
    43. public:
    44. RaptorLrsGUI *lrsGUI;
    45. RaptorCoreGUI *coreGUI;
    46. QStackedWidget *stackedWidget;
    47. QVBoxLayout *layout;
    48. ~Base();
    49. explicit Base (QWidget *parent = 0);
    50.  
    51. private slots:
    52. void rcvInitData();
    53.  
    54. };
    55.  
    56. /* Second UI cpp */
    57. RaptorCoreGUI::RaptorCoreGUI(QWidget *parent) :
    58. QMainWindow(parent, Qt::FramelessWindowHint),
    59. ui(new Ui::RaptorCoreGUI)
    60. {
    61. ui->setupUi(this);
    62. this->setAutoFillBackground(true);
    63. this->setStyleSheet("background-color:white;");
    64. }
    65.  
    66. RaptorCoreGUI::~RaptorCoreGUI()
    67. {
    68. delete ui;
    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. QMainWindow(parent, Qt::FramelessWindowHint),
    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) : QMainWindow(parent, Qt::FramelessWindowHint)
    102. {
    103. qDebug() << "Base CTOR called\n";
    104.  
    105.  
    106. //Show the first screen, loading raptor status
    107. lrsGUI = new RaptorLrsGUI;
    108. lrsGUI->setlabel(QString("Loading Raptor Status..."));
    109. coreGUI = new RaptorCoreGUI;
    110.  
    111. stackedWidget = new QStackedWidget();
    112. //this->setCentralWidget(this->stackedWidget); // If uncommented it directly shows second UI
    113. stackedWidget->addWidget(lrsGUI);
    114. stackedWidget->addWidget(coreGUI);
    115.  
    116. stackedWidget->setCurrentIndex(0);
    117. stackedWidget->showMaximized();
    118. }
    119.  
    120. Base::~Base()
    121. {
    122. qDebug() << "Main Thread: destroying base objects \n";
    123. delete lrsGUI;
    124. delete coreGUI;
    125. delete stackedWidget;
    126. }
    127.  
    128. void Base::rcvInitData()
    129. {
    130. qDebug()<< "Main Thread (CALLBACK): Display second screen\n";
    131. stackedWidget->setCurrentIndex(1);
    132. }
    133.  
    134.  
    135. //! [1]
    136. int main(int argc, char *argv[])
    137. {
    138. QApplication app(argc, argv);
    139.  
    140. //Instantiate base class
    141. Base baseObj;
    142.  
    143.  
    144. //instantiate Client thread object
    145. ClientThread clientThread;
    146. qDebug() << "Connecting sendmsg and handle_callback1()\n";
    147. QObject::connect(&clientThread, SIGNAL(sendInitData()), &baseObj, SLOT(rcvInitData()), Qt::QueuedConnection);
    148.  
    149. qDebug() << "Main Thread: starting clockThread\n";
    150. clientThread.start();
    151.  
    152. app.exec();
    153. qDebug() << "Mian Thread: Quiting clockThread\n";
    154. clientThread.quit();
    155. qDebug() << "Main Thread: Waiting on clockThread \n";
    156. clientThread.wait();
    157.  
    158. return 0;
    159. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,022
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Switching between two ui forms / QMainWindow screens

    This I temp resolved by deriving the ui classes from QMainWindow instead of QWidget.
    Do you understand that QMainWindow is supposed to be the only top-level window in your app, and that there is only supposed to be one instance of that class? You now have THREE classes that are derived from QMainWindow. QMainWindow is not a magic bullet class that you can derive anything from just to achieve some desired UI effect (like a different background color). And showMaximized() should never be called on a child window, only on a top-level window like QMainWindow.

    QMainWindow is not just a widget, it defines a framework and a set of behaviors for applications with a single top-level window and many child windows within it. You cannot just simply use a QMainWindow anywhere you want without running into problems somewhere along the line because you haven't followed the rules.

    I have no idea what types of classes "RaptorLrsGUI" and "RaptorCoreGUI" are. If they aren't derived from QWidget, then that is an error. If you have a custom paintEvent() in these classes, then that is where you should implement the code to fill the background with whatever color you want. If you do not have a paintEvent(), then in the constructor for these classes you should have code like this:

    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 );
    4.  
    5. setAutoFillBackground( true );
    To copy to clipboard, switch view to plain text mode 

    Setting the appropriate colors in the palette as well as ensuring that the widget automatically clears its background and fills it with the chosen color on a paintEvent() is the right way to do it.

    I think you need to take a step back and look at some of the many examples in Qt for QMainWindow-based apps. If you continue down the path you are taking, your PoC is going to turn out to be a "proof of buggy code" that acts in ways you can't explain and is confusing to debug.
    <=== 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.

  11. #11
    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.

  12. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,022
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    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.

  13. #13
    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

  14. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,022
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    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.

  15. #15
    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

  16. #16
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,022
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    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.

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

    Default Re: Switching between two ui forms / QMainWindow screens

    Yes, it does show same in both cases (show() and showMaximized()) as attached. Only label color is white rest is grey.


    Added after 29 minutes:


    Hi,

    I got it work , I missed baseObj.show(); in main.
    Attached Images Attached Images
    Last edited by nitks.abhinav; 26th September 2017 at 22:15.

  18. #18
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,022
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Switching between two ui forms / QMainWindow screens

    I got it work , I missed baseObj.show(); in main.
    Good. I was afraid we would have to put your program together piece-by-piece until it finally worked.
    <=== 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.

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

    Default Re: Switching between two ui forms / QMainWindow screens

    Thank you for all the help!

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.