How to style a table in QTextEdit
-
//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?
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."
-
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."
-
@JonB
My bad sorry, I used a few times::for example in the tabWidget one.
QTabWidget::tabI tried again with that but it still doesn't work.@HenkCoder
Have you tried plainsetDefaultStyleSheet/setStylesheet("table{width: 100%; border-collapse: collapse;}")on something? -
@HenkCoder
Have you tried plainsetDefaultStyleSheet/setStylesheet("table{width: 100%; border-collapse: collapse;}")on something? -
Try just creating a table statically in code first to figure out how to get the stylesheet working.
-
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.
-
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.
@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.
-
If memory serves well, something like:
format.setWidth(QTextLength(QTextLength::PercentageLength, 100)); -
If memory serves well, something like:
format.setWidth(QTextLength(QTextLength::PercentageLength, 100));@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:

but then when I type something in a cell the line separing the different cells adapts to the text. How do I avoid that?

I just wanted to expand the table to the TextEdit's width not also this feature. -
So you want the column to all take the same percentage of space ?
-
Then QTextTableFormat::setColumnWidthConstraints looks like what you want.
-
Then QTextTableFormat::setColumnWidthConstraints looks like what you want.
@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. -
QTextLength oneThird = QTextLength(QTextLength::PercentageLength, 33); QVector<QTextLength> constraints(3, oneThird); format.setColumnWidthConstraints(constraints); -
QTextLength oneThird = QTextLength(QTextLength::PercentageLength, 33); QVector<QTextLength> constraints(3, oneThird); format.setColumnWidthConstraints(constraints);
