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. QTextCursor and CSS: Problem with text-indent property
QtWS25 Last Chance

QTextCursor and CSS: Problem with text-indent property

Scheduled Pinned Locked Moved Unsolved General and Desktop
cssqtextcursor
15 Posts 3 Posters 6.1k Views
  • 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.
  • T Offline
    T Offline
    Tyras
    wrote on last edited by Tyras
    #1

    I'm building a QTextDocument using QTextCursor and setHtml. I want to indent particular lines, while leaving some unindented.

    So, I've setted the following String as the document's default CSS:

    const QString css = "p.indented{text-indent: 10px} p.normal{text-indent: 0px}";
    

    and, when building the strings to use with setHtml():

    QString indentText = "<p class=\"indented\">indented Text</p>";
    QString normalText = "<p class=\"normal\">normal Text</p>";
    

    I expected to "indentText" to be indented and "normalText" to stay unindented regardless of the insertion order... But what happens is that the first identation rule to be inserted is applied to all lines inserted after it, even if they have their own text-indent properties.

    Is this the expected behaviour in QT?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you show an sample code setting up the css as well as the QTextDocument content ? That way it will be reproducible and testable.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tyras
        wrote on last edited by
        #3

        Sure:

        QTextDocument *doc = new QTextDocument();
        QTextCursor cursor(doc);
        QTextBlockFormat format;
        
        doc->setDefaultStyleSheet(css);
        
        while(!loglines->isEmpty())
        {
        	cursor.insertHtml(loglines->takeFirst());
        	cursor.insertBlock();
        }
        

        "loglines" is a QVector<QString> containg the preformatted lines in HTML

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Using:

          QApplication app(argc, argv);
          QTextEdit te;
          QTextDocument *doc = te.document();
          QTextCursor cursor(doc);
          QTextBlockFormat format;
          
          const QString css = "p.indented{text-indent: 10px} p.normal{text-indent: 0px}";
          doc->setDefaultStyleSheet(css);
          QVector<QString> loglines = {"<p class=\"indented\">indented Text</p>", "<p class=\"normal\">normal Text</p>"};
          while(!loglines.isEmpty()) {
              cursor.insertHtml(loglines.takeFirst());
              cursor.insertBlock(format);
          }
          
          te.show();
          return app.exec();
          

          works fine

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tyras
            wrote on last edited by Tyras
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • T Offline
              T Offline
              Tyras
              wrote on last edited by Tyras
              #6

              I tried your code again in a standalone project and it worked right.

              Looks like it's something related to multithreading.

              You see, My application works with big documents (5000+ lines), So, to avoid GUI freezing, I do the QTextDocument building in another thread, and send it to the GUI thread through signal/slot.

              So, my "Worker Class" has a slot:

              const QString css = "p.indented{text-indent: 10px} p.normal{text-indent: 0px}";
              
              void JsonParser::buildDoc()
              {
              	QTextDocument *doc = new QTextDocument();
              	QTextCursor cursor(doc);
              
              	doc->setDefaultStyleSheet(css);
              
              	qDebug() << "[JsonParser] Lines translated, building document...";
              
              	QVector<QString> loglines = {"<p class=\"indented\">indented Text</p>", "<p class=\"normal\">normal Text</p>"};
              
              	while(!loglines.isEmpty())
              	{
              		cursor.insertHtml(loglines.takeFirst());
              		cursor.insertBlock();
              	}
              
              	emit processingFinished(doc);
              }
              

              the processingFinished() signal is connected to this slot in my MainWindow Class:

              void MainWindow::logReady(QTextDocument *log)
              {
              	ui->logViewer->setUpdatesEnabled(false);
              	ui->logViewer->setDocument(log->clone(ui->logViewer));
              	ui->logViewer->setUpdatesEnabled(true);
              
              	ui->mainToolBar->setEnabled(true);
              	ui->actionAbrir_Log->setEnabled(true);
              }
              

              Am I missing something?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Yes, there's a small but yet important difference: you're not reseting the format between two lines.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • T Offline
                  T Offline
                  Tyras
                  wrote on last edited by Tyras
                  #8

                  True, I didn't notice that, sorry!

                  But looks like it only works for the first two lines...

                  Even your code, if I add two more lines, like:

                  QApplication app(argc, argv);
                  QTextEdit te;
                  QTextDocument *doc = te.document();
                  QTextCursor cursor(doc);
                  QTextBlockFormat format;
                  
                  const QString css = "p.indented{text-indent: 10px} p.normal{text-indent: 0px}";
                  doc->setDefaultStyleSheet(css);
                  QVector<QString> loglines = {"<p class=\"indented\">indented Text</p>", "<p class=\"normal\">normal Text</p>", "<p class=\"indented\">indented Text</p>", "<p class=\"normal\">normal Text</p>"};
                  
                  while(!loglines.isEmpty()) {
                  	cursor.insertHtml(loglines.takeFirst());
                  	cursor.insertBlock(format);
                  }
                  
                  te.show();
                  return app.exec();
                  

                  It shows: screenshot

                  could you confirm this, @SGaist ?

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Tyras
                    wrote on last edited by
                    #9

                    @SGaist

                    I just tried the code in the post above in a Linux VM, and it happens what I described above...

                    I'm pretty much at a stalemate. Cant see if I'm missing something more, of if I should just file a bug...

                    kshegunovK 1 Reply Last reply
                    0
                    • T Tyras

                      @SGaist

                      I just tried the code in the post above in a Linux VM, and it happens what I described above...

                      I'm pretty much at a stalemate. Cant see if I'm missing something more, of if I should just file a bug...

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @Tyras
                      File a bug! I spent some time last night debugging that snippet. I couldn't tell why exactly is not working, but from what I saw you shouldn't be required to insertBlock() even. The parser recognizes the <p> that as a block element and should supposedly insert it for you.
                      Unfortunately, I doubt you'll get much attention on that bug. If I had to guess, the code for the QTextDocument class predates even Qt4. Looking at the internal structure, in my opinion, a major refactoring is long overdue.

                      Kind regards.

                      Read and abide by the Qt Code of Conduct

                      T 1 Reply Last reply
                      1
                      • kshegunovK kshegunov

                        @Tyras
                        File a bug! I spent some time last night debugging that snippet. I couldn't tell why exactly is not working, but from what I saw you shouldn't be required to insertBlock() even. The parser recognizes the <p> that as a block element and should supposedly insert it for you.
                        Unfortunately, I doubt you'll get much attention on that bug. If I had to guess, the code for the QTextDocument class predates even Qt4. Looking at the internal structure, in my opinion, a major refactoring is long overdue.

                        Kind regards.

                        T Offline
                        T Offline
                        Tyras
                        wrote on last edited by Tyras
                        #11

                        @kshegunov

                        That's bad news for me...

                        You see, If I don't use html/css, do the formatting using the QTextCharFormat and QTextBlockFormat and use insertText it works fine, but looks like I can't edit a single QTextDocument from multiple threads.

                        And I can't create nor insert premade QTextBlocks in a QTextDocument, so, the only option to do multithreading editing of the document is using html =/

                        kshegunovK 1 Reply Last reply
                        0
                        • T Tyras

                          @kshegunov

                          That's bad news for me...

                          You see, If I don't use html/css, do the formatting using the QTextCharFormat and QTextBlockFormat and use insertText it works fine, but looks like I can't edit a single QTextDocument from multiple threads.

                          And I can't create nor insert premade QTextBlocks in a QTextDocument, so, the only option to do multithreading editing of the document is using html =/

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by kshegunov
                          #12

                          @Tyras said:

                          but looks like I can't edit a single QTextDocument from multiple threads

                          Why should that be?

                          And I can't create nor insert premade QTextBlocks in a QTextDocument, so, the only option to do multithreading editing of the document is using html =/

                          Look up, I see no reason why you wouldn't be able to do this. Also I don't really know why would you want to thread the document editing, but that's an entirely different affair.

                          Kind regards.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          0
                          • kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by
                            #13

                            @Tyras

                            PS.
                            If you wish, you could open another thread in the forum (as the issue of building the document with threaded code is really outside the scope of this discussion) and we can explore the issue there?

                            Read and abide by the Qt Code of Conduct

                            T 1 Reply Last reply
                            0
                            • kshegunovK kshegunov

                              @Tyras

                              PS.
                              If you wish, you could open another thread in the forum (as the issue of building the document with threaded code is really outside the scope of this discussion) and we can explore the issue there?

                              T Offline
                              T Offline
                              Tyras
                              wrote on last edited by
                              #14

                              @kshegunov

                              Will do. Thanks =)

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                Tyras
                                wrote on last edited by
                                #15

                                Done.

                                https://forum.qt.io/topic/64346/qtextdocument-and-multithreading

                                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