c++ - 在 QWidget 内的 QGraphicsScene 内绘制

标签 c++ qt qgraphicsview

我正在尝试创建一个窗口(以 QWidget 的形式),其中包含右侧的菜单和左侧的图形区域。 尽管有许多网站解释了使用 QGraphicsSceneQGraphicsView 的多种方法,但我还是不知道该怎么做。

这里的 main.cpp 被修改为独立工作:

#include <QApplication>
#include <QRectF>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QPushButton>
#include <QVBoxLayout>
#include <QGraphicsRectItem>
#include <QPalette>

int main (int argc, char * argv []) {
    QApplication app (argc, argv) ;

// Main window
    QWidget frame ;
    frame.setFixedSize(750, 550) ;
    frame.show() ;

// Right side, works ok
    QWidget menu (&frame) ;
    menu.setFixedSize(200, 500) ;
    menu.move(550, 10) ;
    QPalette pal = menu.palette() ;
    pal.setColor(QPalette::Background, Qt::red) ;
    menu.setPalette(pal) ; // I expected this to color the whole area 
    // to show the extent of the zone devoted to the menu,
    // but it only outlines the button
    menu.show() ;
    QPushButton button ("Menu", &menu) ;
    button.show() ; // I didn't think this was necessary,
    // but the button didn't appear without this line

// Left side, nothing displayed
    QVBoxLayout layout ;
    QGraphicsScene * scene = new QGraphicsScene () ;
    QGraphicsView view (scene) ;
    layout.addWidget(&view) ;
    QWidget canvas (&frame) ;
    canvas.setLayout(&layout) ;
    // I found this trick to include a QGraphicsScene inside a QWidget,
    // I haven't had the opportunity to see whether it really works.
    scene->addItem(new QGraphicsRectItem(10, 10, 20, 20)) ;
    // The above line has no visible effect
    view.show() ;

    return app.exec() ;
}

我希望这会创建一个窗口,在右侧放置一堆按钮(或者在我提供的 rediced 代码的情况下,只是一个按钮),并在左侧绘制一个矩形,但它离开了整个左侧区域空白。 问题是否来 self 如何将 QGraphicsView 放入 QWidget 中?还是因为其他原因无法绘制?我是否必须更新 QGraphicsView 以反射(reflect)更改?它只是在可见范围之外吗? 最后,绘制失败是否与整个应用程序在 QWidget canvas (&frame) ; when closed 时崩溃有关?

最佳答案

我只是稍微修复了您的程序,以便说明您可以使用 Qt 做什么以及您应该如何使用该框架。

我刚刚将 QPushButton 移动到驻留在 QMenuBar 中的 QAction。 QMenuBar 可以添加到 QMainWindow,这对于普通应用程序来说是合理的。

QMainWindow 的中心部件包含 QGraphicsView。现在,您只是忘记了将 QGraphicsScene 与 QGraphicsView 连接起来。这就是在您看来看不到任何东西的原因。

QGraphicsView 和 QGraphicsScene 只是 MVC 模式的典型示例。您还可以添加另一个 QGraphicsView 并将其连接到同一个 QGraphicsScene。

您还应该使用 new 创建所有对象,因为 Qt 会自动处理 QObject 的所有子对象,如果它被删除或离开范围。

如果您对认真学习 Qt 真的很感兴趣,我建议您创建大量像这样的小示例程序。它真的帮了我很多。

#include <QApplication>
#include <QMenuBar>
#include <QGraphicsView>
#include <QVBoxLayout>
#include <QGraphicsRectItem>
#include <QMainWindow>

int main(int argc, char* argv[]) {
    QApplication app(argc, argv);

    auto mainWindow = new QMainWindow;
    auto menuBar = new QMenuBar;
    auto menu = new QMenu("Menu");
    auto action = new QAction("Action");
    menu->addAction(action);
    menuBar->addMenu(menu);
    mainWindow->setMenuBar(menuBar);

    auto frame = new QFrame;
    frame->setLayout(new QVBoxLayout);
    mainWindow->setCentralWidget(frame);

    auto scene = new QGraphicsScene();
    auto view=new QGraphicsView(scene);
    view->setScene(scene); // That connects the view with the scene
    frame->layout()->addWidget(view);

    QObject::connect(action, &QAction::triggered, [&]() {
        scene->addItem(new QGraphicsRectItem(10, 10, 20, 20));
    });
    mainWindow->show();

    return app.exec();
}

关于c++ - 在 QWidget 内的 QGraphicsScene 内绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57964576/

相关文章:

c++ - 当 CDialog.DoModal() 函数无法创建对话框时?

c++ - 什么是对象切片?

c++ - 无法从 QML 访问 C++ QObject 子类方法

python - 未正确旋转图像

c++ - 测验程序总是评估答案是错误的

qt - QStackedWidget 帮助

c++ - Qt:c++:QTableView不显示我的模型

Qt QGraphicsView 中心的一张背景图像

python - 在 QMainWindow 上显示的自定义 QGraphicsView 中的 PyQT 绘图

c++ - 需要有关类(class)的帮助