Results 1 to 2 of 2

Thread: Custom sort with QTableWidget

  1. #1
    Join Date
    May 2007
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Custom sort with QTableWidget

    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:isplayRole) const{return QVariant();}
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt:isplayRole) const{return QVariant();}

    private:

    };

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom sort with QTableWidget

    How did you reimplement the < operator? Can we see the code?

Similar Threads

  1. QTableWidget won't sort cellwidgets!!!
    By Arsenic in forum Qt Programming
    Replies: 7
    Last Post: 21st July 2007, 10:41
  2. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46

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.