大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

Qtプログラミング – スプレッドシート新規、閉じる

株式会社クローバーフィールドの経営理念
著者:津路高広
公開日:2019/12/30
最終更新日:2019/12/30
カテゴリー:技術情報

津路です。
本日は、既に令和元年12月30日です。今年も雪降らず、静かに暮を迎えようとしています。
今回は、連続して投稿しているスプレッドシートアプリケーションのファイルメニューに、新規、閉じる、終了の3つを追加します。

今回からは、今までのような、SDIではなく、MDIに切り替えます。

SDI
void MainWindow::newFile()
{
    spreadsheet->clear();
}

MDI
void MainWindow::newFile()
{
    MainWindow *mainwin = new MainWindow;
    mainwin->show();
}

上記のように、MDIでは、MainWindowを生成して、表示しますので、メニュー付きのスプレッドシートが新たに開きます。

閉じるメニューでは、closeスロットを呼び出し、以前取り上げたcloseEventにて、イベントを処理し、ウィンドウを閉じて破棄します。

void MainWindow::createActions()
{
    closeAction = new QAction(tr("&Close"),this);
    closeAction->setShortcut(tr("Ctrl+W"));
    closeAction->setStatusTip(tr("Close this window"));
    connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
}

メモリ破棄を行う便利な機能として、Qt::WA_DeleteOnClose という属性があります。

MainWindow::MainWindow()
{
    spreadsheet = new Spreadsheet;
    setCentralWidget(spreadsheet);
    setWindowIcon(QIcon(":/images/inspect.png"));
    setAttribute(Qt::WA_DeleteOnClose);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
    if(okToContinue())
    {
	event->accept();
    } else
    {
   	event->ignore();
    }
}

終了メニューでは、アプリケーションを終了します。すべてのウィンドウを閉じてから終了します。

void MainWindow::createActions()
{
    exitAction = new QAction(tr("E&xit"),this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setStatusTip(tr("Exit the application"));
    connect(exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
}
void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(newAction);
    fileMenu->addAction(openAction);
    fileMenu->addAction(saveAction);
    fileMenu->addSeparator();
    fileMenu->addAction(closeAction);
    fileMenu->addAction(exitAction);
}

さて、起動して、すぐにctrl+Qにて終了します、とエラーが発生します。
free: invalid pointer : メモリアドレス
これは、終了時にメモリが無効であることを意味します。
新たに加えた属性の設定方法が間違っています。
正しくは、spreadsheet->setAttribute(Qt::WA_DeleteOnClose);
です。

ファイルメニュー


エディットメニュー

エディットメニュー

    上に戻る