QCheckBox indicator size is wrong after stylesheet was applied - qt

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.

Related

Font size won't change for selected item

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?

Dynamically change QStyle at runtime?

I want to tweak the existing style at runtime, specifically QStyle::PM_ToolBarIconSize. Can this be done? As far as I can tell, you can only inherit QStyle and override pixelMetric().
Edit 1: In the specific case of QStyle::PM_ToolBarIconSize, I could use QToolBar::setIconSize, but that would set the icon size for just a single toolbar instance. I want to change the underlying style hint to affect all toolbars everywhere with one fell swoop. And QStyle::PM_ToolBarIconSize may not be the only style I want to tweak, it's just the first one I'm looking at that just so happens to have a "change this instance's icon size" function.
Edit 2: I can't just make a new style subclass because the current style is already a custom style based on style sheets. There are several styles that a user can choose from. I don't want to create a bunch of new styles just so I can tweak a couple of toolbar icon or menu height size settings.
This is the exact purpose of QProxyStyle.
Why not overriding QStyle then? Your subclass would return an icon size (via pixelMetric) which depends on a settable parameter of your QStyle.
As Qt does not have a dynamic QStyle mechanism, it is better to create a new style instance with the changed icon size, then set it to the QApplication, rather than altering the current style.

Height of editor in QItemDelegate

I have QListView with custom implementation of QItemDelegate. MyItemDelegate reimplements createEditor() to show custom widget. Size of widget is dependant on content.
By default, each row height is about 20px (one row), but my editor has bigger height.
I was trying to override QItemDelegate::sizeHint() method, but is doesn't contains reference to editor, so I couldn't calculate correct size.
How can I make QListView resize rows to actual size of editor?
You should emit layoutChanged after creating an editor, if you could not override sizeHint correctly. But it should be enough to override sizeHint.

Displaying a QStandardItem with its foreground color in a QTreeView even when it is selected

Using Qt 4.6.3 on Linux/X11.
I have a QTreeView widget which uses a QStandardItemModel as its model, with 4 columns and hundreds of rows. Most of the items in the list are to be displayed with a standard color, but a few need to be of a different color. I can change the colors of those few items easily with QStandardItem::setForeground().
However, that only affects the color of the item when it is not selected. When I select a colored item, its background color changes to blue (which is ok), and the text color changes to white (which is not ok). I tried using a stylesheet to affect the foreground color of selected items (with selector QTreeView::item:selected), but it affects all items.
I would like items for which I called item->setForeground(Qt::red) to remain red even when they are selected, and other items to use the default set of colors (which they already do). How can that be done?
The colors being used are (I assume) those for the QPalette's Hightlight and HighlightedText roles. Unfortunately, I don't know of any way to set those on an individual standard item.
However, since standard items are used in the model/view framework, you have another option. You should be able to create a delegate to paint the view however you want to. I would recommend inheriting from the styled delegate, and calling the parent class's functionality as much as possible. Likely, you'll only need to change a few parameters in the cases where an item is selected and has a non-standard foreground color.

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