Unsolved Query in one requirement in Sorting columns in QtableView
-
I am using a QTableView and have two columns in it
- Index - displays the row number
- other is name of object .
For example
Index Object
0 /obj0
1 /obj1
2 /obj2
3 /obj3I am using
tView = new QTableView
mymodel = new QAbstractTableModel
proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(mymodel);
proxyModel->sort(0,Qt::AscendingOrder);
tView->setModel(d_proxyModel);When we sort the second column , i,e Object name , the index column also changes. and the view is as in example
3 /obj3
2 /obj2
1 /obj1
0 /obj0The requirement is there any way the Index column remains same for example after sorting Object column , the view remains
0 /obj3
1 /obj2
2 /obj1
3 /obj0 -
I am using a QTableView and have two columns in it
Index - displays the row number
other is name of object .
For example
Index Object
0 /obj0
1 /obj1
2 /obj2
3 /obj3I am using
tView = new QTableView
mymodel = new QAbstractTableModel
proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(mymodel);
proxyModel->sort(0,Qt::AscendingOrder);
tView->setModel(d_proxyModel);When we sort the second column , i,e Object name , the index column also changes. and the view is as in example
3 /obj3
2 /obj2
1 /obj1
0 /obj0The requirement is there any way the Index column remains same for example after sorting Object column , the view remains
0 /obj3
1 /obj2
2 /obj1
3 /obj0Please note I have gone through following example https://doc.qt.io/archives/4.6/itemviews-customsortfiltermodel.html but could solve my problem
-
I you only want to display the row number you should use setHeaderData() on the model and use QHeaderView in the view
-
I used , but I am facin certain issues . Please see my requirement in details
I have a application , in which I have a QWidget , The QWidget is divided into two parts using a QSplitter with vertical orientation.
The two parts are
-
QtreeWidget - onleft
-
QtableView on right.
The model used in QtableView is inherited from QAbstractTablemodel
When we select a row in QtreeWidget , the QtableView on right should be updated. The QtableView should display row number in ascending order and one column should display the object names . To display the row numbers , I am using setHeaderData of QAbstractTablemodel for vertical headers I am facing some issues in using setHeaderData for vertical headers.
Issues1
I am also using QSortFilterProxyModel , to sort the object name column .The issue i am facing , when I sort the column with object names by clicking on horizontal header of the column and then If I select the row , the row number in the vertical header changesIs there any way by the , I can display the row numbers in the vertical headers that do not change (while sorting the object names columns) .
-
-
@VRonin said:
setHeaderData
Could you proive some example of usage of setHeaderData for vertical headers
-
@Qt-Enthusiast said:
QSortFilterProxyModel
Subclass QSortFilterProxyModel override headerData method
to return section number+1 for vertical orientation for display role -
My code is something like this
class myModel:QAbstractTablemodel {
}tableModel = new myModel(db);
tableView = new QtableView;
if (db->isSortingEnabled()) {
proxyModel = new QSortFilterProxyModel(this);
d_proxyModel->setSourceModel(myModel);
proxyModel->sort(0,Qt::AscendingOrder);
tableView->setModel(proxyModel);tableView->sortByColumn(0,Qt::AscendingOrder); if( tableView->verticalHeader()) { tableView->verticalHeader()->setSortIndicator(0,Qt::AscendingOrder); }
} else {
tableView->setModel(myModel);
}QVariant myModel::data(const QModelIndex& index, int role) const {
}
QVariant myModel::rowCount(const QModelIndex& index, int role) const {
return rowMap.size();
}QVariant myModel::columnCount(const QModelIndex& index, int role) const {
return colMap.size();
}/*to display headers in the tableview */
QVariant myrModel::headerData(int section ,Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && (orientation == Qt::Horizontal) && section < COL_COUNT ) {
return QString((str_t)getColumnHeader(section));
}if (role == Qt::DisplayRole && (orientation == Qt::Vertical)) {
return QVariant(section);
}
return QVariant();
}I am getting two problems in it . One is vertical headers are displayed in descensding order and other is that when I select the row after sorting the column ,the vertcial header text changes
Please provide help to solve the problem
-
I am not able to solve this problem . Basically I want my row numbers to always in sequential order , even if user sorts a column by clicking on horizontal header.The vertical header should be static and it should display the row numbers in sequential orders . I tried the suggestions but they are not working . If some one can guide me with a example , it will be helpful
-
It is already built in if you use a table view.
check Table Model Example, just addview1->setSortingEnabled(true);
to enable the row sorting