简体   繁体   English

Qt - 如何在两个QVBoxLayouts之间更改QPushButton

[英]Qt - how to make QPushButton change between two QVBoxLayouts

So i have a main layout called the 'vboxmain'. 所以我有一个名为'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. 在这个vboxmain中,我有一个上半部分,它覆盖了大部分程序,并且两个游戏都是相同的,但我也有一个底部应该为两个游戏显示不同的部分。 For example, poker game should hold five QCheckBoxes and one button. 例如,扑克游戏应该拥有五个QCheckBox和一个按钮。 As for the blackjack game i need simply two buttons. 至于二十一点游戏我只需要两个按钮。 I created both of these bottom layouts as QVBoxLayouts. 我创建了这两个底部布局作为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. 因此,我可以使用一些帮助来解决如何使用任何解决方案进行此工作 - 因此,使用两个按钮,我可以根据需要更改我的vboxmain的底部。

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() ). 您必须确保在添加布局时启用布局(通过QLayout::setEnabled ( bool enable) )或该窗口小部件是可见的(通过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. 另一种方法是使用QStackedLayout来显示。 You have a widget poker for the poker view and a widget blackjack for the black jack view. 你有一个小部件poker为扑克视图和小部件blackjack为黑杰克视图。 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. 为此,请为从QWidget派生的每个游戏(比如blakjackWidget和pokerWidget)创建类。 and show only one of them in the bottom layout. 并在底部布局中仅显示其中一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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