I’ve been experimenting with the model view concept in qt and c++ for some time now. My goal is to present the data in a table like Windows file explorer can do…
There is no problem with tabular data presentation. It only occurs when the cursor is implemented…
The cursor breaks into individual cells in both QTreeView and QTableView.
No matter how I try, every time the cursor breaks up into individual cells, which can at least be controlled with the cursor keys. But I don’t want this behavior. I don’t want to work with individual cells at all, and the gui “shouldn’t” allow cells to be selected, marked, moved around in them, etc… (even if the cells had editing disabled).
Only the treeview option in the basic settings works correctly. Once I enable multiple row selection. The cursor immediately breaks into cells.
I am wondering which solution is correct in the end. If this behavior affects the model (data is not stored in rows with one column but in rows and columns) or if this behavior affects the view.
I am also thinking about whether it is possible to somehow configure and modify qtreeview or whether to create a completely new view based on qtreeview or qtableview
Can anyone point me in the right direction? I’ve already consulted ChatGpt, it’s a great help, but it couldn’t help me with this. Maybe I asked him the wrong questions.
Test code here:
#include <QApplication>
#include <QTreeView>
#include <QTableView>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QVBoxLayout>
#include <QWidget>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// Create base layout
QWidget mainWidget;
QVBoxLayout layout(&mainWidget);
// Create 3 rows and 3 columns model
QStandardItemModel model(3, 3);
model.setHorizontalHeaderLabels({"Code", "Name", "Description"});
// insert data
for (int row = 0; row < 3; ++row)
{
for (int column = 0; column < 3; ++column)
{
QStandardItem *item = new QStandardItem(QString("Data %1").arg(row * 3 + column + 1));
model.setItem(row, column, item);
}
}
// Create QTreeView
QTreeView view1;
view1.setModel(&model);
view1.setRootIndex(model.index(-1, 0));
view1.setRootIsDecorated(false); // Hide expand/collapse ikon
view1.setSelectionBehavior(QAbstractItemView::SelectRows);
view1.setSelectionMode(QAbstractItemView::MultiSelection);
// Create QListView
QTableView view2;
view2.setModel(&model);
view2.setRootIndex(model.index(-1, 0));
view2.setSelectionBehavior(QAbstractItemView::SelectRows);
view2.setSelectionMode(QAbstractItemView::MultiSelection);
layout.addWidget(&view1);
layout.addWidget(&view2);
mainWidget.show();
return app.exec();
}
Bad example:
Ideal target solution:
Since I’m not a complete professional, it’s quite difficult for me to determine which object is best for achieving the goal. It seems that no object (QTreeView, QListView, QTableView) is correct and I need to create a completely new custom view.
- QTreeView is primarily built for a tree structure, so I need to remove all tree elements
- QTableView is primarily built to work with cells, so I need to rebuild it from cells to rows
- QListView uses only one column, so I need to implement a multi-column feature.
It almost seems to me that I should create some new view (MyRowView) that combines QTableView and QTreeView.
Can you advise me how to do it?
Addition:
To be unclear. I know that index and cursor are two things. But I need to work with whole rows. I just need to know that I’m standing on line X, I don’t need column information for anything.
If you look at the BASIC settings of QTreeView, it is not possible to get the “selected row” and “current index” to be different from each other (desired behavior). But then I turn on multi-line marking and suddenly the two are separated.
It’s not just about “bad” behavior, it’s also about the fact that you can’t do anything like that with programs like Explorer. You cannot get the current index for example on the file “size” column in File Explorer. You can’t get it at “writetime” either.
Whether you’re clicking the mouse, moving the cursor keys, multi-line marking, you’re still in full-line(row) mode. But I can’t achieve this concept in QT :-(.
And I don’t know if problem is in model, or in view. If I must create own new View, or use any existing object(treeview, tableview) + setting any property?
10