isWindowModified is no effect



  • I create a winow called NoteBook and include a textedit object.Create an action called new and connect a slot called newFile.```
    //
    void MainWindow::newFile()
    {
    if(okTocontinue())
    {
    _textedit->clear();
    setCurrentFile("");
    }
    }
    bool MainWindow::okTocontinue()
    {
    if (isWindowModified()) {
    qDebug()<<" modify";
    int r = QMessageBox::warning(this, tr("NoteBook"),
    tr("The document has been modified.\n"
    "Do you want to save your changes?"),
    QMessageBox::Yes | QMessageBox::No
    | QMessageBox::Cancel);
    if (r == QMessageBox::Yes) {
    return saveFile();
    } else if (r == QMessageBox::Cancel) {
    return false;
    }
    }
    qDebug()<<"not modify";
    return true;
    }

    I have typed some character in textedit,but it always debug "not modify.I donot know what happened. Thank for your help.

  • Moderators

    @chaochao As far as I know you have to call setWindowModified() whenever your document is modified. MainWindow cannot know when it is modified as it can contain many widgets and it does not know what the logic of your application is. See this example: http://doc.qt.io/qt-5/qtwidgets-mainwindows-application-example.html

    MainWindow::MainWindow()
        : textEdit(new QPlainTextEdit)
    {
        connect(textEdit->document(), &QTextDocument::contentsChanged,
                this, &MainWindow::documentWasModified);
    }
    void MainWindow::documentWasModified()
    {
        setWindowModified(textEdit->document()->isModified());
    }
    


  • @jsulm very thanks,that's very good.When I see this example, I find what i want to realise is similar with that.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.