简体   繁体   中英

Qt - how to make QPushButton change between two QVBoxLayouts

So i have a main layout called the 'vboxmain'. And the program has two states: blackjack and poker. For both I have a button. In this vboxmain I have an upper part, which covers most of the program, and is the same for both games, but I also have a bottom part which should display different parts for both games. For example, poker game should hold five QCheckBoxes and one button. As for the blackjack game i need simply two buttons. I created both of these bottom layouts as QVBoxLayouts. So now i have:

BlackjackiValikud = new QVBoxLayout; //for blackjack
Pokkerivalikud = new QVBoxLayout;  //for poker

And I tried creating two button actions like this:

void mainwindow::BlackJack_clicked(){
    vboxmain->removeItem(Pokkerivalikud);
    vboxmain->addItem(BlackjackiValikud);
}

void mainwindow::Poker_clicked(){
    vboxmain->removeItem(BlackjackiValikud);
    vboxmain->addItem(Pokkerivalikud);
}

Buttons are connected like this:

connect(BlackjackButton, SIGNAL(clicked()), this, SLOT(BlackJack_clicked()));
connect(PokerButton, SIGNAL(clicked()), this, SLOT(Poker_clicked()));

But currently it's not working and I can't figure out a way to do this, so I'm asking for help. This is probably not the best way to do this either but I don't know any other ways. So I could use some help on how to make this work with whatever solution - so that with both buttons I can change the bottom part of my vboxmain as needed.

I'm open to solutions.

What do you mean by it is not working?

You have to make sure that the layout are enabled when you add them (via QLayout::setEnabled ( bool enable) ) or that widget are visible (via QWidget::show() ). In general you have to manuable make visible items which are added to a widget which is already visible...

An alternative would be to use a QStackedLayout to display either. You have a widget poker for the poker view and a widget blackjack for the black jack view. On button push you use either

void QStackedLayout::setCurrentIndex ( int index )
void QStackedLayout::setCurrentWidget ( QWidget * widget )

You may want to keep the layouts and change what's presented in the bottom layout. To do so, create classes for each game(say blakjackWidget and pokerWidget) derived from QWidget. and show only one of them in the bottom layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM