【发布时间】:2020-01-17 17:55:45
【问题描述】:
我试图让一个窗口(以QWidget 的形式)由右侧的菜单和左侧的图形区域组成。
尽管有许多网站解释了QGraphicsScene 和QGraphicsView 的多种使用方法,但我就是不知道该怎么做。
这里的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 (&frame) ; 线上崩溃的事实有关?
【问题讨论】:
标签: c++ qt qgraphicsview