【问题标题】:SIGSEGV when using inherited QGraphicsScene使用继承的 QGraphicsScene 时的 SIGSEGV
【发布时间】:2019-05-29 19:20:51
【问题描述】:

我正在尝试从 QGraphicsScene 继承,但是当我尝试使用它时,它会以某种方式使我的程序崩溃。我想在某个地方我错过了一件小事,但我无法弄清楚。这是迄今为止我尝试过的一个最小示例:

myscene.h

#ifndef MYSCENE_H
#define MYSCENE_H

#include <QGraphicsScene>

class MyScene : public QGraphicsScene
{
    Q_OBJECT

    QPen* pen_bg;

public:
    explicit MyScene(QObject* parent=nullptr);
    ~MyScene();
};

#endif // MYSCENE_H

myscene.cpp

#include "myscene.h"

MyScene::MyScene(QObject* parent):
    QGraphicsScene (parent)
{
    pen_bg = new QPen(Qt::blue);
}

MyScene::~MyScene()
{
    delete pen_bg;
}

ma​​inwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myscene.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{    
    MyScene *m_scene = new MyScene(this);
    ui->graphicView->setScene(m_scene);
}

只要我调用setScene(m_scene) 函数,程序就会崩溃并显示 SIGSEGV。

【问题讨论】:

    标签: c++ qt inheritance qwidget qgraphicsscene


    【解决方案1】:

    如果使用UI文件,使用前需要先调用setupUi(),否则UI元素没有初始化。请参阅文档:http://doc.qt.io/qt-5/designer-using-a-ui-file.html

    添加

     ui->setupUi(this); 
    

    到您的MainWindow 构造函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 2016-08-15
      • 2012-07-17
      相关资源
      最近更新 更多