Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Wrong value for ui->textEdit->document()->isModified()

Wrong value for ui->textEdit->document()->isModified()

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    silviubogan
    wrote on last edited by
    #1

    I have created a new Qt Widgets Application project with a MainWindow class and a MainWindow.ui file. In MainWindow.ui I've added a QTextEdit widget with its objectName set to textEdit. MainWindow.cpp has the following content:

    @#include "MainWindow.h"
    #include "ui_MainWindow.h"

    #include <QDebug>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    connect(ui->textEdit->document(), &QTextDocument::modificationChanged,
            this, &MainWindow::on_textEditDocumentModificationChanged);
    
    qDebug() << "before clear():" <<
                ui->textEdit->document()->isModified(); // false
    
    qDebug() << "ui->textEdit->clear():";
    ui->textEdit->clear();
    
    qDebug() << "after clear():" <<
                ui->textEdit->document()->isModified() << // false
                "  [Why here it's false?]";
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_textEditDocumentModificationChanged(bool modified)
    {
    qDebug() << " on_textEditDocumentModificationChanged:" <<
    modified << ui->textEdit->document()->isModified(); // true true
    }@

    The on_textEditDocumentModificationChanged slot is called with a 'true' parameter and inside that slot @ui->textEdit->document()->isModified()@ has the value 'true', because calling the clear() method changes the modified status of the document to true, but when the execution returns to the MainWindow constructor, ui->textEdit->document()->isModified() has the value 'false'. I am interested if anyone can reproduce this issue and if there is a solution to this problem.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      I suspect the textEdit is empty before you call clear(), right? In that case I would consider that to be expected behaviour.

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        silviubogan
        wrote on last edited by
        #3

        Yes but in on_textEditDocumentModificationChanged slot the line @qDebug() << " on_textEditDocumentModificationChanged:" <<
        modified << ui->textEdit->document()->isModified(); // true true@ prints "true true".

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          I would think that it does change the status while checking for changes (when clearing). I understand this might be strange at times, but to me it still looks like a valid behaviour ;)

          What happens if you set some text in your textEdit before clearing?

          (Z(:^

          1 Reply Last reply
          0
          • S Offline
            S Offline
            silviubogan
            wrote on last edited by
            #5

            I set some text in the textEdit before clearing and it's the same situation with the same output text.

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              OK, now it finally seems a bit weird to me :-) Sorry it took so long.

              But, when you set the text, does it still show as isModified == false before the clearing?

              Quoting from the documentation:
              [quote]This property holds whether the document has been modified by the user.[/quote]

              I suspect that is the key here: it will return true when modified by the user, but not when being modified programatically. In that light, I think the fact that the state changes briefly when clear() is called is a bug, please report it.

              (Z(:^

              1 Reply Last reply
              0
              • S Offline
                S Offline
                silviubogan
                wrote on last edited by
                #7

                In the end I added just below this line:

                @ui->textEdit->clear();@

                the following line:

                @ui->textEdit->document()->setModified(true);@

                This simulates the expected behavior.

                I think that, when I have enough time for this, I will report this bug.

                Thank you for your messages!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved