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. How to style a table in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

How to style a table in QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
30 Posts 5 Posters 6.1k Views 3 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.
  • H Offline
    H Offline
    HenkCoder
    wrote on last edited by
    #1

    Hello,
    How do I change the stylesheet to a table that I insert in the QTextEdit?
    I've read this article: https://www.w3schools.com/Css/css_table.asp but edit->setStyleSheet("QTextEdit:table{border-collapse: collapse;}"); doesn't seem to work.
    Is there a way I can achieve this?
    Thanks.

    raven-worxR JonBJ 2 Replies Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #29
      QTextLength oneThird = QTextLength(QTextLength::PercentageLength, 33);
      QVector<QTextLength> constraints(3, oneThird);
      format.setColumnWidthConstraints(constraints);
      

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

      H 1 Reply Last reply
      2
      • H HenkCoder

        Hello,
        How do I change the stylesheet to a table that I insert in the QTextEdit?
        I've read this article: https://www.w3schools.com/Css/css_table.asp but edit->setStyleSheet("QTextEdit:table{border-collapse: collapse;}"); doesn't seem to work.
        Is there a way I can achieve this?
        Thanks.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #2

        @HenkCoder
        You must set the stylesheet with QTextEdit::setDefaultStyleSheet() to apply a stylesheet to the contents.
        With QTextEdit::setStyleSheet() you set the Qt stylesheet style to the widget itself.

        Further note only the following CSS subset is supported:
        https://doc.qt.io/qt-5/richtext-html-subset.html

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        H 1 Reply Last reply
        1
        • raven-worxR raven-worx

          @HenkCoder
          You must set the stylesheet with QTextEdit::setDefaultStyleSheet() to apply a stylesheet to the contents.
          With QTextEdit::setStyleSheet() you set the Qt stylesheet style to the widget itself.

          Further note only the following CSS subset is supported:
          https://doc.qt.io/qt-5/richtext-html-subset.html

          H Offline
          H Offline
          HenkCoder
          wrote on last edited by
          #3

          @raven-worx Hello, thank you for your answer.
          I checked the link you sent me and table and border-collapse are supported by Qt's rich text engine but I don't know how to apply the style.
          I tried with the following:

              edit->document()->setDefaultStyleSheet("QTextDocument:table{width: 100%; border-collapse: collapse}");
          

          This is the result:
          dbc07f6f-4690-4e8a-8198-baf976cb66e9-image.png
          border-collapse should make this effect tho:
          e00fc4ec-d8f6-4846-8abf-7f2df759a76c-image.png

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mchinand
            wrote on last edited by
            #4

            @HenkCoder said in How to style a table in QTextEdit:

            edit->document()->setDefaultStyleSheet("QTextDocument:table{width: 100%; border-collapse: collapse}");

            Do you still get the same result if you leave off 'QTextDocument'? I think it is expecting a purely CSS stylesheet, not QSS.

            H 1 Reply Last reply
            1
            • M mchinand

              @HenkCoder said in How to style a table in QTextEdit:

              edit->document()->setDefaultStyleSheet("QTextDocument:table{width: 100%; border-collapse: collapse}");

              Do you still get the same result if you leave off 'QTextDocument'? I think it is expecting a purely CSS stylesheet, not QSS.

              H Offline
              H Offline
              HenkCoder
              wrote on last edited by
              #5

              @mchinand Yep, I tried and it's still the same.

              1 Reply Last reply
              0
              • H HenkCoder

                Hello,
                How do I change the stylesheet to a table that I insert in the QTextEdit?
                I've read this article: https://www.w3schools.com/Css/css_table.asp but edit->setStyleSheet("QTextEdit:table{border-collapse: collapse;}"); doesn't seem to work.
                Is there a way I can achieve this?
                Thanks.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #6

                @HenkCoder said in How to style a table in QTextEdit:

                QTextEdit:table{border-collapse: collapse;}

                QTextDocument:table{width: 100%; border-collapse: collapse}

                You may know more than I, but where did you get that QTextEdit:table (or QTextDocument:table) syntax from?

                H 1 Reply Last reply
                1
                • M Offline
                  M Offline
                  mchinand
                  wrote on last edited by
                  #7

                  Can you share a minimal full project that has your current code for applying a stylesheet to a QTextDocument?

                  H 1 Reply Last reply
                  0
                  • M mchinand

                    Can you share a minimal full project that has your current code for applying a stylesheet to a QTextDocument?

                    H Offline
                    H Offline
                    HenkCoder
                    wrote on last edited by
                    #8

                    @mchinand I can't find any, that's the problem, I tried to apply that style sheet with my knowledge in CSS

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @HenkCoder said in How to style a table in QTextEdit:

                      QTextEdit:table{border-collapse: collapse;}

                      QTextDocument:table{width: 100%; border-collapse: collapse}

                      You may know more than I, but where did you get that QTextEdit:table (or QTextDocument:table) syntax from?

                      H Offline
                      H Offline
                      HenkCoder
                      wrote on last edited by
                      #9

                      @JonB
                      Hello, I didn't find anywhere that code, I just wrote that because in all of the other style sheet that I've used the syntax was like this but I don't knoe.

                      JonBJ 1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mchinand
                        wrote on last edited by
                        #10

                        I meant your current code. Post it here. Not just the single line calling setDefaultStyleSheet().

                        H 1 Reply Last reply
                        0
                        • H HenkCoder

                          @JonB
                          Hello, I didn't find anywhere that code, I just wrote that because in all of the other style sheet that I've used the syntax was like this but I don't knoe.

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by JonB
                          #11

                          @HenkCoder said in How to style a table in QTextEdit:

                          Hello, I didn't find anywhere that code, I just wrote that because in all of the other style sheet that I've used the syntax was like this but I don't knoe.

                          That's not a very good start. If you don't know whether the selector works it doesn't matter what you write inside it.

                          Apart from the fact that Qt documents what subset it uses, what CSS /"all of the other style sheet that I've used" uses the : where you do? (Other than a few pseudo-selectors like a:link, which is quite different from your case.)

                          H 1 Reply Last reply
                          0
                          • M mchinand

                            I meant your current code. Post it here. Not just the single line calling setDefaultStyleSheet().

                            H Offline
                            H Offline
                            HenkCoder
                            wrote on last edited by HenkCoder
                            #12

                            @mchinand

                                //Setting up the TextEdit
                                QString fontName{obj["font"].toObject()["family"].toString()};
                                int fontSize{obj["font"].toObject()["pointSize"].toInt()};
                                ui->tabWidget->addTab(new TextEdit(), QString("New Document " + QString::number(ui->tabWidget->count() + 1)));
                                ui->tabWidget->setCurrentIndex(ui->tabWidget->count() - 1);
                                TextEdit *edit{getTabTextEdit()};
                                edit->setGeometry(0,0,ui->tabWidget->width() - 3, ui->tabWidget->height() - 59);
                                edit->setTabStopDistance(32);
                                edit->setFocus();
                                if(foreRed + foreGreen + foreBlue != 0)
                                    edit->setTextColor(QColor(foreRed, foreGreen, foreBlue));
                                if(backRed + backGreen + backBlue != 765)
                                    edit->setTextBackgroundColor(QColor(backRed, backGreen, backBlue));
                                edit->setFontPointSize(fontSize);
                                edit->document()->setDefaultStyleSheet("QTextDocument:table{width: 100%; border-collapse: collapse;}");
                                edit->setFontFamily(fontName);
                                ui->fontComboBox->setCurrentFont(QFont(fontName));
                                ui->fontsize->setValue(fontSize);
                                QObject::connect(edit, &TextEdit::textChanged, this, &MainWindow::edit_changed);
                                QObject::connect(edit, &TextEdit::cursorPositionChanged, this, &MainWindow::cursorPosChanged);
                            
                            

                            Do you want the full constructor code?

                            M 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @HenkCoder said in How to style a table in QTextEdit:

                              Hello, I didn't find anywhere that code, I just wrote that because in all of the other style sheet that I've used the syntax was like this but I don't knoe.

                              That's not a very good start. If you don't know whether the selector works it doesn't matter what you write inside it.

                              Apart from the fact that Qt documents what subset it uses, what CSS /"all of the other style sheet that I've used" uses the : where you do? (Other than a few pseudo-selectors like a:link, which is quite different from your case.)

                              H Offline
                              H Offline
                              HenkCoder
                              wrote on last edited by
                              #13

                              @JonB
                              My bad sorry, I used a few times :: for example in the tabWidget one.
                              QTabWidget::tab I tried again with that but it still doesn't work.

                              JonBJ 1 Reply Last reply
                              0
                              • H HenkCoder

                                @mchinand

                                    //Setting up the TextEdit
                                    QString fontName{obj["font"].toObject()["family"].toString()};
                                    int fontSize{obj["font"].toObject()["pointSize"].toInt()};
                                    ui->tabWidget->addTab(new TextEdit(), QString("New Document " + QString::number(ui->tabWidget->count() + 1)));
                                    ui->tabWidget->setCurrentIndex(ui->tabWidget->count() - 1);
                                    TextEdit *edit{getTabTextEdit()};
                                    edit->setGeometry(0,0,ui->tabWidget->width() - 3, ui->tabWidget->height() - 59);
                                    edit->setTabStopDistance(32);
                                    edit->setFocus();
                                    if(foreRed + foreGreen + foreBlue != 0)
                                        edit->setTextColor(QColor(foreRed, foreGreen, foreBlue));
                                    if(backRed + backGreen + backBlue != 765)
                                        edit->setTextBackgroundColor(QColor(backRed, backGreen, backBlue));
                                    edit->setFontPointSize(fontSize);
                                    edit->document()->setDefaultStyleSheet("QTextDocument:table{width: 100%; border-collapse: collapse;}");
                                    edit->setFontFamily(fontName);
                                    ui->fontComboBox->setCurrentFont(QFont(fontName));
                                    ui->fontsize->setValue(fontSize);
                                    QObject::connect(edit, &TextEdit::textChanged, this, &MainWindow::edit_changed);
                                    QObject::connect(edit, &TextEdit::cursorPositionChanged, this, &MainWindow::cursorPosChanged);
                                
                                

                                Do you want the full constructor code?

                                M Offline
                                M Offline
                                mchinand
                                wrote on last edited by
                                #14

                                Where's your table? Did you see this comment regarding the defaultStyleSheet?

                                "Note: Changing the default style sheet does not have any effect to the existing content of the document."

                                H 1 Reply Last reply
                                0
                                • M mchinand

                                  Where's your table? Did you see this comment regarding the defaultStyleSheet?

                                  "Note: Changing the default style sheet does not have any effect to the existing content of the document."

                                  H Offline
                                  H Offline
                                  HenkCoder
                                  wrote on last edited by
                                  #15

                                  @mchinand
                                  I have a button that asks lets you create a table.
                                  b411cc49-e3a1-4cb8-b1d3-cfac05311818-image.png
                                  Is this the problem?

                                  M 1 Reply Last reply
                                  0
                                  • H HenkCoder

                                    @JonB
                                    My bad sorry, I used a few times :: for example in the tabWidget one.
                                    QTabWidget::tab I tried again with that but it still doesn't work.

                                    JonBJ Online
                                    JonBJ Online
                                    JonB
                                    wrote on last edited by
                                    #16

                                    @HenkCoder
                                    Have you tried plain setDefaultStyleSheet/setStylesheet("table{width: 100%; border-collapse: collapse;}") on something?

                                    H 1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @HenkCoder
                                      Have you tried plain setDefaultStyleSheet/setStylesheet("table{width: 100%; border-collapse: collapse;}") on something?

                                      H Offline
                                      H Offline
                                      HenkCoder
                                      wrote on last edited by
                                      #17

                                      @JonB
                                      Sorry, I didn't understand what do you mean.

                                      1 Reply Last reply
                                      0
                                      • H HenkCoder

                                        @mchinand
                                        I have a button that asks lets you create a table.
                                        b411cc49-e3a1-4cb8-b1d3-cfac05311818-image.png
                                        Is this the problem?

                                        M Offline
                                        M Offline
                                        mchinand
                                        wrote on last edited by
                                        #18

                                        Try just creating a table statically in code first to figure out how to get the stylesheet working.

                                        H 1 Reply Last reply
                                        0
                                        • M mchinand

                                          Try just creating a table statically in code first to figure out how to get the stylesheet working.

                                          H Offline
                                          H Offline
                                          HenkCoder
                                          wrote on last edited by
                                          #19

                                          @mchinand
                                          Ok.

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

                                            Hi,

                                            I think you are mixing two very different things here:

                                            • Qt Style Sheet for customizing widgets
                                            • QTextDocument Rich Text support shown through QTextEdit.

                                            What you want to do is handled by the later. See the Rich Text chapter in Qt's documentation.

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

                                            H 1 Reply Last reply
                                            2

                                            • Login

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