带有布局、QPushButton 和 QGraphicsItem 的 Qt 应用程序

发布于 2025-01-09 14:23:53 字数 1573 浏览 0 评论 0原文

我正在尝试使用 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.
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不一样的天空 2025-01-16 14:23:53

实际上,它应该与我的评论中给出的提示配合使用。

我做了一个 MCVE 来说服自己:

#include <QtWidgets>

int main(int argc, char **argv)
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // setup GUI
  QWidget qMain;
  qMain.setWindowTitle("Test Box Layout");
  qMain.resize(640, 320);
  QHBoxLayout qHBox;
  QVBoxLayout qVBoxBtns;
  QPushButton qBtnRect("Rectangle");
  qVBoxBtns.addWidget(&qBtnRect);
  QPushButton qBtnCirc("Circle");
  qVBoxBtns.addWidget(&qBtnCirc);
  QPushButton qBtnText("Text");
  qVBoxBtns.addWidget(&qBtnText);
  qVBoxBtns.addStretch();
  qHBox.addLayout(&qVBoxBtns);
  QVBoxLayout qVBoxView;
  QGraphicsView qView;
  qVBoxView.addWidget(&qView, 1);
  qHBox.addLayout(&qVBoxView, 1);
  qMain.setLayout(&qHBox);
  qMain.show();
  // runtime loop
  return app.exec();
}

输出:
快照testQBoxLayout (GIF 动画)

因此,OP 的代码中一定还有其他内容……

不幸的是,OP 没有公开 MCVE
因此,尚不清楚 OP 的 Widget 是如何实例化的。是成为主窗口的小部件吗?是否存在另一个小部件,其中 Widget 的实例成为其子级?

这只是猜测,但后者会解释OP所描述的内容。


为了证实我的猜测,我对上面的代码做了一些修改:

  // setup GUI
  QWidget qMain0; // main window widget
  QWidget qMain(&qMain0); // child widget of main window widget

  qMain.setLayout(&qHBox);
  qMain0.show();
  // runtime loop
  return app.exec();

请注意,qMain 现在是 qMain0 的子级,但是没有布局可以调整 的大小当 qMain0 大小调整时,code>qMain

因此,在调整窗口大小时,视图的大小保持初始大小。

快照(现已损坏)testQBoxLayout(GIF 动画)

Actually, it should work with the hints given in my comments.

I made an MCVE to convince myself:

#include <QtWidgets>

int main(int argc, char **argv)
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // setup GUI
  QWidget qMain;
  qMain.setWindowTitle("Test Box Layout");
  qMain.resize(640, 320);
  QHBoxLayout qHBox;
  QVBoxLayout qVBoxBtns;
  QPushButton qBtnRect("Rectangle");
  qVBoxBtns.addWidget(&qBtnRect);
  QPushButton qBtnCirc("Circle");
  qVBoxBtns.addWidget(&qBtnCirc);
  QPushButton qBtnText("Text");
  qVBoxBtns.addWidget(&qBtnText);
  qVBoxBtns.addStretch();
  qHBox.addLayout(&qVBoxBtns);
  QVBoxLayout qVBoxView;
  QGraphicsView qView;
  qVBoxView.addWidget(&qView, 1);
  qHBox.addLayout(&qVBoxView, 1);
  qMain.setLayout(&qHBox);
  qMain.show();
  // runtime loop
  return app.exec();
}

Output:
Snapshot of testQBoxLayout (GIF Animation)

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 the Widget'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:

  // setup GUI
  QWidget qMain0; // main window widget
  QWidget qMain(&qMain0); // child widget of main window widget

  qMain.setLayout(&qHBox);
  qMain0.show();
  // runtime loop
  return app.exec();

Please, note that qMain is now a child of qMain0 but there is no layout which adjusts the size of qMain when qMain0 is resized.

Hence, the size of view stays the initial size while the window is resized.

Snapshot of (now broken) testQBoxLayout (GIF Animation)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文