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

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

Qtプログラミング-スプレッドシートのセル変更

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

津路です。
今回は、前回までで実装した機能に、セルの値の変更を検知して、閉じる際や別のファイルをオープンする際に、
ユーザに保存するかを尋ねたり、ステータスバーの更新を行います。
まず、ステータスバーを更新する機能です。

void MainWindow::updateStatusBar()
{
    locationLabel->setText(spreadsheet->currentLocation());
    formulaLabel->setText(spreadsheet->currentFormula());
}
QString Spreadsheet::currentLocation() const
{
    return QChar('A'+ currentColumn()) + QString::number(currentRow() + 1);
}
QString Spreadsheet::currentFormula() const
{
    return formula(currentRow(), currentColumn());
}

次に、セル移動、セルの値が変更されたことを検知する機能です。

void MainWindow::createStatusBar()
{
    connect(spreadsheet, SIGNAL(currentCellChanged(int,int,int,int)),this,SLOT(updateStatusBar()));
    connect(spreadsheet, SIGNAL(modified()), this, SLOT(spreadsheetModified()));
}
void MainWindow::spreadsheetModified()
{
    setWindowModified(true);
    updateStatusBar();
}

currentCellChangedは、セル間の移動が行われた際にイベントが起き、ステータスバーを更新します。
modifiedは、値が変更された際にイベントが起き、スプレッドシートに変更があったことを保存します。

void MainWindow::updateStatusBar()
{
    locationLabel->setText(spreadsheet->currentLocation());
    formulaLabel->setText(spreadsheet->currentFormula());
}

setWindowModifiedにて、QMainWindowの状態変更フラグが立ちます。

次に、変更があった状態を検知して、ダイアログ表示します。

bool MainWindow::okToContinue()
{
    if(isWindowModified()) {
	int ret = QMessageBox::warning(this, tr("Spreadsheet"),
		tr("The document has been modified.\n"
		   "Do you want to save your changes?"),
		QMessageBox::Yes | QMessageBox::Default,
		QMessageBox::No,
		QMessageBox::Cancel | QMessageBox::Escape);
	if( ret == QMessageBox::Yes)
	    return save();
 	else if( ret == QMessageBox::Cancel)
	    return false;
    }
    return true;
}
    上に戻る