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

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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 15 Feb 2016, 00:36 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 15 Feb 2016, 11:56 last edited by Tyras
      #5
      This post is deleted!
      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tyras
        wrote on 15 Feb 2016, 13:22 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 Feb 2016, 22:15 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 16 Feb 2016, 13:43 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 18 Feb 2016, 00:23 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...

              K 1 Reply Last reply 18 Feb 2016, 10:25
              0
              • T Tyras
                18 Feb 2016, 00:23

                @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...

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 18 Feb 2016, 10:25 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 18 Feb 2016, 12:10
                1
                • K kshegunov
                  18 Feb 2016, 10:25

                  @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 18 Feb 2016, 12:10 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 =/

                  K 1 Reply Last reply 18 Feb 2016, 12:36
                  0
                  • T Tyras
                    18 Feb 2016, 12:10

                    @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 =/

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 18 Feb 2016, 12:36 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
                    • K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 18 Feb 2016, 13:48 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 18 Feb 2016, 14:18
                      0
                      • K kshegunov
                        18 Feb 2016, 13:48

                        @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 18 Feb 2016, 14:18 last edited by
                        #14

                        @kshegunov

                        Will do. Thanks =)

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          Tyras
                          wrote on 18 Feb 2016, 14:59 last edited by
                          #15

                          Done.

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

                          1 Reply Last reply
                          0

                          13/15

                          18 Feb 2016, 13:48

                          • Login

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