RforMaser
RforMaser

Reputation: 29

how can i switch to another widget under the same mainwindow

I want to change to another widget under the same Mainwindow when the PushButton is clicked. whether I need to create another py file? or create 2 widget in same class? thanks for your help

Upvotes: 0

Views: 852

Answers (1)

jpo38
jpo38

Reputation: 21604

The best is to use a QStackedLayout. This one can have many widgets attached but one and only one is used/displayed.

Example:

QMainWindow* wnd = new QMainWindow();
QWidget* centralWidget = new QWidget( wnd );
wnd->setCentralWidget( centralWidget );

QStackedLayout* layout = new QStackedLayout( centralWidget );
QLabel* label1 = new QLabel( "label1", centralWidget );
QLabel* label2 = new QLabel( "label2", centralWidget );
layout->addWidget( label1 );
layout->addWidget( label2 );

...
layout->setCurrentWidget( label1 ); // shows label1, hides label2

...
layout->setCurrentWidget( label2 ); // shows label2, hides label1

Upvotes: 1

Related Questions