Changing QTreeview checkbox to icon without using stylesheets - c++

I wanted to have a QTreeView with icons ( which act as a checkbox ) followed by some text from a subclass of QAbstractItemModel. I cannot modify or change the structure of this QAbstractItemModel.
When I use this QAbstractItemModel with QTreeview, it shows checkbox followed by some text and performs the intended function required. I just wanted to change this check box to on and off icons.
I found this:- Customizing the checkboxes of the items of a QTreeView
But, I cannot use stylesheets in my case as the icon I want to use some system icons which are loaded using icon loader which returns a KIcon instance ( subclass of QIcon ).
What would be the right way of doing this?
I saw this tutorial and tried creating an editor for a checkbox. But, how should I register editor as QVariant does not have Qt::CheckStateRole type?
I tried using Qt::Int, but it doesn't works

Related

Proper way to find out which Tab of QTabWidget is currently selected

I have a QTabWidget with several tabs, every tab containing a QTableWidget with numbers. Outside of this QTabWidget, I have one button. When the button is pressed, the currently selected widget should be processed. The QTableWidgets are identical in their structure, they just display numbers, and if I have a pointer to one of these widgets, I cannot deduce which tab it came from.
A simple method to identify which widget is currently selected is to call currentIndex(). But this will break when I reorder the tabs in the designer and is probably not working with movable tabs.
Another way is to use currentIndex() and tabText(int index). Like this I have a stable way to find out which tab is currently selected. The disadvantage of this is that I am then dependent on having unique tab texts, and I in general don't like to rely on a UI property to implement functionality.
My solution for now is to give every Widget a different accessible name that I can then read out like this:
QWidget* widget = tabWidget->currentWidget();
QString* name = widget->accessibleName();
This works, but I wonder if there is a better solution, something like the Qt::UserRole that can be assigned to any cell in a QTableWidget.

How to create an editable list of items in Qt Designer?

I'm using Qt Designer to create a Qt 5.4 GUI application (actually, I'm using Qt Creator 3.3.2 but haven't done any C++ yet; everything is in the .ui file).
I'd like to create a box containing an (empty) list of items from which the user could double click inside the box to add a new element to the list (can be single click or other, doesn't really matter).
I just can't figure out how to do this only through the designer. I've checked the properties of the "List Widget (Item-Based)" and "List View (Model-Based)" and there is some options in "editTriggers", but even with the default "DoubleClicked" checked, the compiled application does not allow adding a new element (or deleting...)
Any easy way to achieve this using only the Designer (without doing C++)?
Or is there another widget that would be better suited for this?
Thanks!
If you want to make such a widget, that can be setup completely in the designer only, you are asking a little too much.
If you start with a List Widget and add a bunch of empty named elements, and then make all of them editable, it will seem like you can just double click to add an element.
To make an existing element you've added in the list editable, (in the designer) double click your List Widget, then
in Edit Widget List, click the plus sign, followed by Properties. Scroll down to flags, then check Editable.
This needs to manually be done for each item added.
The C++ to add what you are asking for is very painless.
Add a Push Button. Rename it's objectName to addItemButton.
Then right click on it, and select Go to slot....
Select clicked(), then click Ok.
Now put the following in the slot:
void MainWindow::on_addItemButton_clicked()
{
QListWidgetItem * item = new QListWidgetItem("New Item");
item->setFlags(item->flags() | Qt::ItemIsEditable);
ui->listWidget->addItem(item);
}
Done.
Hope that helps.

Populating a QTreeView on an existing application with a QFilesystemModel

I have an existing Qt application with buttons and other widgets on it. I would like to add a QTreeView (or QTreeWidget) to the application, and populate it with a QFilesystemModel.
I have seen examples and read the Qt documentation on using QTreeView and QTreeWidgets but they are always just creating a standalone QTreeView on its own, which I can replicate fine.
I used QtDesigner to add a QTreeView on my application, but i cannot work out how to use the QFilesystemModel to populate it.
How do I access the QTreeView on my application and set its model to the QFilesystemModel?

exposing subcontrols from the custom widget plugin in QT

I am using QT 4.3. I have created one custom widget plugin. I could be able to show it in the desiner tool box as well as use it on the form with no problem.
This custom widget internally holds QGroupBox, QLabel, QTextEdit.
Now I want to apply the styles to individual componets of this custom widget.
I want to expose these internal conrols as sub-control and style them. This would be similar to tear subcontrol of QTabWidget. In style sheet we can refer it as QTabWidget::tear...
Is there any way by which I can do similar thing with my custom widget?
The subcontrols are defined in the (internal to Qt) knownPseudoElements array in qstylesheetstyle.cpp, so you won't be able to add your own pseudoelements. However, you can use the ID Selector feature to address individual controls in your widget. For example, if the names of your QGroupBox, QLabel, and QTextEdit are group, label, and edit, you can use:
#group {color:green} #label {color:blue} #edit {background-color:red}
to change the sub-widgets

file selector in a qtablewidget

I'm trying to get one cell in a QTableWidget to be a box with button at end"..." with file selector, but don't know how to change what kind of widget the cell is.
To control the types of cells in a table, it's best to use the QTableView class. Then, by using QItemDelegate you can make some table cell a button, to which you may bind a signal that opens a dialog.
I recommend starting by reading about delegate classes in the Qt docs.