0

(テキストボックス)、(でタブを作成するために使用される)、ファイルのアドレスを保持するための、およびファイルが保存/変更されたかどうかを確認するためのクラスを呼び出しFileます。QTextEdit*QWidget*QTabWidgetQStringbool

これらのファイルを に保存しますQList( のようなものstd::listです)。

void MainWindow::newFile()
{
    QWidget* tab = new QWidget( tabWidget ); //QTabWidget is a member of MainWindow, it gets automatically deleted when MainWindow gets destryoed
    tabWidget->addTab( tab, tr("New Tab") ); //Add it to the QTabWidget
    tab->setLayout( new QHBoxLayout( tab ) );

    QTextEdit* textEdit = new QTextEdit( tab ); //When tab gets deleted, this also gets deleted
    tab->layout()->addWidget( textEdit );

    File *f = new File( this, textEdit, tab ); //Store both in a file
    fileList.push_back( f ); //Add it to the list (member of MainWindow)

    tabWidget->setCurrentIndex( tabWidget->count()-1 ); //Go to that tabPage
    f->textEdit()->setFocus(); //Set the focus to the textEdit
}

これにより、新しいタブページが作成され、 に追加され、タブページのレイアウトが作成さQTabWidgetれ、新しい が作成QTextEditされ、レイアウトに追加されて塗りつぶされ、自動的にサイズが変更され、最後に に押し戻されるQListので、タブなどを削除できます。

クラスFileにはメンバーとしてポインターがあるため、ヒープ上のオブジェクトを削除する必要があります。しかし、デストラクタでそれらを削除しようとすると、SIGSEGV が発生し、プログラムがクラッシュします。

File::~File()
{
delete m_textEdit; //QTextEdit* - Crashes here
delete m_tab; //QWidget* //Doesn't reach this line
}

m_textEdit私の質問は、のオブジェクトを削除しようとすると、プログラムがクラッシュするのはなぜですか? これは、ウィンドウを閉じたときにのみ発生します。

また、リストからファイルを削除する場合、ファイル* を削除する必要がありますか? または、リストは自動的にそれを行いますか? もしそうなら、どうすればいいですか?

//delete fileList.at(0); //This would cause a crash
fileList.removeAt( 0 ); //removing for example the first index

編集:のヘッダーファイルFile

class File : public QObject
{
    Q_OBJECT

    QTextEdit* m_textEdit;
    QWidget* m_tab;
    QString m_filepath;
    bool m_saved;

public:
    explicit File(QObject *parent = 0);
    File( const File& );
    File( QObject* parent, QTextEdit* textEdit = 0, QWidget* tab = 0, const QString& filepath = QString(), bool saved = true );
    ~File();

signals:
    void savedChanges( bool );

public slots:
 //Getters and setters only
};
4

1 に答える 1