QT 5:closeEventを書き換える.

5222 ワード

くだらないことを言わないでコードを見てください.
 #include <QMainWindow>
#include <memory>
#include <QTextEdit>
#include <QCloseEvent>
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow()=default;
private:
    void open()noexcept;
    void save()noexcept;
    std::shared_ptr<QAction> openAction;
    std::shared_ptr<QAction> saveAction;
    std::shared_ptr<QMenu> openMenu;
    std::shared_ptr<QToolBar> theToolBar;
    std::shared_ptr<QDockWidget> dockWidget;
    std::shared_ptr<QTextEdit> textEdit;
protected:
    virtual void closeEvent(QCloseEvent* ev)override;
};
 #include "mainwindow.h"
#include <QMessageBox>
#include <QStatusBar>
#include <QMenuBar>
#include <QToolBar>
#include <QDockWidget>
#include <QDebug>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    this->setWindowTitle(tr("shihuawoaini [*]")); // [*] (placeholder).
    this->setMinimumSize(500, 400);
    qDebug()<<iconSize();
    this->openAction = std::shared_ptr<QAction>(new QAction(tr("&open"), this));
    (this->openAction)->setShortcut(QKeySequence::Open); // .
    (this->openAction)->setStatusTip(tr("open..."));
    connect((this->openAction).get(), &QAction::triggered, this, &MainWindow::open);
    this->saveAction = std::shared_ptr<QAction>(new QAction(tr("&save"), this));
    saveAction->setShortcut(QKeySequence::Save); // .
    saveAction->setStatusTip(tr("save..."));
    connect(saveAction.get(), &QAction::triggered, this, &MainWindow::save);
    this->openMenu = std::shared_ptr<QMenu>(menuBar()->addMenu(tr("open")));
    (this->openMenu)->addAction(this->openAction.get());
    this->theToolBar = std::shared_ptr<QToolBar>(this->addToolBar(tr("File")));
    (this->theToolBar)->addAction(this->openAction.get());
    theToolBar->addAction(saveAction.get());
    this->dockWidget = std::shared_ptr<QDockWidget>(new QDockWidget(tr("DockWidget")));
    this->addDockWidget(Qt::LeftDockWidgetArea, (this->dockWidget).get());
    this->statusTip();
    this->statusBar();
    this->textEdit = std::shared_ptr<QTextEdit>(new QTextEdit(this));
    this->setCentralWidget(textEdit.get());
    connect(textEdit.get(), &QTextEdit::textChanged, [=](){ this->setWindowModified(true); }); // !!!!!!!!!!!!!!!!!!!!! 
}
void MainWindow::open()noexcept
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("open a file."), ".", tr("Text files(*.txt)"));
    if(!fileName.isEmpty()){
        QFile file(fileName);
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
            QMessageBox::warning(this, tr("Read File"), tr("Can not open file.
%1").arg(fileName));             return;
        }else{
            QTextStream readFile(&file);
            textEdit->setText(readFile.readAll());
            file.close();
        }
    }else{
        QMessageBox::warning(this, tr("Path"), tr("You do not select any file."));
    }
}
void MainWindow::save()noexcept
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("save file"),".", tr("Text files(*.txt);;Images (*.png *.jpg)"));
    if(!fileName.isEmpty()){
        QFile file(fileName);
        if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
            QMessageBox::warning(this, tr("write file"), tr("open nothing"));
            return;
        }
        QTextStream writeFile(&file);
        writeFile<<(this->textEdit)->toPlainText();
        file.close();
    }else{
        QMessageBox::warning(this, tr("write fail"), tr("nothing for writing"));
    }
}
void MainWindow::closeEvent(QCloseEvent* ev) // !!!!!!!!!!!!!!!!! 
{
    if(this->isWindowModified()){
        bool boolean = QMessageBox::question(this, tr("Close Window"), tr("Do you want exit?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes;
        qDebug()<<boolean;
        if(boolean){
            ev->accept();
        }else{
            ev->ignore();
        }
    }else{
        ev->accept();
    }