带有布局、QPushButton 和 QGraphicsItem 的 Qt 应用程序
我正在尝试使用 QGraphicsView 和 QGraphicsScene 绘制各种形状,如矩形、椭圆形、文本等。为此,我正在尝试创建一个界面,其中将有一个垂直布局,除此之外将有几个按钮。单击这些按钮后,我可以在屏幕上显示各种 QGraphicsItem。我想以编程方式创建此界面,但不使用 ui。
我尝试过并以这种方式结束。
我想要按钮在右侧,verticalLayout在左侧,两者都应该填满整个屏幕。
这是我当前的实施:
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QGraphicsScene* scene = new QGraphicsScene(this);
QGraphicsView* view = new QGraphicsView(this);
view->setScene(scene);
QWidget* mainWidget = new QWidget(this);
QHBoxLayout *mainLayout = new QHBoxLayout();
QVBoxLayout *buttonLayout = new QVBoxLayout();
QVBoxLayout *vlayout2 = new QVBoxLayout();
vlayout2->addWidget(view);
QPushButton *btn1 = new QPushButton("Rectangle");
btn1->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
QPushButton *btn2 = new QPushButton("Ellipse");
btn2->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
QPushButton *btn3 = new QPushButton("Text");
btn3->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
buttonLayout->addWidget(btn1);
buttonLayout->addWidget(btn2);
buttonLayout->addWidget(btn3);
buttonLayout->addStretch();
mainLayout->addLayout(buttonLayout);
mainLayout->addLayout(vlayout2);
mainWidget->setLayout(mainLayout);
}
任何人都可以指导我吗?
I am trying to draw various shapes like rectangle, ellipse, text etc uisng QGraphicsView and QGraphicsScene. For that I am trying to create an interface where there will be a vertical layout and besides that there will be few buttons. On clicking those buttons, I can show various QGraphicsItem's on screen. I want to create this interface programatically but not using ui.
I tried and ended up this way.
I wanted buttons on the right side and verticalLayout on the left side and both should filled up the whole screen.
Here is my current implementation :
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QGraphicsScene* scene = new QGraphicsScene(this);
QGraphicsView* view = new QGraphicsView(this);
view->setScene(scene);
QWidget* mainWidget = new QWidget(this);
QHBoxLayout *mainLayout = new QHBoxLayout();
QVBoxLayout *buttonLayout = new QVBoxLayout();
QVBoxLayout *vlayout2 = new QVBoxLayout();
vlayout2->addWidget(view);
QPushButton *btn1 = new QPushButton("Rectangle");
btn1->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
QPushButton *btn2 = new QPushButton("Ellipse");
btn2->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
QPushButton *btn3 = new QPushButton("Text");
btn3->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
buttonLayout->addWidget(btn1);
buttonLayout->addWidget(btn2);
buttonLayout->addWidget(btn3);
buttonLayout->addStretch();
mainLayout->addLayout(buttonLayout);
mainLayout->addLayout(vlayout2);
mainWidget->setLayout(mainLayout);
}
Can anyone guide me ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,它应该与我的评论中给出的提示配合使用。
我做了一个 MCVE 来说服自己:
输出:
因此,OP 的代码中一定还有其他内容……
不幸的是,OP 没有公开 MCVE。
因此,尚不清楚 OP 的
Widget
是如何实例化的。是成为主窗口的小部件吗?是否存在另一个小部件,其中Widget
的实例成为其子级?这只是猜测,但后者会解释OP所描述的内容。
为了证实我的猜测,我对上面的代码做了一些修改:
⋮
请注意,
qMain
现在是qMain0
的子级,但是没有布局可以调整的大小当
。qMain0
大小调整时,code>qMain因此,在调整窗口大小时,视图的大小保持初始大小。
Actually, it should work with the hints given in my comments.
I made an MCVE to convince myself:
Output:
Thus, there must be something else in OP's code…
Unfortunately, OP didn't expose an MCVE.
Thus, it's not clear how OP's
Widget
is instanced. Is it the widget which becomes the main window? Is there another widget where theWidget
's instance becomes child of?It's just guessing but the latter would explain what OP described.
To confirm my guess, I modified the above code a bit:
⋮
Please, note that
qMain
is now a child ofqMain0
but there is no layout which adjusts the size ofqMain
whenqMain0
is resized.Hence, the size of view stays the initial size while the window is resized.