基本上我需要一些东西来画它, 这很容易与设计器一起使用(例如,从 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)
关于c++ - Qt: setScreenRect = 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36578948/