How to style a table in QTextEdit
-
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 butedit->setStyleSheet("QTextEdit:table{border-collapse: collapse;}");
doesn't seem to work.
Is there a way I can achieve this?
Thanks. -
QTextLength oneThird = QTextLength(QTextLength::PercentageLength, 33); QVector<QTextLength> constraints(3, oneThird); format.setColumnWidthConstraints(constraints);
-
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 butedit->setStyleSheet("QTextEdit:table{border-collapse: collapse;}");
doesn't seem to work.
Is there a way I can achieve this?
Thanks.@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 -
@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@raven-worx Hello, thank you for your answer.
I checked the link you sent me andtable
andborder-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:
border-collapse should make this effect tho:
-
@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.
-
@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.
-
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 butedit->setStyleSheet("QTextEdit:table{border-collapse: collapse;}");
doesn't seem to work.
Is there a way I can achieve this?
Thanks.@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
(orQTextDocument:table
) syntax from? -
Can you share a minimal full project that has your current code for applying a stylesheet to a QTextDocument?
-
@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
(orQTextDocument:table
) syntax from? -
@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.@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 likea:link
, which is quite different from your case.) -
I meant your current code. Post it here. Not just the single line calling
setDefaultStyleSheet()
.//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?
-
@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 likea:link
, which is quite different from your case.) -
//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::tab
I 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.