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 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
                                • SGaistS SGaist

                                  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.

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

                                  @SGaist Hello,
                                  So, I got the border collapse working by doing this:

                                  TextEdit *edit{getTabTextEdit()};
                                      QTextTableFormat format;
                                      format.setBorderCollapse(true);
                                      edit->textCursor().insertTable(lineNum, columnNum, format);
                                  

                                  But I don't know how to set the table's width to change when the textedit's width changes and I don't know how to put its width to 100% as CSS does.

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

                                    If memory serves well, something like:

                                    format.setWidth(QTextLength(QTextLength::PercentageLength, 100));
                                    

                                    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
                                    0
                                    • SGaistS SGaist

                                      If memory serves well, something like:

                                      format.setWidth(QTextLength(QTextLength::PercentageLength, 100));
                                      
                                      H Offline
                                      H Offline
                                      HenkCoder
                                      wrote on last edited by
                                      #23

                                      @SGaist Yes! This was right!
                                      Thank you so much, but I have one last question, sorry about that.
                                      When I create the table it's like this:
                                      2ee25edf-65b3-4cb4-b708-c72fc795898b-image.png
                                      but then when I type something in a cell the line separing the different cells adapts to the text. How do I avoid that?
                                      1028cb6d-ea87-49a6-a923-5f6d7e1adbb7-image.png
                                      I just wanted to expand the table to the TextEdit's width not also this feature.

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

                                        So you want the column to all take the same percentage of space ?

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

                                        H 2 Replies Last reply
                                        1
                                        • SGaistS SGaist

                                          So you want the column to all take the same percentage of space ?

                                          H Offline
                                          H Offline
                                          HenkCoder
                                          wrote on last edited by HenkCoder
                                          #25
                                          This post is deleted!
                                          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