I have a custom tool button where when checked the font become bold. When happens, the size of the text increase and consequently the ToolButton shrink or stretch accordly. How can i pre calculate the correct fixed size for the text inside the button?
You can use FontMetrics QML type for this purpose. Setup the FontMetrics object with the same font and style you are using for your text, then call an appropriate method of your FontMetrics instance like boundingRect or tightBoundingRect and resize your button based on the results.
If you just want to keep your ToolButton to a fixed size that can contain the normal and bold font without auto-resizing, you can just get the size for the bold text using the FontMetrics instance and set the button size accordingly.
For convenience you can also use TextMetrics QML type. The advantage is that the boundingRect and tightBoundingRect are properties and you can bind to them.
The latter is preferred for your use-case.
Related
When I use any method to select one of non-Default (or Base) styles in Qt Quick Controls 2 application (i.e. Universal or Material), then all the controls with text (like Label, TextField, whose font size must depend on QGuiApplication::font) uses font size value, which QCoreApplication has before changing:
QFont font = application.font();
bool ok = false;
font.setPointSize(QSettings{}.value("fontSize", 17).toInt(&ok));
Q_ASSERT(std::exchange(ok, false));
application.setFont(font);
Only Text, TextField are resized properly, but they are of no use in my GUI.
When I stick Default style, then all the mentioned items are resized properly.
How to make all the items be resized depending on global font.pointSize when used styles, other then Default?
Another connected question is how to get proper (means "contrast" and style-conformant) color for, say, highlighted text and background for the current style theme used? Using SystemPalette { id: palette } from ApplicationWindow in children gives colors suitable only for Default style (say palette.highlightedText is "white", palette.highlight is "blue" or "darkblue" (not sure)). It looks ugly in style themes, differs from Default.
Another important observation is: if I set font.pointSize: 17 (or equally font: Qt.application.font) in root ApplicationWindow, then all the items are resized properly, except of those of them which have new context: say, highlight: and delegate:s into *Views, sourceComponent:s into Loaders, default property item of Component and Repeater and others, where inheritance of the font breaks due to lost of the parent Item's context.
It seems, that I should to manually "inherit" ApplicationWindow.window.font for each new context. It is sad if so. It is boring, if e.g. in Repeater I use RowLayout with a plenty of Labels: in each of Label I have to add font: ApplicationWindow.window.font.
Orient, I know it is too late, but one can also set font size in QApplication like this:
QFont font = QApplication::font();
font.setPointSizeF(fontSize);
QApplication::setFont(font);
Im changing text of QTabWidget dynamically and also changing its font family. so the text in the tabBar header is exceeding the actual width. how i can control the text to wrap inside the tabBar width like
"Text.."
im setting the font family using stylesheet
QString menuBarStyle = QString("QTabBar::tab {font-family: %1;font-style: %2;font-size: 11pt;font-weight: %3;width: 130px}").arg(m_currentFont.family()).arg(fontStyle).arg(fontWeight);
TabWidget->setStyleSheet(menuBarStyle);
Qt stylesheet technique may or may not let to solve it that but I once resolved similar problem like that:
QString elidedText(const QFont& someFont, int width)
{
QFontMetrics metrix( someFont );
return metrix.elidedText(text, Qt::ElideRight, width);
}
So, in addition to font object you have to have the width to fit the text in. Also frequently applying changes via the stylesheet for the widget can hardly be optimal solution, it does a bit too much to get the small change.
I want to paint controls directly into TextEdit. It is easy, but the problem is i need to have space for them, so they are not painted over the text.
For example (the whole line is representing what will be shown in TextEdit and the highlighted code should be qml component):
Here is button: button and here is text again.
I need somehow to reserve space for the button between Here is button: and and here is text again.
It looks like Qt qml doesn't provide any way how to specify font metrics (in that case i could ask the component what it's width is and just add
one whitespace char with properly setup font and its metrics and than
specify component coordinates so it is painted exactly where the space is).
I did it in Java SWT, because SWT StyledText allows to setup metrics for each character. So this is example how it should look.
You can use HTML markup in TextEdit
TextEdit {
textFormat: TextEdit.RichText
text: "<font size=50>text</font>"
}
For example, we have a QLabel with MaximumWidth set to 400.
When we try to display some text with pixel width more than 400, it's shown cut off.
Is there any way to make QLabel display this string in multiple lines without using QFontMetrics or the like?
If I understood your question correctly, you should use the setWordWrap function for your label, with true as its parameter.
QLabel lbl("long long string");
lbl.setWordWrap(true);
In order to show multiple lines in QLabel, right click on QLabel and select 'change rich text'. This brings up dialog where you can type the text as you want to see including enter key. Setting the word wrap is not required for this.
If you set the word wrap as well (in QLabel properties) than it will wrap each individual line in the Qlabel if it was longer than the real estate.
As another option to wrap text using Qt Designer, you can check the box under Property Editor for a QLabel:
I have a QDialog I'm working with. It is made somewhat like a QMessageBox. I noticed that the size of the QMessageBox (and the size of its label) depends on the size of the message displayed.
How would I make the size of my QDialog adjust automatically like a QMessageBox? Presently my QDialog contains a button box and a label, and the QDialog is layout Vertical.
(I know I could just use the message box directly but eventually I will have more complex dialogs.)
Automatic solution:
Use layouts and set size policies to QSizePolicy::Expanding. In QtDesigner, once all your children are placed on your QDialog, then click on the Adjust Size button next layout ones. Your QDialog will be automatically resized at runtime.
Manual solution:
The QWidget class has a method adjustSize that resize the QWidget to fit its content. Just call it when all children are set.
Set your dialog to be expanding, and very small. Then, be sure to set your message before showing the dialog. When shown, it will try to find its proper size, based on the size of the objects it contains. (This happens recursively, so if the dialog isn't the direct parent of the label in which you show your message, make sure everything between the label and the dialog is set to use layouts.)
A TIP : if you try to use "adjustSize()" function when dialog is hidden, it may not be works fine. It would be better to use it after the "show()" function.