PDA

View Full Version : QTableView setSortingEnabled not working inFrozen Column Example



RameshNani
13th February 2015, 07:23
Hi when i use Frozen Column Example the sorting not working on QTableView . any bodu help me .




#include <QtGui>

#include "freezetablewidget.h"
#include <CustomHeader.h>

FreezeTableWidget::FreezeTableWidget(QAbstractItem Model * model,CustomHeader *m_HeaderN)
{
setModel(model);
frozenTableView = new QTableView(this);
fcol=1;
m_header1=new CustomHeader(Qt::Horizontal,this);
m_header=m_HeaderN;
m_header->setSortIndicatorShown(true);
frozenTableView->setSortingEnabled(true);
// frozenTableView->setHorizontalHeader(m_header1);
// this->setHorizontalHeader(m_header);
// m_header1->setMovable(false);

init();

//connect the headers and scrollbars of both tableviews together
// m_header
// frozenTableView->setHorizontalHeader(m_header);
frozenTableView->setAlternatingRowColors(true);

connect(horizontalHeader(),SIGNAL(sectionResized(i nt,int,int)), this,
SLOT(updateSectionWidth(int,int,int)));
connect(verticalHeader(),SIGNAL(sectionResized(int ,int,int)), this,
SLOT(updateSectionHeight(int,int,int)));

connect(frozenTableView->verticalScrollBar(), SIGNAL(valueChanged(int)),
verticalScrollBar(), SLOT(setValue(int)));
connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
frozenTableView->verticalScrollBar(), SLOT(setValue(int)));

connect(frozenTableView, SIGNAL(doubleClicked(const QModelIndex &)),this, SLOT(DoubleClickEmit(const QModelIndex &)));
connect(m_header1, SIGNAL(searchStringM(int,QString)), this, SLOT(SearchEmit(int,QString)));
connect(m_header1, SIGNAL(indexOfColumn(int)), this, SLOT(indexOfColumnEmit(int) ));
//SUPRAJA JAN 20
connect(m_header1, SIGNAL(unfilterM(int,QString)), this, SLOT( unfilterEmit(int,QString)));
connect(m_header1, SIGNAL(triggered(QModelIndex)), this, SLOT( sortenab(QModelIndex)));


}
void FreezeTableWidget::sortenab(QModelIndex ind)
{
qDebug()<<"INDEXXXXXXXXXXX"<<ind;
frozenTableView->sortByColumn(ind.column());
}

FreezeTableWidget::~FreezeTableWidget()
{
delete frozenTableView;
}
void FreezeTableWidget::SearchEmit(int aa ,QString bb)
{
emit searchStringM(aa,bb);
}
void FreezeTableWidget::indexOfColumnEmit(int aa)
{
emit indexOfColumn(aa);
}
void FreezeTableWidget:: unfilterEmit(int aa ,QString bb)
{
emit unfilterM(aa,bb);
}

void FreezeTableWidget::DoubleClickEmit(const QModelIndex Index)
{
qDebug()<<"INDEX IN FROZEN VIEWWWWWWWWWWWWWW"<<Index;
emit DoubleClickEmitSignal(Index);
}

void FreezeTableWidget::horizantalHeader_Hide()
{
frozenTableView->horizontalHeader()->hide();
// QHeaderView *m_header1 = new CustomHeader(Qt::Horizontal);
// frozenTableView->setHorizontalHeader(m_header1);

}


void FreezeTableWidget::init()
{
// <<<<<< Both tables get the same header type
// setHorizontalHeader(new CustomHeader(Qt::Horizontal, this));
// frozenTableView->setHorizontalHeader(new CustomHeader(Qt::Horizontal, this));
setHorizontalHeader(m_header);
frozenTableView->setHorizontalHeader(m_header1);

frozenTableView->setModel(model());
frozenTableView->setFocusPolicy(Qt::NoFocus);
frozenTableView->verticalHeader()->hide();
// frozenTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
frozenTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);


viewport()->stackUnder(frozenTableView);
horizontalHeader()->stackUnder(frozenTableView); // <<<<<< make sure the header goes under the overlaid view too
//! [init part1]

//! [init part2]
frozenTableView->setStyleSheet("QTableView { border: none;"
"background-color: #2e5c7f;"
"alternate-background-color:#B4CFE4;"
"selection-background-color: 2D347B}"); //for demo purposes

//setStyleSheet("background-color:#2e5c7f;" "alternate-background-color:#B4CFE4;" "selection-background-color: 2D347B ;" "color:white;" "border-width:0px;" );
// frozenTableView->setSelectionModel(selectionModel());
for (int col = 0; col < model()->columnCount(); ++col) // <<<<<< hide one less column
if(col<fcol)
frozenTableView->setColumnWidth(col, columnWidth(col) );
else
frozenTableView->setColumnHidden(col, true);

// frozenTableView->setColumnWidth(0, columnWidth(0) );
// frozenTableView->setColumnWidth(1, columnWidth(1) ); // <<<<<< match the extra column's width
//
frozenTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
frozenTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;

frozenTableView->show();

updateFrozenTableGeometry();

setHorizontalScrollMode(ScrollPerPixel);
setVerticalScrollMode(ScrollPerPixel);
frozenTableView->setVerticalScrollMode(ScrollPerPixel);
// frozenTableView->setSortingEnabled(true);
qDebug()<<"Is Sorting Enabled"<<frozenTableView->isSortingEnabled ();
}
//! [init part2]



void FreezeTableWidget::updateSectionWidth(int logicalIndex, int /* oldSize */, int newSize)
{
if (logicalIndex == 0){
frozenTableView->setColumnWidth(0, newSize);
updateFrozenTableGeometry();
}
}

void FreezeTableWidget::updateSectionHeight(int logicalIndex, int /* oldSize */, int newSize)
{
frozenTableView->setRowHeight(logicalIndex, newSize);
}
//! [sections]


//! [resize]
void FreezeTableWidget::resizeEvent(QResizeEvent * event)
{
QTableView::resizeEvent(event);
updateFrozenTableGeometry();
}
//! [resize]


//! [navigate]
QModelIndex FreezeTableWidget::moveCursor(CursorAction cursorAction,
Qt::KeyboardModifiers modifiers)
{
QModelIndex current = QTableView::moveCursor(cursorAction, modifiers);

if (cursorAction == MoveLeft && current.column() > 0
&& visualRect(current).topLeft().x() < frozenTableView->columnWidth(0) ){
const int newValue = horizontalScrollBar()->value() + visualRect(current).topLeft().x()
- frozenTableView->columnWidth(0);
horizontalScrollBar()->setValue(newValue);
}
return current;
}
//! [navigate]

void FreezeTableWidget::scrollTo (const QModelIndex & index, ScrollHint hint){
if (index.column() > 0)
QTableView::scrollTo(index, hint);
}

//! [geometry]
void FreezeTableWidget::updateFrozenTableGeometry()
{
int Hwidth=0;
for(int i=0;i<fcol;i++)
{
Hwidth=Hwidth+columnWidth(i);
}
frozenTableView->setGeometry(verticalHeader()->width() + frameWidth(), frameWidth(),
Hwidth, // <<<<<< the width of the two visible columns
viewport()->height()+horizontalHeader()->height());
}
//! [geometry]

StrikeByte
13th February 2015, 10:39
Try adding a QSortFilterProxyModel then it should work
QAbstractItemModel has an empty sort() function