Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTextDocument default style sheet

Tags:

css

stylesheet

qt

I try to draw a text on a QImage using the QTextDocument. How to set a default ("body") style?

Currently I use a <p> tag, like this

QPainter painter(_image);
QTextDocument doc;
doc.setDefaultStyleSheet("p { color : green; background-color : black; }");
doc.setHtml("<p>test 123</p>");
doc.drawContents(&painter,_image->rect());

I would like to get rid of <p> tag

doc.setHtml("test 123");

I have tried

doc.setDefaultStyleSheet("body { color : green; background-color : black; }");
doc.setDefaultStyleSheet("{ color : green; background-color : black; }");
doc.setDefaultStyleSheet("color : green; background-color : black; ");
doc.setDefaultStyleSheet("QImage { color : green; background-color : black; }");

EDIT:

I have also tried

doc.setDefaultStyleSheet("* { color : green; background-color : black; }");
like image 519
user2449761 Avatar asked May 11 '15 14:05

user2449761


Video Answer


1 Answers

You should wrap content with <body> tag:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QTextDocument doc;
    doc.setDefaultStyleSheet("body { color : green; background-color : black; }");
    doc.setHtml("<body>test 123</body>");

    QTextEdit textEdit;
    textEdit.setDocument(&doc);
    textEdit.show();

    return app.exec();
}

enter image description here

like image 68
svlasov Avatar answered Nov 15 '22 09:11

svlasov