PDA

View Full Version : Replace Original Interface with a new one



stbb24
9th June 2012, 06:35
I have an option menu and under it is original and replace with the code definition below for each option


// code for original interface
void MainWindow::on_actionOriginal_triggered()
{
ui->centralWidget->show();
}

// code for replace original interface
void MainWindow::on_actionReplace_triggered()
{
ui->centralWidget->hide();
}


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?

ChrisW67
9th June 2012, 07:24
You already have a perfectly workable answer to this question in the first thread you opened on the topic.

stbb24
9th June 2012, 07:37
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



void MainWindow::on_actionReplace_triggered()
{
ui->newWidget = new QWidget;
ui->stackWidget = new QStackedWidget;
ui->stackWidget->addWidget(ui->newWidget);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(ui->stackWidget);
setLayout(layout);
}


It gives this error
QWidget::setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout

ChrisW67
9th June 2012, 08:06
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 (http://en.wikipedia.org/wiki/Grue_%28monster%29)) 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.