C++ 如何在不进行设计的情况下将Qbutton添加到页面?
我是新来的。我开发了一个程序,需要C++ 如何在不进行设计的情况下将Qbutton添加到页面?,c++,qt,qt5,C++,Qt,Qt5,我是新来的。我开发了一个程序,需要1024行,每行有两个按钮,两个单选按钮和两个LALBE。我有两条路要走 在设计器模式下,我拖放2*1204个按钮、2*1024个标签和2*1024个标签 单选按钮显示这是不合逻辑的 有一种方法可以在没有设计器模式和拖放的情况下添加此小部件 例如,在运行时,我单击一个按钮并在代码隐藏中打开页面 将这些小部件(标签、按钮、单选按钮)添加到页面或类似的内容 我在网络编程方面做了第二件事。这在QT中可能吗?或者类似的事情? < P>你在Qt设计器中所做的一切最终都被翻
1024行
,每行有两个按钮,两个单选按钮和两个LALBE。我有两条路要走
我在网络编程方面做了第二件事。这在QT中可能吗?或者类似的事情? < P>你在Qt设计器中所做的一切最终都被翻译成C++代码创建对象并将它们链接到一起。
因此,将代码(查看生成的头文件)放入for循环是非常简单的。甚至可以自己重新开始,创建三个按钮并将它们添加到布局只需几行代码。您可以通过编程方式创建小部件并通过布局调整它们的位置。 例如,它可能看起来是这样的:
QVBoxLayout *topLayout = new QVBoxLayout();
for (int lineNumber = 0; lineNumber < 1024; ++lineNumber)
{
QWidget *oneLineWidget = new QWidget(this);
QHBoxLayout *oneLineWidgetLayout = new QHBoxLayout();
{ //added these brackets just for the ease of reading.
QLabel *labFirst = new QLabel(tr("first label"), oneLineWidget);
QLabel *labSecond = new QLabel(tr("second label"), oneLineWidget);
QPushButton *bFirst = new QPushButton(tr("first button"), oneLineWidget);
QPushButton *bSecond = new QPushButton(tr("second button"), oneLineWidget);
QRadioButton *rbFirst = new QRadioButton(tr("first radiobutton"), oneLineWidget);
QRadioButton *rbSecond = new QRadioButton(tr("second radiobutton"), oneLineWidget);
oneLineWidgetLayout->addWidget(labFirst);
oneLineWidgetLayout->addWidget(labSecond);
oneLineWidgetLayout->addWidget(bFirst);
oneLineWidgetLayout->addWidget(bSecond);
//lets put one radioButton under another.
QVBoxLayout *radioButtonsLayout = new QVBoxLayout();
{
radioButtonsLayout->addWidget(rbFirst);
radioButtonsLayout->addWidget(rbSecond);
}
//and now we can combine layouts.
oneLineWidgetLayout->addLayout(radioButtonsLayout);
}
oneLineWidget->setLayout(oneLineWidgetLayout);
topLayout->addWidget(oneLineWidget);
}
this->setLayout(topLayout);
QVBoxLayout*topLayout=新的QVBoxLayout();
对于(int lineNumber=0;lineNumber<1024;++lineNumber)
{
QWidget*oneLineWidget=新的QWidget(此);
QHBoxLayout*oneLineWidgetLayout=新的QHBoxLayout();
{//添加这些括号只是为了便于阅读。
QLabel*labFirst=新的QLabel(tr(“第一标签”),oneLineWidget);
QLabel*labSecond=新的QLabel(tr(“第二标签”),oneLineWidget);
QPushButton*bFirst=新的QPushButton(tr(“第一个按钮”),oneLineWidget);
QPushButton*bSecond=新的QPushButton(tr(“第二个按钮”),oneLineWidget);
QRadioButton*rbFirst=新的QRadioButton(tr(“第一个单选按钮”),oneLineWidget);
QRadioButton*rbSecond=新的QRadioButton(tr(“第二个单选按钮”),oneLineWidget);
oneLineWidgetLayout->addWidget(labFirst);
oneLineWidgetLayout->addWidget(labSecond);
oneLineWidgetLayout->addWidget(bFirst);
oneLineWidgetLayout->addWidget(b秒);
//让我们把一个单选按钮放在另一个单选按钮下面。
QVBoxLayout*radioButtonsLayout=新的QVBoxLayout();
{
radioButtonsLayout->addWidget(rbFirst);
radioButtonsLayout->addWidget(秒);
}
//现在我们可以组合布局了。
oneLineWidgetLayout->addLayout(radioButtonsLayout);
}
oneLineWidget->setLayout(oneLineWidgetLayout);
topLayout->addWidget(oneLineWidget);
}
此->设置布局(topLayout);
您可以使用不同类型的布局(QBoxLayout、QGridLayout、QFormLayout等)。你可以从一开始。有一个继承它的类的列表。
我希望这会有所帮助!:)祝你好运 顺便说一句,我把它写在记事本上,并没有“在现实生活中”进行测试。所以它可能包含一些错误。但至少我希望它能为你指明正确的方向。