Results 1 to 4 of 4

Thread: Replace Original Interface with a new one

  1. #1
    Join Date
    May 2012
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Replace Original Interface with a new one

    I have an option menu and under it is original and replace with the code definition below for each option
    Qt Code:
    1. // code for original interface
    2. void MainWindow::on_actionOriginal_triggered()
    3. {
    4. ui->centralWidget->show();
    5. }
    6.  
    7. // code for replace original interface
    8. void MainWindow::on_actionReplace_triggered()
    9. {
    10. ui->centralWidget->hide();
    11. }
    To copy to clipboard, switch view to plain text mode 

    The original interface has three buttons and two line edits when the program is run. Now when the user clicks the replace option I want to replace the original interface with 2 buttons and a line edit. And when the user clicks the original option it's able to display the original interface again

    So how can I replace the contents of the original interface that was shown before with a new one?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,592
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Replace Original Interface with a new one

    You already have a perfectly workable answer to this question in the first thread you opened on the topic.

  3. #3
    Join Date
    May 2012
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Replace Original Interface with a new one

    i tried that but nothing happens so i decided to try the one above since it's the closest that i've gotten so far


    Added after 6 minutes:


    i tried doing this

    Qt Code:
    1. void MainWindow::on_actionReplace_triggered()
    2. {
    3. ui->newWidget = new QWidget;
    4. ui->stackWidget = new QStackedWidget;
    5. ui->stackWidget->addWidget(ui->newWidget);
    6.  
    7. QVBoxLayout *layout = new QVBoxLayout;
    8. layout->addWidget(ui->stackWidget);
    9. setLayout(layout);
    10. }
    To copy to clipboard, switch view to plain text mode 

    It gives this error
    Qt Code:
    1. QWidget::setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout
    To copy to clipboard, switch view to plain text mode 
    Last edited by stbb24; 9th June 2012 at 07:37.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,592
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Replace Original Interface with a new one

    So, here is the chain of events:
    • You ask how to swap between two sets of widgets in the central area of another widget
    • Several people suggest you use QStackedWidget
    • Point you at the documentation several times
    • Even provide a simple example (copied straight from the documentation) when requested

    Rather than post to that thread with "I tried that but nothing happens", explain what you actually did, and ask for help you launch off in another direction. Can you appreciate that from the outside it appears you are fumbling around in the dark (beware the grues) rather than working the problem?

    Here is the answer to this version of you question: to replace the central widget of a main window you need another widget... and then call QMainWindow::setCentralWidget() with it. Don't forget to keep a track of the memory allocated to the widgets you are switching between.

    Now contrast that to adding the two 'central' widgets from above to a single stacked widget and setting the stacked widget as the main window's central widget: once in the constructor. Then switch from one to the other with a single call to QStackedWidget::setCurrentWidget() or QStackedWidget::setCurrentIndex(). It's not difficult, and can be done in Designer if that is what you are using to construct your UI.



    OK, now I have seen your edit....

    You should put both central widgets into the QStackedWidget in the constructor of your main window class. Your slots should only set the current index of the stack to one or the other option.

Similar Threads

  1. Replies: 2
    Last Post: 6th January 2011, 12:56
  2. How to get the index of original SQL table?
    By Kode.Cooper in forum Qt Programming
    Replies: 0
    Last Post: 27th January 2010, 08:43
  3. turning widgets to original state
    By helvin in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2009, 08:35
  4. Replies: 3
    Last Post: 20th September 2009, 23:03
  5. Replies: 3
    Last Post: 3rd May 2009, 08:58

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.