It is possible to set the CSS white-space of a QtQuick Label to pre-wrap? - qt

I have a QtQuick Label in a QML file like so:
import QtQuick.Controls 1.3 as Controls
Controls.Label {
id: lbl
text: "This is some <b>bold text</b> with extra white space"
}
If the text property of my label contains any HTML, then the label renders it as HTML and the multiple spaces in the original text are compressed down to a single space (if the text contains no HTML then it is rendered as normal text and the spaces are preserved).
QWidget has a setStyleSheet method that apparently supports the style "white-space: pre-wrap" which is what I need to get the HTML rendering to preserve the whitespace, but I'm not sure if I can apply this to a Label in a QML file. Is there any way to achieve this?
Edit: This answer: https://stackoverflow.com/a/2756376/14606 shows setting the styleSheet property for a QLabel. Is there any way to write a function that will let me pass my QtQuick Label and cast it as a QLabel and set the stylesheet in this way?

All you need to know is how to apply css in qml, instead of code (widget.setStyleSheet("...")) ?
Have you tried this?
Controls.Label {
id: lbl;
white-space: pre-wrap;
text: "This is some <b>bold text</b> with extra white space";
}
edit: Since white-space is not a property of Label, you need to set this property for the label text.
If possible (just guessing) use:
Controls.Label.Text {
white-space: pre-wrap;
But if not, use this workaround:
Controls.Label {
id: lbl;
textFormat: Text.RichText;
text: "<style>white-space: pre-wrap;</style>This is some <b>bold text</b> with extra white space";
}

You can actually achieve what I was looking for (a Label that can display HTML while preserving white space) by doing a text replace on your original string and replacing all regular spaces (" ") with ​ (which is a zero-length string followed by a regular string). The Label will then faithfully render consecutive spaces, as if the white-space style had been set to pre-wrap. Unfortunately this only kind of works (for my purposes at least) since the Label does not handle line wrapping the same way in both cases.

Related

Material TextInput Difficult to Identify

I'm trying to render a simple TextInput using QML with the following markup:
TextInput {
id: input
text: "some text box that has no easy way to identify"
font {
pointSize: 16
family: "Segoe UI Light"
}
}
What's actually rendering is this:
Notice how there is no way to identify the TextInput without the "default text" I've added. I would have expected something like the following (where there is an underline identifying the TextInput)
Things tried:
Isolated Text Input
Text Input in a Pane
Text Input in a Layout
Not exactly sure why it's not rendering correctly.
Based on #splaytreez's comment. I was incorrectly using TextInput but should have used TextField. Hopefully someone else finds this useful; although, I do feel it's a silly mistake.

How to wrap a long literal block in reStructuredText?

I have a literal block consisting of a single long line. It is currently displayed as a single-line block with horizontal scrollbar (like the literal block below). How do you implement auto-formatting to wrap the line so it wraps dynamically when the browser is resized?
::
Long line that does not wrap...........................................................................................
This could be done by overriding the default CSS with a new style:
pre {
white-space: pre-wrap;
}
Edit your theme's CSS file to include this directive and it should just work.

Is it possible to make .tt-input wider?

The typeahead example on the typeahead website seems to limit the width of the .tt-input using 3em !important in the style of the element, so that when you type something which is longer than 3em it starts to scroll out of view in this tiny input.
How can I make .tt-input wider?
Here is a screenshot to help explain what I'm talking about: http://oi57.tinypic.com/2vvt9qc.jpg.
This appears to be hardcoded into bootstrap-tagsinput.js, see lines 46-47:
var inputWidth = (this.inputSize < 3 ? 3 : this.inputSize) + "em";
this.$input.get(0).style.cssText = "min-width: " + inputWidth + " !important;";
inputSize is computed based on the placeholder text. If there is none, it will default to 3em. You might want to change these lines to obtain a different value, or remove the attribute altogether. I'm not entirely sure why it's there.
set the class to this:
input.tt-input {
width:auto !important;
}
... make sure you put it AFTER the typehead css so that it's overwritten.
I had the same problem and Eepzy is correct: bootstrap-tagsinput.js does use the placeholder text to determine the size of the input field, but the 3em is only used if you don't have a placeholder text that is a least 3 characters long.
I think editing a 3rd party js is not good practice in general.
There is a different solution to our problem: just use a placeholder text.
<input type="text" class="tagsinput" placeholder="Type here..."/>
Any text (except whitespaces) will do as long as it is at least 3 characters long.

Set font color in a tr() function

I am thinking of changing the color of some text in a tr() function.I am thinking of
tableModel->setHeaderData(2, Qt::Horizontal, tr("<font color=red><i>Org. Name</i></font>"));
Will it be possible to change the font color?.
I don't think that providing an HTML string will change the appearence of the text and even if it works it is not the best approach to mix the content with the styling. You should use stylesheets and change the color of the header view of your QTableView.
QString styleSheet = "QHeaderView::section {"
"color: red;"
"background-color: black; }";
tableView->horizontalHeader()->setStyleSheet(styleSheet);
Yes, the whole HTML string will be offered for translation, including the markup. If you want translators to mess with the HTML, your approach just works. Usually one wouldn't want translators to mess with (and possibly break) the HTML markup, then one would use something like this:
QString::fromLatin1("<font color=red><i>%1</i></font>").arg(tr("Org. Name"))
I.e. mark only the actual text for translation, instad of the whole html string.

Qt Stylesheet for QMessageBox

I am using stylesheets. I want to set style information for the main message text and the informative text for a QMessageBox. Is it possible to access these sub-controls ?
Yes it is possible. The trick is to know how to select the sub-controls. Here's how you can change the style of the text, in this example I make the dialog grey and the text off-white:
QMessageBox {
background-color: #333333;
}
QMessageBox QLabel {
color: #aaa;
}
The second clause uses a Descendant Selector which in this case means "any QLabel that is a descendant of a QMessageBox including children and grandchildren etc". You can be more specific and only select children with QMessageBox > QLabel
I found this information here http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html
Just use rich text - with the exception for the detailed text since it's always interpreted as plain text.
Take a look at the documentation here.
You can simply do QMessageBox {font:...} in the stylesheet or with setStyleSheet(...)
Unless you trying to set the style for the main message text separately from the informative text... is that what you are trying to do?

Resources