PDA

View Full Version : QTableWidget: Strange row selection after changing item while sorting is enabled



Tobi2504
20th April 2015, 11:37
Hi guys,

i have a QTableWidget with a few (in my case 5) columns and following properties:
- Select Row
- Sorting Enabled

When the table is sorted in the first column and I change the content an item of the first column, the row changes to the new position in the sorting order but the selection for some rows stays on the same row number as before. (see picture)

11104

After a few tests with different column numbers I saw, that only the first and the last column selection will change. When I click on any item, the selection goes to the normal behavior.

Does somebody have the idea to fix the problem?

Thanks,
Tobi

wysota
22nd April 2015, 09:05
Please provide a minimal compilable example reproducing the problem.

Tobi2504
22nd April 2015, 10:30
Hi,
here is a minimal example that produces the described issue. Btw I'm using Qt 5.4.1.

main.cpp


#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTableWidget>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:
QTableWidget *table;
};

#endif // MAINWINDOW_H


mainwindow.cpp


#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setFixedSize(550,200);


// Setup Table
table = new QTableWidget(this);
table->setColumnCount(5);
table->setRowCount(5);
table->setSelectionMode(QAbstractItemView::SingleSelectio n);
table->setSelectionBehavior(QAbstractItemView::SelectRows );
table->setSortingEnabled(true);

// filling the table
for(int i=0; i<table->rowCount(); i++)
table->setItem(i,0,new QTableWidgetItem(QString::number(i+1)));

// sort table
table->sortByColumn(0,Qt::AscendingOrder);

// Add table to window widget
setCentralWidget(table);
}

MainWindow::~MainWindow()
{
delete table;
}


When I change the number in the first cell from 1 to 6 I got the selection problem.

Santosh Reddy
22nd April 2015, 12:27
I think it is a bug.

I tried to change other column and row data randomly the app crashes with this message

ASSERT failure in QPersistentModelIndex::~QPersistentModelIndex: "persistent model indexes corrupted", file itemmodels\qabstractitemmodel.cpp, line 556