在我的场景中添加一个带有按钮的滚动条 window
Add a scrolabble window with buttons into my scene
我正在尝试添加一个带有 scroll-able 布局的框(位置:1140、485 和 size:225、365),布局内有按钮。我不希望盒子 move/resize。我希望该框有一个滚动条,我们可以使用它滚动浏览其中的所有按钮。
到目前为止我的代码根本不起作用,我得到的只是一个充满按钮的布局和一个拉伸框(我不想要)。到目前为止唯一可行的是按钮以 right-ish 方式添加到框中。
我只需要制作框 scroll-able 并且当按钮太多时框不会自行调整大小。
这是我的代码:
QWidget *box = new QWidget(); //creating the box and placing it where I want it
box->move(1145, 485);
box->resize(225, 365);
gameScene->addWidget(box); //adding it to the main scene
//where AM i supposed to use this?
QScrollArea *scrollArea = new QScrollArea();
QGridLayout *layout = new QGridLayout();
box->setLayout(layout);
//testButtons
QPushButton *testButton1 = new QPushButton("Button1");
layout->addWidget(testButton1);
....
QPushButton *testButtonN = new QPushButton("ButtonN");
layout->addWidget(testButtonN);
您可以在右下角看到标题为 GAME TRANSCRIPT 的框。
我只希望该框按原样包含按钮。但我不想调整它的大小,我确实想要它 scroll-able 因为它会切断底部的按钮。
QScrollArea setWidget 接受一个QWidget 作为参数。
这意味着您必须将按钮作为子项添加到布局,并且布局作为子项添加到小部件,然后您将能够将小部件设置为 QScrollArea 的子项。请参阅下面的示例:
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("button1");
QPushButton *button2 = new QPushButton("button2");
QPushButton *button3 = new QPushButton("button3");
QPushButton *button4 = new QPushButton("button4");
QPushButton *button5 = new QPushButton("button5");
QGridLayout * mainLayout = new QGridLayout;
QWidget* buttonsContainer = new QWidget;
QVBoxLayout *buttonsContainerLayout = new QVBoxLayout;
QScrollArea *scrollArea = new QScrollArea();
buttonsContainerLayout->addWidget(button1);
buttonsContainerLayout->addWidget(button2);
buttonsContainerLayout->addWidget(button3);
buttonsContainerLayout->addWidget(button4);
buttonsContainerLayout->addWidget(button5);
buttonsContainer->setLayout(buttonsContainerLayout);
scrollArea->setWidget(buttonsContainer);
mainLayout->addWidget(scrollArea);
window->setLayout(mainLayout);
window->setWindowTitle(
QApplication::translate("testscrollable", "Test Scrollable"));
window->show();
我正在尝试添加一个带有 scroll-able 布局的框(位置:1140、485 和 size:225、365),布局内有按钮。我不希望盒子 move/resize。我希望该框有一个滚动条,我们可以使用它滚动浏览其中的所有按钮。
到目前为止我的代码根本不起作用,我得到的只是一个充满按钮的布局和一个拉伸框(我不想要)。到目前为止唯一可行的是按钮以 right-ish 方式添加到框中。
我只需要制作框 scroll-able 并且当按钮太多时框不会自行调整大小。
这是我的代码:
QWidget *box = new QWidget(); //creating the box and placing it where I want it
box->move(1145, 485);
box->resize(225, 365);
gameScene->addWidget(box); //adding it to the main scene
//where AM i supposed to use this?
QScrollArea *scrollArea = new QScrollArea();
QGridLayout *layout = new QGridLayout();
box->setLayout(layout);
//testButtons
QPushButton *testButton1 = new QPushButton("Button1");
layout->addWidget(testButton1);
....
QPushButton *testButtonN = new QPushButton("ButtonN");
layout->addWidget(testButtonN);
您可以在右下角看到标题为 GAME TRANSCRIPT 的框。 我只希望该框按原样包含按钮。但我不想调整它的大小,我确实想要它 scroll-able 因为它会切断底部的按钮。
QScrollArea setWidget 接受一个QWidget 作为参数。 这意味着您必须将按钮作为子项添加到布局,并且布局作为子项添加到小部件,然后您将能够将小部件设置为 QScrollArea 的子项。请参阅下面的示例:
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("button1");
QPushButton *button2 = new QPushButton("button2");
QPushButton *button3 = new QPushButton("button3");
QPushButton *button4 = new QPushButton("button4");
QPushButton *button5 = new QPushButton("button5");
QGridLayout * mainLayout = new QGridLayout;
QWidget* buttonsContainer = new QWidget;
QVBoxLayout *buttonsContainerLayout = new QVBoxLayout;
QScrollArea *scrollArea = new QScrollArea();
buttonsContainerLayout->addWidget(button1);
buttonsContainerLayout->addWidget(button2);
buttonsContainerLayout->addWidget(button3);
buttonsContainerLayout->addWidget(button4);
buttonsContainerLayout->addWidget(button5);
buttonsContainer->setLayout(buttonsContainerLayout);
scrollArea->setWidget(buttonsContainer);
mainLayout->addWidget(scrollArea);
window->setLayout(mainLayout);
window->setWindowTitle(
QApplication::translate("testscrollable", "Test Scrollable"));
window->show();