Font size won't change for selected item - qt

I am using a QComboBox in my program with items added on launch. The size of the font for the listed items is correct, but when I select one item, the font size that appears in the main box is different.
I tried this:
How to change font size of first item in Combo Box PyQt4
but I see no results. Modifying the size in Qt Creator's ui editor only changes the size of listed items. At the beginning of my class constructor, I tried this to fix the problem:
ui->setupUi(this);
QFont font = ui->OrderNumber_edit->font();
font.setPointSize(15);
ui->OrderNumber_edit->setFont(font);
OrderNumber_edit is a pointer to my QComboBox.
Still, I see no changes for the selected item, only for the listed items.

I can't reproduce that bug.
With the code:
ui->comboBox->addItems(QStringList()<<"asdf"<<"qwer");
QFont font = ui->comboBox->font();
font.setPointSize(25);
ui->comboBox->setFont(font);
I get the correct result: [look here]
Maybe a stylesheet overwrites the font size of your combobox?

Related

Font size when using non-Default style in Qt Quick Controls 2

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);

How to change the height and width QTabWidget tab

I would like to customize the look of QTabWidget tabs themselves (the tabs that we actually click to switch to another tab widget) by changing their height and width (which on OS X look more like pushButtons).
How to achieve this?
from PyQt4 import QtGui
app=QtGui.QApplication([])
dialog = QtGui.QDialog()
dialog.setLayout(QtGui.QVBoxLayout())
tab_widget = QtGui.QTabWidget(dialog)
dialog.layout().addWidget(tab_widget)
tab_widget.addTab(QtGui.QWidget(), "First")
tab_widget.addTab(QtGui.QWidget(), "Second")
tab_widget.addTab(QtGui.QWidget(),"Third")
dialog.show()
app.exec_()
On OS X, the size of these elements is fixed. They will lose platform styling when you attempt to resize them. Thus you will have to come up with full tab styling on your own: overriding even one attribute drops the platform style, which can't be adjusted, and reverts full control to you.
The styling is done via QSS (Qt Style Sheets), a CSS-lookalike. Here's an example, and you'll also want to consult the documentation.

How to set a fixed size for the items of a QListView

I have QListView in icon mode and I am setting a QIdentityProxyModel on it. The problem is that my items are not placed in an order like being on a grid and I guess that the problem is that they have different widths, due to the different size of their names.
How can I fix this issue? Is there any solution for QListView items similar to setSizeHint() of QListWidgetItem?
m_itemsModel = new QFileSystemModel(this);
m_iconProxy = new IconProxy(this);
m_iconProxy->setSourceModel(m_itemsModel);
ui.listView->setModel(m_iconProxy);
ui.listView->setUniformItemSizes(true);
And as I have mentioned in my comment below, I have set iconSize to 64x64.
Furthermore, my QIdentityProxyModel has overridden the method data to return an empty QIcon() or a custom thumbnail or the operating system's icon.
And I get this (wrong):
instead of this (correct):

QCheckBox indicator size is wrong after stylesheet was applied

I created an own checkbox class from QCheckBox class. In my implementation I use size of checkbox indicator and checkbox label spacing for my internal algorithms.
I get sizes like this:
// Checkbox indicator size.
style()->subElementRect(QStyle::SE_CheckBoxIndicator, &option);
// Checkbox label spacing size.
style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &option);
For standard widget it works OK. But when I apply qss on my widget with custom indicator icon with another size, I still get standard values, instead of qss one.
How can I get a correct values for styled widget? I am using Qt 4.6.

How do I auto-adjust the size of a QDialog depending on the text length of one of its children?

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.

Resources