【问题标题】:Draw inside a QGraphicsScene inside a QWidget在 QWidget 内的 QGraphicsScene 内绘制
【发布时间】:2020-01-17 17:55:45
【问题描述】:

我试图让一个窗口(以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() ;
}

我希望这会创建一个窗口,在右侧放置一堆按钮(或者在我提供的简化代码的情况下,只有一个按钮),然后在左侧绘制一个矩形,但它离开了整个左侧区域空白。 问题是否来自我如何将 QGraphicsView 放在 QWidget 中?还是因为别的原因没画好?我必须更新QGraphicsView 以反映更改吗?它只是超出了可见范围吗? 最后,绘制失败是否与整个应用程序在关闭时在QWidget canvas (&amp;frame) ; 线上崩溃的事实有关?

【问题讨论】:

    标签: c++ qt qgraphicsview


    【解决方案1】:

    我刚刚对您的程序进行了一些修改,以便说明您可以使用 Qt 做什么以及您应该如何使用该框架。

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

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

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

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

    如果你真的对认真学习 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多