Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to style a table in QTextEdit
Qt 6.12 LTS is out! See what's new in the release blog

How to style a table in QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
30 Posts 5 Posters 11.0k 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.
  • 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 Offline
          JonBJ Offline
          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
                            • 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
                              #26

                              @SGaist Yes, but in the way that they are distribuited, for example if I have 3 columns each one will be 33% but I don't want that they adapt with the text.

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

                                Then QTextTableFormat::setColumnWidthConstraints looks like what you want.

                                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

                                  Then QTextTableFormat::setColumnWidthConstraints looks like what you want.

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

                                  @SGaist And how do I do that? I tried to search online but I didn't find any solution that may help me, because they are reguarding QTableView.
                                  I also checked the documentation but it doesn't explain much and I still don't know how to use it.
                                  Thanks in advance.

                                  1 Reply 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
                                    • SGaistS SGaist
                                      QTextLength oneThird = QTextLength(QTextLength::PercentageLength, 33);
                                      QVector<QTextLength> constraints(3, oneThird);
                                      format.setColumnWidthConstraints(constraints);
                                      
                                      H Offline
                                      H Offline
                                      HenkCoder
                                      wrote on last edited by
                                      #30

                                      @SGaist This was the right solution! Thank you so much!

                                      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
                                      • Unsolved