Custom QAbstractItemModel or QAbstractItemView like explorer

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật