質問

I implemented a custom QAbstractTableModel 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".

他のヒント

I just did this. First, you have to connect the header signal to the sort method you have created. This is a Python sample so you'll need to adapt it to C++:

model = SBStateTableModel()
table = QtGui.QTableView()
table.setModel(model)

table.setSortingEnabled(True)

When you sort, the entire view will change - or at least most of the view will change. So emitting the modelReset signal will cause the view to change. Model reset is one of the most inefficient signals to call, because it causes the entire view to be redrawn. However, most of the view will change anyway on a sort.

emit modelReset();

You could also emit the dataChanged signal, indicating that all of the data has changed. The dataChanged signal may be faster.

Python:

self.dataChanged.emit(self.index(0, 0), self.index(self.rowCount()-1, self.columnCount()-1))

C++:

(emitting a dataChanged signal, in a subclass of QTableView)

auto m = model();
emit dataChanged (m->index(0, 0), m->index(m->rowCount()-1, m->columnCount()-1));
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top