QListView how to add column? - qt

How can I add columns to QListView control. Found a method addColumn while seardhing, but in my Qt Creator 1.2.1 based on Qt 4.5.2(32 bit) QListView doesn't have such method at all !!!
So how would I add columns ?
Say I have 3 columns then what is the code to add a row ?

You can use QTableView for this purpose. But if you need QListView look & feel, you can use QTableView borderless using Qt Stylesheet. Also you may want to add an icon. You can add icons to your QTableView by setting icon data to Qt::DecorationRole.

You cannot add a column, cause list views are designed to view simple lists. You need QTable[View/Widget].

QListWidget is a single column only. Use QTreeWidget/View for multiple columns.

As the start point you could watch how works QTableView class here: http://qt.nokia.com/doc/4.2/sql-tablemodel.html and do the similar things with QListView. So, you can't just emit addColumn() for QListView class, first you need to create model and then do listView->setModel(model).

Related

How to add column before treecolumn in Qt qtreeview

I want to add a column before the tree column.
A similar thing is visible in the picture.
But I can't find any examples of that.
To achieve this. It is necessary to swap columns, as shown in the example. It works the same way on all Qt languages that have QTreeView.
QTreeView *view;
view->setModel(model);
view->header()->swapSections(0, 1);

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.

How to implement a table with multiple colums filter and QPushButton on each row?

I'm stuck. With QTableView + QStandartItemModel + QSortFilterProxyModel I can only add 1 QLineEdit for 1 specific column line_edit.textChanged.connect(filter_model.setFilterRegExp). Moreover I can't figure out how to add widget item to QTableView but only to QTableWidget (which i can't use because of filter?). I may give up the idea of adding widget and just make my whole row open another dialog on double click.But still I don't understand how to filter multiple columns at the same time. Thanks in advance
Moreover I can't figure out how to add widget item to QTableView
You need use QtGui.QItemDelegate for such thing. Have a look at this nice code snippet (not mine). And please read Qt manual about QItemDelegate and Model View Delegate pattern.

Need a GridView for Qt without QML

Is there a GridView implementation for native Qt (not for QML)? I need to read some data from model and put them into GridView.
GridView in QML support dynamic rows/columns, it's friendly to the users when resizing. Or, I have to implement it with QWidget and QGridLayout?
In my experience you have several possibilities:
As you said, use QGridLayout to display your custom data
QTableView works out of the box with Qt model classes, you can pretty much customize it easily to include widgets and other data as you wish.
Use QGraphicsView/QGraphicsScene to draw a grid, basically QML is built on top of QGraphicsView... it shouldn't be too hard.
QTableView or QTableWidget should be the starting points.
There is commercial data grid for Qt/C++ here http://www.devmachines.com/qtitandatagrid-overview.html We use it without any issues.

How to customize a listview in Qt

I want to customize a listview in Qt, can anyone provide me some example or hints of how to do it? I am new to Qt.
You can apply stylesheet to your QListView.
Check out here for the Qt documentation of customizing QListView using stylesheets.
If you're using a standard item model or a QListWidget (or any other model that uses QStandardItem), you can set appearance properties on the items using setData.
So, The following will add a red item to a list widget:
QListWidgetItem *colorItem = new QListWidgetItem("Red");
colorItem->setData(QBrush(QColor(Qt::red)), Qt::ForegroundRole);
list.addItem(colorItem);
For a working code example and more detailed explanation, please see: http://ynonperek.com/qt-mvc-customize-items

Resources