Results 1 to 2 of 2

Thread: QTableView setSortingEnabled not working inFrozen Column Example

  1. #1
    Join Date
    Feb 2014
    Posts
    43
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default QTableView setSortingEnabled not working inFrozen Column Example

    Hi when i use Frozen Column Example the sorting not working on QTableView . any bodu help me .

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "freezetablewidget.h"
    4. #include <CustomHeader.h>
    5.  
    6. FreezeTableWidget::FreezeTableWidget(QAbstractItemModel * model,CustomHeader *m_HeaderN)
    7. {
    8. setModel(model);
    9. frozenTableView = new QTableView(this);
    10. fcol=1;
    11. m_header1=new CustomHeader(Qt::Horizontal,this);
    12. m_header=m_HeaderN;
    13. m_header->setSortIndicatorShown(true);
    14. frozenTableView->setSortingEnabled(true);
    15. // frozenTableView->setHorizontalHeader(m_header1);
    16. // this->setHorizontalHeader(m_header);
    17. // m_header1->setMovable(false);
    18.  
    19. init();
    20.  
    21. //connect the headers and scrollbars of both tableviews together
    22. // m_header
    23. // frozenTableView->setHorizontalHeader(m_header);
    24. frozenTableView->setAlternatingRowColors(true);
    25.  
    26. connect(horizontalHeader(),SIGNAL(sectionResized(int,int,int)), this,
    27. SLOT(updateSectionWidth(int,int,int)));
    28. connect(verticalHeader(),SIGNAL(sectionResized(int,int,int)), this,
    29. SLOT(updateSectionHeight(int,int,int)));
    30.  
    31. connect(frozenTableView->verticalScrollBar(), SIGNAL(valueChanged(int)),
    32. verticalScrollBar(), SLOT(setValue(int)));
    33. connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
    34. frozenTableView->verticalScrollBar(), SLOT(setValue(int)));
    35.  
    36. connect(frozenTableView, SIGNAL(doubleClicked(const QModelIndex &)),this, SLOT(DoubleClickEmit(const QModelIndex &)));
    37. connect(m_header1, SIGNAL(searchStringM(int,QString)), this, SLOT(SearchEmit(int,QString)));
    38. connect(m_header1, SIGNAL(indexOfColumn(int)), this, SLOT(indexOfColumnEmit(int) ));
    39. //SUPRAJA JAN 20
    40. connect(m_header1, SIGNAL(unfilterM(int,QString)), this, SLOT( unfilterEmit(int,QString)));
    41. connect(m_header1, SIGNAL(triggered(QModelIndex)), this, SLOT( sortenab(QModelIndex)));
    42.  
    43.  
    44. }
    45. void FreezeTableWidget::sortenab(QModelIndex ind)
    46. {
    47. qDebug()<<"INDEXXXXXXXXXXX"<<ind;
    48. frozenTableView->sortByColumn(ind.column());
    49. }
    50.  
    51. FreezeTableWidget::~FreezeTableWidget()
    52. {
    53. delete frozenTableView;
    54. }
    55. void FreezeTableWidget::SearchEmit(int aa ,QString bb)
    56. {
    57. emit searchStringM(aa,bb);
    58. }
    59. void FreezeTableWidget::indexOfColumnEmit(int aa)
    60. {
    61. emit indexOfColumn(aa);
    62. }
    63. void FreezeTableWidget:: unfilterEmit(int aa ,QString bb)
    64. {
    65. emit unfilterM(aa,bb);
    66. }
    67.  
    68. void FreezeTableWidget::DoubleClickEmit(const QModelIndex Index)
    69. {
    70. qDebug()<<"INDEX IN FROZEN VIEWWWWWWWWWWWWWW"<<Index;
    71. emit DoubleClickEmitSignal(Index);
    72. }
    73.  
    74. void FreezeTableWidget::horizantalHeader_Hide()
    75. {
    76. frozenTableView->horizontalHeader()->hide();
    77. // QHeaderView *m_header1 = new CustomHeader(Qt::Horizontal);
    78. // frozenTableView->setHorizontalHeader(m_header1);
    79.  
    80. }
    81.  
    82.  
    83. void FreezeTableWidget::init()
    84. {
    85. // <<<<<< Both tables get the same header type
    86. // setHorizontalHeader(new CustomHeader(Qt::Horizontal, this));
    87. // frozenTableView->setHorizontalHeader(new CustomHeader(Qt::Horizontal, this));
    88. setHorizontalHeader(m_header);
    89. frozenTableView->setHorizontalHeader(m_header1);
    90.  
    91. frozenTableView->setModel(model());
    92. frozenTableView->setFocusPolicy(Qt::NoFocus);
    93. frozenTableView->verticalHeader()->hide();
    94. // frozenTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    95. frozenTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
    96.  
    97.  
    98. viewport()->stackUnder(frozenTableView);
    99. horizontalHeader()->stackUnder(frozenTableView); // <<<<<< make sure the header goes under the overlaid view too
    100. //! [init part1]
    101.  
    102. //! [init part2]
    103. frozenTableView->setStyleSheet("QTableView { border: none;"
    104. "background-color: #2e5c7f;"
    105. "alternate-background-color:#B4CFE4;"
    106. "selection-background-color: 2D347B}"); //for demo purposes
    107.  
    108. //setStyleSheet("background-color:#2e5c7f;" "alternate-background-color:#B4CFE4;" "selection-background-color: 2D347B ;" "color:white;" "border-width:0px;" );
    109. // frozenTableView->setSelectionModel(selectionModel());
    110. for (int col = 0; col < model()->columnCount(); ++col) // <<<<<< hide one less column
    111. if(col<fcol)
    112. frozenTableView->setColumnWidth(col, columnWidth(col) );
    113. else
    114. frozenTableView->setColumnHidden(col, true);
    115.  
    116. // frozenTableView->setColumnWidth(0, columnWidth(0) );
    117. // frozenTableView->setColumnWidth(1, columnWidth(1) ); // <<<<<< match the extra column's width
    118. //
    119. frozenTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    120. frozenTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    121.  
    122. frozenTableView->show();
    123.  
    124. updateFrozenTableGeometry();
    125.  
    126. setHorizontalScrollMode(ScrollPerPixel);
    127. setVerticalScrollMode(ScrollPerPixel);
    128. frozenTableView->setVerticalScrollMode(ScrollPerPixel);
    129. // frozenTableView->setSortingEnabled(true);
    130. qDebug()<<"Is Sorting Enabled"<<frozenTableView->isSortingEnabled ();
    131. }
    132. //! [init part2]
    133.  
    134.  
    135.  
    136. void FreezeTableWidget::updateSectionWidth(int logicalIndex, int /* oldSize */, int newSize)
    137. {
    138. if (logicalIndex == 0){
    139. frozenTableView->setColumnWidth(0, newSize);
    140. updateFrozenTableGeometry();
    141. }
    142. }
    143.  
    144. void FreezeTableWidget::updateSectionHeight(int logicalIndex, int /* oldSize */, int newSize)
    145. {
    146. frozenTableView->setRowHeight(logicalIndex, newSize);
    147. }
    148. //! [sections]
    149.  
    150.  
    151. //! [resize]
    152. void FreezeTableWidget::resizeEvent(QResizeEvent * event)
    153. {
    154. QTableView::resizeEvent(event);
    155. updateFrozenTableGeometry();
    156. }
    157. //! [resize]
    158.  
    159.  
    160. //! [navigate]
    161. QModelIndex FreezeTableWidget::moveCursor(CursorAction cursorAction,
    162. Qt::KeyboardModifiers modifiers)
    163. {
    164. QModelIndex current = QTableView::moveCursor(cursorAction, modifiers);
    165.  
    166. if (cursorAction == MoveLeft && current.column() > 0
    167. && visualRect(current).topLeft().x() < frozenTableView->columnWidth(0) ){
    168. const int newValue = horizontalScrollBar()->value() + visualRect(current).topLeft().x()
    169. - frozenTableView->columnWidth(0);
    170. horizontalScrollBar()->setValue(newValue);
    171. }
    172. return current;
    173. }
    174. //! [navigate]
    175.  
    176. void FreezeTableWidget::scrollTo (const QModelIndex & index, ScrollHint hint){
    177. if (index.column() > 0)
    178. QTableView::scrollTo(index, hint);
    179. }
    180.  
    181. //! [geometry]
    182. void FreezeTableWidget::updateFrozenTableGeometry()
    183. {
    184. int Hwidth=0;
    185. for(int i=0;i<fcol;i++)
    186. {
    187. Hwidth=Hwidth+columnWidth(i);
    188. }
    189. frozenTableView->setGeometry(verticalHeader()->width() + frameWidth(), frameWidth(),
    190. Hwidth, // <<<<<< the width of the two visible columns
    191. viewport()->height()+horizontalHeader()->height());
    192. }
    193. //! [geometry]
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableView setSortingEnabled not working inFrozen Column Example

    Try adding a QSortFilterProxyModel then it should work
    QAbstractItemModel has an empty sort() function

Similar Threads

  1. Replies: 4
    Last Post: 27th February 2014, 09:42
  2. Set an editable column for QTableView
    By SIFE in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2012, 23:09
  3. QTableView set column Decimals
    By ottoshmidt in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2010, 05:00
  4. QTreeWidget, setSortingEnabled
    By osiris81 in forum Qt Programming
    Replies: 3
    Last Post: 29th March 2010, 15:51
  5. QTableWidget - setSortingEnabled
    By bruccutler in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2007, 15:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.