qt qtableview 刷新列表,排序Qt表模型 - QTableView不会更新

I implemented a custom QAbstractTableModle and I'm using a std::vector for my data objects.

Now I wanted to implement the sort() method, to get my table sorted by column.

That's basically what I do:

void SBStateTableModel::sort (int column, Qt::SortOrder order)

{

emit layoutAboutToBeChanged();

switch (column)

{

case Address:

if (order == Qt::DescendingOrder)

std::sort(states.begin(), states.end(), addr_comp_desc);

else

std::sort(states.begin(), states.end(), addr_comp_asc);

default:

return;

}

emit layoutChanged();

}

But emitting layoutChanged() alone doesn't redraw the view. When mark a row and cycle through them, they get updated as they are being highlighted.

The documentation also speaks about updating the persistent indexes. Some people on here have suggested, that this is in fact not necessary.

I'm not even sure how to go about it. Getting the list with persistentIndexList() and then I have to sort it. But std::sort is not a stable sort. I'm not sure how to match the persistent indices with my vector indices.

EDIT:

There was just a "break" missing in the 'case'! So the function would return before emitting the layoutChanged signal.

解决方案

D'oh!

I was ready to dig into the Qt Source Code. But as I single stepped through my code, I saw the cursor jumping to the return statement in the 'default' case.

I had just forgotten to add a 'break' to my switch-case! It was just a simple fall-through error :((

It works now perfectly fine with "layoutChanged".

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt 中,QTableView 控件的多头可以通过 QHeaderView 控件来实现。QHeaderView 控件可以设置为水平和垂直两种模式,并且可以在两个方向上同时存在。 下面是一个简单的示例,演示如何在 QTableView 控件中添加多头: ```cpp // 创建 QTableView 控件 QTableView* tableView = new QTableView(this); // 设置数据模型 QStandardItemModel* model = new QStandardItemModel(this); tableView->setModel(model); // 添加数据 model->setColumnCount(3); model->setRowCount(5); for (int i = 0; i < model->rowCount(); i++) { for (int j = 0; j < model->columnCount(); j++) { QModelIndex index = model->index(i, j); model->setData(index, QString("Cell %1,%2").arg(i).arg(j)); } } // 设置水平头 QHeaderView* horizontalHeader = tableView->horizontalHeader(); horizontalHeader->setSectionsClickable(true); horizontalHeader->setStyleSheet("QHeaderView::section {" "background-color:#f0f0f0;" "border:1px solid #d8d8d8;" "padding:2px; }"); horizontalHeader->resizeSection(0, 100); horizontalHeader->resizeSection(1, 150); horizontalHeader->resizeSection(2, 200); // 添加竖直头 QHeaderView* verticalHeader = tableView->verticalHeader(); verticalHeader->setSectionsClickable(true); verticalHeader->setStyleSheet("QHeaderView::section {" "background-color:#f0f0f0;" "border:1px solid #d8d8d8;" "padding:2px; }"); verticalHeader->resizeSection(0, 50); verticalHeader->resizeSection(1, 75); verticalHeader->resizeSection(2, 100); verticalHeader->resizeSection(3, 125); verticalHeader->resizeSection(4, 150); // 将 QTableView 控件添加到布局中 layout->addWidget(tableView); ``` 在上面的示例中,首先创建了一个 QTableView 控件和一个数据模型。然后,使用 setColumnCount() 和 setRowCount() 函数设置了格的行数和列数,并添加了一些示例数据。 接下来,使用 horizontalHeader() 函数获取 QTableView 的水平头,使用 verticalHeader() 函数获取竖直头。然后,分别设置头的样式和大小。 最后,将 QTableView 控件添加到布局中。 通过这种方式,就可以在 QTableView 控件中添加多头了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值