What is the best way to implement this using Qt - 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.

Related

Complex content in QTableView

We would like to do a TableView that allows complex content in its cells.
The TableView should be as generic that I can do simple stuff like in picture 1. The left image is a simple example, where I fill a simple TableModel, set it for the TableView and display it.
But what if I want to add more complex content to one cell? Please again look at the first picture. The right part is more complex, for every cell we want to display an image, a description, and more description, so three items in one cell.
I understand, that I can put widgets to the cells of a TableView.
But, if I want to have a proper TableModel in the background, how would I store the data?
On top, the view should automatically resize when I make the widget of the TableView smaller the content should adapt
So if I use TableView and want to resize, I would have to shovel the content from one colum to another.
From what I understand, the columns also define the layout.
Would I be better of if I used a QGridLayout for this purpose?
Do I have to define a completely new model for QGridLayout?
Thanks for any help!
I am a newbie to QT and would appreciate your input a lot!
Qt's proposed solution to having a complex view in each cell of a table view is to use a custom delegate. Take a look at Star Delegate example, it demonstrates exactly this technique.
There are basically two options to proceed with a custom delegate: either you subclass QStyledItemDelegate (or its base class QItemDelegate if you need to draw the items of Qt's datatypes somewhat specially) or subclass QAbstractItemDelegate to have the full control over the delegate's appearance and behaviour.
However, your second requirement of automatic layout rearrangement on widget resizing suggests that your view doesn't really has to follow the underlying table's schema. Qt has a flow layout example which implements a layout with exactly this rearrange-on-resize property and I suppose the simplest approach would be just using this layout along with custom widgets representing the table model's cells. To make it happen you could implement a custom view class listening to the model's signals and creating/deleting widgets and updating the flow layout as necessary. This book, even though a little outdated nowadays as it covers Qt4, contains a chapter (#6) on implementing a custom view which is not a subclass of QAbstractItemView but is just a widget updating itself as its underlying model updates. To me it feels the right approach to your problem.

Customize QListWidgetItem

I am creating my first app in QT and wanted to design a list. The listitem has two texts and one icon.
The problem is, i cant find any example or helping material, Only helping link i found is : Customize QListWidgetItem but i dont understand it. Although i have the same problem which this links points to...
What i understand is, i have two options:
1- Customize QListWidgetItem to use with QListWidget
2- Make some delegate to use with QListView
I was hoping to get started with option 1. Now in the link, some reply talks about "myItem" which is taking 2 texts as input. I want to know the implementation of "myItem".
In the link you posted, MyItem is just a normal QWidget. This means that you can create a widget in Qt Designer and then set that new widget you created as the widget that the QListWidgetItem should use for display. In the above example, MyItem takes two strings because there is a vertical layout with two labels in it (that's my assumption at least).
You should also note, and is discussed in the above link, that taking the approach of setting an item widget to use for every item in the list is an expensive thing to do in terms of performance and memory consumption. Because a QListWidget is a QListView, you can set an item delegate on it just like any other view and come out with a lighter weight solution (one instantiation of class vs. one instantiation for every item in your list)

Displaying QAbstractListModel items in QTableView columns

I've been toying around with Qt and ran into a small issue.
I want to display a list of pictures as a table of icons. Right now, I'm doing this by subclassing QAbstractTableModel, and plugging it into a subclass of QTableView.
This, in my opinion, overly complicates the code, the model and the view (especially when trying to edit/append items). When trying to implement the model as a QAbstractListModel, the items are displayed as table rows.
Is there a way to make QTableView display items as columns, instead?
Edit: Such that the items are rendered in a single row from left to right, and wrapped to a new row.
Or is it preferable to use the table model for table views in any case and work around the issues?
It's worth mentioning I'm using C# bindings for Qt based on Qt Jambi.
The QListView has exactly the functionality you are talking about.
If you don't need any functionality specific to QTableView, then I would suggest switching.
If you set "isWrapping", then the list will start from the top, go down to the bottom, then wrap to a new column.
Set "flow" to LeftToRight to display the list in rows instead of columns
You might also need to set "resizeMode", instead of Fixed, to Adjust. Which will automatically move things around when the list is resized.
Hope that helps.
Just as a side note, here is FlowLayout example. So if you just want to display a set of images in a self-adjusting grid, this would do it for you without all the overhead of a list/table. However, it doesn't give you any selection/editing capabilities or anything just layout.
I don't understand something here. You implement TableModel, so you can prepare data for view in the most desirable way. Suppose you have data A,B,C,D, then you can return A(00),B(01),C(10),D(11), or for example A(00),C(01),B(10),D(11). Is it what you want to do?

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

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

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