c++ - Qt: setScreenRect = 段错误

标签 c++ qt

基本上我需要一些东西来画它, 这很容易与设计器一起使用(例如,从 QWidget 继承和覆盖 paintEvent 需要一些努力才能在 Qt 设计器中使用它)。

所以我将 QGraphicsView 添加到 .ui 文件并在按钮方法中编写这样的代码:

QGraphicsScene *scene = ui->graphicsView->scene();    
const QRectF scene_r = ui->graphicsView->sceneRect();
//here I see that scene_r == {0, 0, 0, 0};

然后我添加这样的代码:

const QSize draw_size = ui->graphicsView->size();
//draw_size == {100, 100}
scene->setSceneRect(QRectF(0, 0, draw_size.width(), draw_size.height()));

并在 scene->setSceneRect 行出现段错误这是怎么回事?

其实我想要这样的代码:

//scene size == draw_size
QPixmap pix(draw_size.width(), draw_size.height());
//fill pix with external program
scene->addPixmap(map_pix);

这可能吗?

我以前从未使用过 QGraphicsView。

最佳答案

出现段错误的原因是您没有为 View 设置任何场景,因此 scene() 将返回一个空指针。

QGraphicsScene scene;
QGraphicsView view(&scene);
view.show();

另一种方法是创建没有场景的 View ,然后再添加它:

void    setScene(QGraphicsScene *scene)

引用: http://doc.qt.io/qt-5/qgraphicsview.html

关于c++ - Qt: setScreenRect = 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36578948/

相关文章:

c++ - 从 getchar 读取输入

c++ - 如何用 C++ 扩展 python?

qt - QML 几个具有主动焦点/键盘快捷键控制的项目

c++ - 我有一个使用 openCV 检测人脸的视频流,我怎么能捕捉到离相机最近的人脸并将其保存到图像文件中呢?

c++ - 在 C++14 中,可恢复函数将在什么情况下执行?

c++ - 使用求解器 SparseLU 并出现错误 C2664

c++ - 如何从 QGraphicsView 中检索选定区域?

c++ - JumpList 将无法使用 QWinJumpList

c++ - 使用动态插件处理 Qt5 中的 QMetaType 注册

c++ - 将 3d 模型加载到 OpenGL 场景中