Qt: Change the icon in a QListView - qt

Is there a way to change the icon of an element in a QListView after it has been displayed?
I can't find a way to do a setData with the DecorationRole (the role used for the icon).
The model used is a custom QFileSystemModel, but the default implementation is only good for the EditRole so to change the name of the item.
I'm don't know how the the overridden setData can be done.
The goal is to change the icon when the mouse is over the item (through mouseMoveEvent)

You will need to subclass QListView and implement the behavior that changes the role of the displayed icon when the mouse hovers over the item.
Such a subclass could use an internal proxy model so that it wouldn't need to mess with the base view's painting. Simply inform the proxy that a given item's decoration role data has changed, and the base view will know what to do.

Related

How to gray a group of widgets when they are disabled?

There is a QGroupBox full of widgets, and all of them need to be disabled and grayed out.
setDisabled(true) does disable them functionally, but they don't turn gray.
What is the easiest and most proper way to turn them gray?
This should be a standard operation: text turns gray so that users can easily see that they are disabled.
QWidget::setDisabled() is yet (another) slot for the QWidget enabled property.
From Qt doc.:
This property holds whether the widget is enabled
In general an enabled widget handles keyboard and mouse events; a disabled widget does not. An exception is made with QAbstractButton.
Some widgets display themselves differently when they are disabled. For example a button might draw its label grayed out.
(Emphasizing by me.)
How widgets are displayed depends on their respective rendering as well as the (default) QStyle which is used in the application.
Concerning custom widgets (classes derived from any "built-in" Qt widget) where paintEvent() is overloaded, the custom widget is itself responsible to render accordingly to different states.

Qt Item Delegate

I am new to Qt and am slowly finding my way around. My goal is to have a QListView of a QFileSytemModel where the names of the files in the icons wraps, similar to the behavior found on any OS where the text gets split if the name is too long.
From perusing the internet, I believe I need to create a custom class that extends QAbstractItemDelegate to do my special drawing and text wrapping. However, I haven't been able to find the default ItemDelegate that the stock QListView class uses out of the box.
The reason I want the default class is so I can poke around and figure out more about the life-cycle of Qt components while working on my own renderer. I was wondering if anyone knew of where the default renderer for the QListView class could be found?
If you want to show icons with text, QListView has a mode to do that, just set the view mode to QListView::IconMode using QListView::setViewMode(). If you still want to customize display features it is right that you should implement a custom item delegate, preferably subclassing QItemDelegate and overriding paint() with your own implementation.

Qt Squared Radio Button

I want to create a squared radio button, with the text inside the button. Is that possible? I think the button shape could be changed trough css, but what about the text?
Any hint?
Rather than trying to deform a QRadioButton into something which visually resembles a QPushButton, I would simply use a QPushButton with some custom logic.
You won't have to worry about the visual aspect then, while the logic itself is not all that hard to write.
As stated by #besworland, QPushButton inherits from QAbstractButton, which already has the option to be checkable or not. You can set this via setCheckable(bool).
To mimic the "exclusive" behaviour of a set of QRadioButtons, you can add your buttons to a QButtonGroup and make it an exclusive one. As stated in the documentation "An exclusive button group switches off all checkable (toggle) buttons except the one that was clicked." You can use a QButtonGroup's setExclusive(bool) method for that.
In any case, I would consider those easier options than transforming a QRadioButton to fit your needs.

Customizing the checkboxes of the items of a QTreeView

I'm having the following situation: I need to create a custom tree control, whose checkboxes are also customized. I have easily made most of the customizations for the tree control by using style sheets; I have succeeded adding checkboxes to the QTreeView's items, but I'm having big problems with customizing them - I need to display a custom image for the checked state, and another for the unchecked state.
The place I concluded this can be done is in my subclass of QStyledItemDelegate, in the paint event (i.e. CheckBoxItemDelegate::paint). What I need is to display the text, the icon, and the checkbox for the item. But the problems are:
- I can't get the style of the item (which I set using the stylesheet) - otherwise my text can be drawn with the incorrect color;
- I don't know the rects of each subitem (the checkbox, the icon, the text);
- I don't know how to get the icon of an item (given its QModelIndex) to draw it.
P.S. I had subclassed the QTreeView (obviously), and, as I am working with QFileSystemModel, I have subclassed it too in order to add the checkbox functionality to it.
Can anybody help me, please?
Is QStyledItemDelegate::paint the proper place for changing the visuals of the checkbox of the tree items? If yes, can you please give me a small example or something, how I can do that?
This is how I rendered checkable items inside a QTreeView with two images (eye opened/eye closed, to represent their visibility state) instead of a checkbox:
ui.myTreeView->setStyleSheet(
"QTreeView::indicator:unchecked {image: url(:/icons/eye_grey.png);}"
"QTreeView::indicator:checked {image: url(:/icons/eye.png);}"
);
Items should be set as checkable, of course. Hope this helps.

QAction vs QToolButton and when to override the Basic class?

I've recently been studying Qt, and have the following questions:
What is the difference between QAction and QToolButton?
How do I know when to override QPushButton? For example, should I override in order to be informed when the mouse enters a QPushButton's bounds? I don't need to in order to get the signal click().
Question 1:
QActions are used to define tasks performed by an application in a way which can be understood by a number of different user interface objects. Using an example from the Qt docs for context:
A QAction is defined; it is given an icon image, a text description, a keyboard shortcut and a longer tooltip description, as well as being linked to a user defined function.
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
Later in the implementation, the QAction is added both to a textual menu bar...
fileMenu->addAction(newAct);
... and an icon-based tool bar:
fileToolBar->addAction(newAct);
The menu bar uses the text description to create an entry in the menu corresponding to the action. Similarly the toolbar uses the icon set on the QAction to create an icon in the toolbar which corresponds to the action. Selecting the menu item, clicking the toolbar icon or pressing the keyboard shortcut will all result in the same effect: that defined by the linking of QAction::triggered() to newFile().
So to directly answer your question: a QAction is an abstract way of defining the various parameters and behaviour of a particular task performed by the application. A QToolbarButton is a UI object (derived from QWidget) created by a QToolbar in response to QToolbar::addAction()
Question 2:
Yes, QPushButton has a clicked() signal inherited from QAbstractButton, but it does indeed lack a way to inform when the mouse has entered its bounds. You have a couple of options in order achieve this, but first you need to first set the mouseTracking property to be enabled. This will allow you to receive mouse move events on the QPushButton even if no mouse buttons are pressed. With that done you need to explore one of the following options:
As you suggest, you could subclass QPushButton and reimplement mousePressEvent in order to respond to the mouse position.
You could install another widget as an eventFilter on the QPushButton, and watch for events of type (QEvent::MouseMove).

Resources