How to display drop-down in column of QTableView and filter based on drop-down - qt

I am newbie to Qt. I have to display a chunk of data in a QTableView and filter it column wise. For this I have used QSortFilterProxyModel, but as per requirement each column of the QTableView should have a drop-down list which shows unique values in that column. On selection of any of these values in the drop-down, only the rows having that particular value in the column should be displayed in the QTableView (Like you can do in Excel).
How would I implement this?

I had the same issue a week ago
I found a tutorial explaining how to do it. see link below
http://programmingexamples.net/wiki/Qt/Delegates/ComboBoxDelegate
Now my problem is how to retrieve the value of a specific combobox.
I think it is such a complex things to do in C++ and Qt display a combobox into a tableView.
For being a web developer at first I can tell that web language are better suited to do thoses kind of things.
But still some time performance matter and I tried to do it in C++ with Qt but it is not as easy as it seems to be in Web language.

This is very general question, and if I try to explain it all it will take pages, so it's better if you read the Qt model/view architecture documentation.
You can create your own class inherited from QTableView to create your customized table view. You have to use delegates for drop down functions and all. so read the QItemDelegate class documentation and documentation on subclassing delegates as well.

If you want to display it always and not just when editing, I would suggest setting a widget for the specific column like described in this thread: Qt - QTableView - Clickable button in table row

Related

Qt Quick Controls 2 and QAbstractTableModel

I have a model based on QAbstractTableModel that I would like to use to display its content in a list in QML.
Actually, I need to display the first column only.
TableView does not exist in Qt Quick Controls 2 and suffers from performance issue and rendering issues on HiDPI devices.
There is suggestion in this question to use a ListView. But I fail to see how I can tell the model that I need to display the first column only.
Is there any solution for this ?
This is automatically done in QML by ListView: the first column from the model is taken for any requested role.

What is the best way to implement this using Qt

I'm totally new to Qt, so I'd be glad to have a wide answer.
Here I drew up some model:
We have a kind of table that contains:
an integer value with a spinbox.
a cell with three(not specifically) grouped radio buttons
Editbox
A button that interacts with this particular editbox.
Also we have 2 buttons to add and remove items from the table.
I did some google search and found out that it can be done via QTableView.
Is there any way to put such complex structures into a cell? Must it be a separate class inherited from QTableView?
If you're going to have up to a hundred or maybe a few hundreds of elements in the table, then use QTableWidget.
If you're going to have too many elements (about thousands), then go for QTableView, and learn model-view programming.
The reason why I recommend QTableWidget is because you're a beginner. All you have to do there is create a widget, and use setCellWidget() and you're done.
If you have thousands of rows, then you're gonna have to draw the widgets yourself using QStyledItemDelegate, which will paint the widgets inside your QTableView. This is a very painful thing to do, but there's no way around it. The reasons you can find here.
I see at least three options to implement that in Qt:
Use a QtableView or QTableWidget and insert some custom controls in it. See comments made be other persons to your post
Use a QGridLayout and fill it with your controls by line and column
Make your own QWidget to store and manage the line elements (the spinbox, edit field, radio button) using a QHBoxLayout. You can design this in QtCreator, it can have it's own .ui. This could make it easy to handle the interaction between each QWidget of a line (directly handled by your QWidget class). Later, you can put an instance of it for every line you need in a QVBoxLayout.
Personnaly, I would go with the last option, but it may not work smartly if the controls of each line have different content/size (see comments), then first options should be prefered.

QML Nested List View with separators

I'm trying to implement a tree view that represent structure of buildings/floors/rooms. All rooms should be classified by floor and building ( thus header for each building and floor is required.
First I implemented it with Repeater, Row and Grid elements. However i need a selection behaviour so i need to use ListView and GridView. However I have problems displaying nested list views.
Here's my code so far: http://pastie.org/private/3seqntgvskbyxnmnuluaua
Expected Result( when using Grid and Repeater instead of GridView and List View ):
Haven't tried it myself, but here is how to implementation a tree view in QML.
http://www.codeproject.com/Articles/632795/QML-TreeModel-and-TreeView
There is still no official tree view component in Qt (as of version 5.2). However, see the solution provided by jens on this thread
http://qt-project.org/forums/viewthread/30521
This is by far the best tree view that I have come across. It should provide a good basis for what you are trying to do.
Looks like Qt Company is releasing a QML TreeView in Qt5.5. Alpha release should be coming out in February.
http://blog.qt.io/blog/2015/02/05/licensing-of-new-modules-in-qt-5-5/

Qt Database content view / manipulation as list view

I have a database, which tables should viewed in a widget. Seems simple, but I can't decide what to du or use.
Each row of the table should be viewed as one list view item, for instance, imagine table with this fields: id, title, content, date, number.
I need to view it as a list view (not hard-coded, it may also be another thing, if it is possible or better), and the label of the list view item should be the title field. But when the user clicks or double clicks on that item it should open all the contents of the current row in a separate widget. The all of these can be implemented easily by me, but I can't understand what to use: QListView with its model, or QListWidget? Or maybe QSqlTableModel? The last one is unfamiliar to me, I can read about from documentation, but if you have heard or met some kind things/applications, please provide a better solution for described problem.
Hope I could explain my problem correctly,
Thanks in advance.
Have you read about model/view programming in Qt? Basically you should use some model (QSqlTableModel, QSqlQueryModel, QSqlRelationalTableModel or create your own) an then attach it to QListView or QListWidget.

How do I tell Qt to always show an editor in a QTableView?

I've got a QTableView for which I want to display the last column always in edit mode. (It's a QComboBox where the user should be able to always change the value.)
I think I've seen the solution in the Qt documentation, but I can't find it anymore. Is there a simple way of doing it?
I think I could archive this effect by using openPersistentEditor() for every cell, but I'm looking for a better way. (Like specifying it only one time for the whole column.)
One way to get the automatic editing behaviour is to call the view's setEditTriggers() function with the QAbstractItemView::AllEditTriggers value.
To display the contents of a given column in a certain way, take a look at QAbstractItemView::setItemDelegateForColumn(). This will let you specify a custom delegate just for those items that need it. However, it won't automatically create an editor widget for each of them (there could in principle be thousands of them), but you could use the delegate to render each item in a way that makes it look like an editor widget.
There are two possibilities:
Using setIndexWidget, but Trolltech writes:
This function should only be used to
display static content within the
visible area corresponding to an item
of data. If you want to display custom
dynamic content or implement a custom
editor widget, subclass QItemDelegate
instead.
(And it breaks the Model/View pattern…)
Or using a delegate's paint method. But here you have to implement everything like enabled/disabled elements yourself.
The QAbstractItemModel::flags virtual function is called to test if an item is editable (see Qt::ItemIsEditable). Take a look at Making the Model Editable in the Model/View Programming documentation.
I can't see an easy way to do this, but you might be able to manage by using a delegate. I honestly don't know exactly how it would work, but you should be able to get something working if you try hard enough. If you get a proper delegate, you should be able to set it on a whole view, one cell of a view, or just a column or row.

Resources