I want to write a single, bold red line in my application using Qt.
As far as I understand, I would create a QLabel, set its textFormat to rich text and give it a rich text string to display:
QLabel *warning = new QLabel;
warning->setTextFormat(Qt::RichText);
warning->setText("{\\rtf1\\ansi\\ansicpg1252 {\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;} {\\colortbl;\\red255\\green0\\blue0;} \\f0 \\cf0 this is bold red text}");
I tested this rich text string in a rich text editor and it displays fine.
But Qt displays the whole string with all braces, keywords and backslashes instead of "this is bold red text". What am I doing wrong?
Thank you for your help.
Try using HTML formatting: <b><font... etc </b>.
Qt Designer does it like this: <span style=" font-size:8pt; font-weight:600; color:#aa0000;">TextLabel</span>
You can use Qt StyleSheets and set the styleSheet property of QLabel
warning->setStyleSheet("font-weight: bold; color: red");
Qt supports most CSS styles on its QWidget-derived classes. You don't need to set the text format to Qt::RichText for this to work.
Qt uses a simple HTML subset for formatting.
You can also do it programmatically using the settext function. Something like this:
QString labelText = "<P><b><i><font color='#ff0000' font_size=4>";
labelText .append("Text what u want to display");
labelText .append("</font></i></b></P></br>");
QLabel label->setText(labelText);
You can do it in a single line as well.
Related
I have a QTabWidget with two tabs inside. I want to set the background of both tabs to be transparent to see the color of the main window underneath, but when I set
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("background-color: transparent;");
it makes all the background of the widgets I have inside the tabs transparent, not the tab itself.
Album showing before stylesheet change:
after change,
and what I'm trying to achieve.
I can provide more code if needed. Thanks in advance!
Stylesheets are applied to touched widgets and all its children.
Currently your style definitions
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("background-color: transparent;");
says something like "Set background color for all widgets to transparent".
... but you can restrict stylesheet applicability.
E.g. you can say, that style definitions should be applied only to instances of certain classes like QLabel:
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("QLabel{ background-color: transparent; }");
There are even more possibilietes to define applicability in more details:
The Qt Style Sheet Syntax, Selector Types
Relevant for your case is the possibility to restrict the applicability of style definitions to objects with certain name:
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("QWidget#custom_tab, QWidget#templates_tab{"
" background-color: transparent; "
"}");
You must make sure, that object names match (here: 'custom_tab' and 'templates_tab'.
I have a docked QTextEdit which I am using to emulate a debug terminal in a QT c++ gui, and have it set to a black background with white text.
I am trying to use it to print out error messages from QXmlSchemaValidator, but the messages from the schema validator are in html format, and whenever I insert them into the QTextEdit, it reverts to it's default font, and I end up with black text on a black background.
The actual message is something like:
<html xmlns='http://www.w3.org/1999/xhtml/'>
<body>
<p>Content of element
<span class='XQuery-keyword'>minValue</span> does not match its type definition: <span class='XQuery-data'>fu</span> is not valid according to <span class='XQuery-type'>xs:decimal</span>..
</p>
</body>
</html>
using setAcceptRichText(false) doesn't solve the problem, and if I use insertPlainText() to add text to the lineEdit, it removes all the line breaks and leaves the html tags in the error message, which is unacceptable.
Is there some way I can display the HTML rich text, but without blowing away my style sheet font?
I tested it on my computer. It works perfectly. Use this:
edit->setHtml(htmlDescription);
edit->selectAll();
edit->setTextColor(Qt::green);
//Ok, but clear selection
QTextCursor cur = edit->textCursor();
cur.clearSelection();
edit->setTextCursor(cur);
It's sort of a kludge, but the workaround that I went with was to remove the HTML tags from the schema validator message before adding it to the text edit.
QString errorMsg = msg.statusMessage().remove(QRegExp("<[^>]*>"));
textEdit->setText(errorMsg);
In my application im trying to load a simple formatted text with some images into QTextBrowser. When im trying to set my image size using :
<img style="width: 128px; height: 128px;" src=":/new/prefix1/Star-five.png"/>
The qtextbrowser is automatically modifying my html and removing "style". Is there something i do wrong? Im setting this style from form editor.
Question asnwered. Need to use width height properties without "px".
I'm trying to make a simple design to select a color and I'm using a QToolButton to open a QColorDialog.
My problem is that I would like the QToolButton to be of the selected color but I've only suceeded to set the background color, which is not what I want.
Here is a picture:
My code:
QColor color = QColorDialog::getColor(m_couleur);
if (color.isValid()) m_couleur=color;
m_labelCouleur->setPalette(QPalette(m_couleur));
m_labelCouleur->setAutoFillBackground(true);
m_buttonCouleur->setPalette(QPalette(m_couleur));
m_buttonCouleur->setAutoFillBackground(true);
Basically I would like the QToolButton to look something like this:
edit: Can anyone explain to me why the website won't let me add "Hi everybody" at the beginning?
QColor color = QColorDialog::getColor(m_couleur);
QPixmap px(20, 20);
px.fill(color);
m_buttonCouleur->setIcon(px);
No CSS involved in this case is (for me ofcourse) big pro
Use the setStylesheet function in order to change the background color of your button
m_buttonCouleur->setStyleSheet(QString("QToolButton{ background: %1; }").arg(m_couleur.name()));
I've done exactly that by using a QPushButton and setting its style sheet to the result from the color picker. I guess a tool button should probably be the same.
button->setStyleSheet(QString("background-color: %1; "
"border: 1px; "
"border-color: black; "
"border-style: outset;").arg(color.name()));
I am trying to modify the QTableView to always show all editors. I am ok with the workaround to call openPersistentEditor() on all cells.
However I'd like the content of the cells to not be selected and no text cursor for empty fields.
This is what I get:
And this is what I'd like to have:
I tried using clearSelection() and clearFocus() but that does not do the trick. If I click on each cell I get the desired result and I could do the same thing programmatically, but I'd to know if there's a more direct way.
I had this exact same issue. I ended up just adjusting the selection color and selection background color on the QLineEdits. You can do it on all QLineEdits or just on a custom QLineEdit by giving each editor an object name and referencing that in the stylesheet.
/* applies to all QLineEdits in the application */
QLineEdit {
selection-background-color: white;
selection-color: black
}
/* applies to all QLineEdits with the object name "custom" in the application */
QLineEdit#custom {
selection-background-color: white;
selection-color: black
}