PDA

View Full Version : Custom sort with QTableWidget



nicolas44
24th July 2007, 11:29
Hi,

I'd like to have my own sorting method for my QTableWidget.

I've tried two methods: subclass of QTableWidgetItem and subclass of QAbstractTableModel.

I'm trying to redefine the QTableWidgetItem::< and > operators, or QTableModel::itemGreaterThan or itemLessThan.

With the first method, my < operator is not called (QTableWidgetItem instead).

With the second method, I just can't call (segmentation fault) the rowCount method, despite the fact that I redefined it (with columnCount, data etc.).

First method:

class LMItem : public QTableWidgetItem
{
public :
// Constructeurs et destructeurs :
LMItem (QString donnee,int type);
~LMItem ();

// Méthodes d'accès :

// Méthodes publiques :
virtual bool operator<(const QTableWidgetItem &other) const;

//bool operator <(const LMItem& droite) const;
bool lessThan(const QTableWidgetItem& droite) const;
bool itemLessThan(struct QPair<class QTableWidgetItem *,int> const &,struct QPair<class QTableWidgetItem *,int> const &);
protected :
// Méthodes protégées :

private :
// Méthodes privées :

// Données membres :
};

call:
LMItem *index=new LMItem(QString("%1").arg(i),0);
ui.tableWidget->setItem(i,0,index);

I've tried this:

QTableWidgetItem *index=new LMItem(QString("%1").arg(i),0);
ui.tableWidget->setItem(i,0,index);

and this:
virtual bool operator<(const QTableWidgetItem &other) const;

Second method:

class LMTableModel : public QAbstractTableModel
{
Q_OBJECT

public:
LMTableModel(QObject *parent = 0){}

void setImage(const QImage &image){}

int rowCount(const QModelIndex &parent = QModelIndex()) const{return 0;}
int columnCount(const QModelIndex &parent = QModelIndex()) const{return 0;}

QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const{return QVariant();}
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const{return QVariant();}

private:

};

wysota
9th August 2007, 00:47
How did you reimplement the < operator? Can we see the code?