Problems with displaying data in QListView - qt

Good day!
There are instances of classes QListView and QTreeView.
Both of the instances loads data from model (QStandardItemModel).
QTreeView displays positions (For example: Chief, Manager, Developer, etc).
Clicking on the title of position a list of employees revealed.
QListView displays only positions of staff.
Question:
How can I display a full list of names of employees in QListView not showing their positions?
Which methods I need to override?
What can you advise in this situation?
P.S. Thanks!

I don't think you are going to be able to do that with a single model.
This thread suggests using a proxy model to flatten the original one without having to maintain two instances of that data. But the implementation pointed to (KDE's KReparentingProxyModel) isn't exactly trivial.
There is some documentation on proxy models, and the QSortFilterProxyModel might be usable in your context, although I think you'll need something more specific.
You might also find the classes attached to the third response on this thread: ModelView - how to use proxies to filter this data? interesting as a starting point.
(Sorry this isn't very specific. Searching for "qt flatten tree model" will give you other ideas.)

Try to use QListWidget, is easier than QListView.

Related

Learning Qt: Which methodology to be used for this "advanced UI"?

I am learning Qt5 using PyQt.
My goal is to create a UI with several goals (I will base my need on the screenshot below).
So here are my goals:
My need
Add directories to a list of directories to be scanned (I know how to use QFileDialog.getExistingDirectory for that). For this I'd like to have a "+" button.
When pressed the QFileDialog would open and a new row would be added.
Then I would scan the directory to look for files. It won't be my first implementation but I'd like to have a circular progress bar being displayed during the scan (at the place of the classic progress bar).
When the scan is done, the UI would display the number of files found during the scan. And the progress bar would be replaced by a tick mark icon (not shown on the screenshot...).
At the beginning of each row, I'd like to have a "-" button to delete the row.
My goal is to learn
This is important, I know I am not the first one to have the idea of such a UI.
So I not looking for a third party lib on top of Qt.
My goal is to learn Qt5 (with PyQt) with this example.
But if its unrealistic, please tell me!
My knowledge
Mode View: I implemented some basic model view widgets to display strings, and I understand how to extend the idea to tables or trees.
In this case the number of rows in the table would be handled automatically.
But is it possible to created a table that would display not only strings but also widgets?
Widget Mapper: I read that another methodology is the QDataWidgetMapper. In this case, I would have to deal with creation of news rows by myself, and then I would map data onto them.
But it seems to be a hard and long job.
So, is it a good idea?
So finally could someone tell what is the best direction for that?
I am not looking for code, but since it takes a lots of time to learn new concepts, I'd like to learn and use the correct one before starting coding.
Thank you for your help :)!
After so more searches, I found that I need to use the delegate methodology.
In Qt5 it's QStyledItemDelegate and in Qt4 it's QItemDelegate.
I could find a nice tutorial at the moment, but I started coded it.

Best way to list files in a QFileSystemModel()?

I'm beginning Qt/pySide programming and am trying to implement a simple QListView with QFileSystemModel as the model. I have this working and in addition have defined a name filter on the model. I'd like to get a list of all files in the QListView (or rather the underlying model).
The following code appears to do this, but is incredibly ugly and cannot possibly be the correct way. Help!
model = myQListView.model()
idx = model.index(model.rootPath())
for i in range(0, model.rowCount(idx)):
child = idx.child(i, idx.column())
print model.fileName(child)
That is the correct way of working. The whole idea of the QAbstractItemModel abstraction is to provide a unified API for accessing arbitrary and possibly dynamic data which happen to fit into a list, table or tree presentations. Because this API has to accomodate everything from a simple dummy list of a few strings to the contents of an address book, including the rich contact details, it is inherently complex. Depending on what you want to achieve, using a one-purpose tool might be better in your specific situation.
By the way, the QFileSystemModel is very dynamic in nature (the directory enumeration happens on a separate thread). You won't get meaningful data until the directoryLoaded signal is emited, you have to wait for it. If you are simply looking for a list of files to use in your code, using Python's native facilities might be easier.

Qt View/Model/Data Paradigm: How to modify data?

I just read the Model/View documentation for Qt 4.8. I believe to understand how the Model/View (Delegates/SelectionModel) work. One thing that I'm unsure about is how to manipulate data when I have a for example a TreeView.
Imagine having a TreeView to display a list and buttons to remove elements from this list when an item/row is selected. I see two approaches here
1) In the slot of the PushButton I retrieve the SelectionModel and the ItemModel of the TreeView and call model->removeRow(index.row ...). This way the model (that i subclassed from QAbstractItemModel) manipulates the data that it is supposed to represent.
2) In the slot of the PushButton I remove the item directly from the data source, that the TreeView's model represents. Then I can link the data with the model via signals/slot, such that the model can then tell the TreeView that the underlying data has changed.
The same scenario can be imagined with adding elements. Should I add the new element to the data which signals its changed state to the ItemModel which the informs the TreeView, or should I add the new item through the ItemMode?
I haven't found any Best Practices documentation on this. The two approaches differ strongly, such I would like to know in advanced which one is preferable.
Does anybody have a suggestion which path to follow?
Thanks
1) is preferable - you should probably avoid manipulating your data source directly from the UI code - your life will be better if you go through the model. At the very least add a method to your model to do the data manipulation, and call that method from your UI code.
You will find that some of Qt's methods are protected such that they can only be called from the model itself (e.g. endInsertRows etc.)

QTableWidget, allow to type numbers only

I have a QTableWidget with four columns. I want the user to be able to insert only integers in the first three and a double in the fourth.
I believe that this can be done with delegate, but I have not found relevant examples (only some with QDateTimeEdit).
What should I do?
Look at the documentation for QItemDelegate; it provides a pretty good description on how it can be used.
Since with a delegate, you'll be able to provide your own custom editor, I would suggest that you use a QLineEdit with a validator set using setValidator(). I believe the classes QIntValidator and QDoubleValidator will be perfect in this situation.

Checkbox in Flex Datagrid Broken on Scrolling

I have a checkbox in a Flex DataGrid, and when I scroll, other rows are randomly checked/unchecked.
After reading over: Creating a column of RadioButtons in Adobe Flex
it's clear that the itemRenderers are getting recycled, but the problem I have with the solution presented there is it moves info about the view into the model.
Does anyone have a better way of solving it, that doesn't force me to put information for the UI into my actionscript model classes? (in my case, I am converting incoming XML data to actionscript classes, and these are getting bound to my datagrid).
Thanks for the help.
thanks everyone. great tips. unfortunately it was becoming too much overhead to keep the model pure, so i just polluted the model like the link in my original post. :( at least it works.
Chetan, neat idea.. i tried working with this for almost an entire day with no luck though.
brd6644, good thoughts on separating the two model classes.. i might go back and do this later.
You could create a subclass of DataGrid that internally stores what rows are checked/unchecked (Array/Collection of Boolean) but you would have a devil of a time keeping that in sync with the dataProvider when it is sorted or filtered. I suppose you could use a Dictionary that is keyed by the object in each index of the dataProvider and valued with a Boolean to indicate whether it's selected. That would at least isolate you from the sorting / filtering issues. This will not work if you have duplicate references in your dataProvider.
Alternatively, you could create a subclass of your ActionScript model class and add the "selected" property to it, then write some simple utility methods to "convert" between the two. That way your View deals only with the "ViewModel" class and other layers (especially the server side) deals only with the real "Model" class.
Adding to what cliff.meyers said, there is a third option of creating a custom IList class as described in this blog post by Alex Harui. It is pretty clever actually, and is cleaner as it doesn't require subclassing the component or polluting your model classes.

Resources