Sorting QTableWidget



  • Hi I am not expert so please forgive me if I asked very easy question.
    i want to sort one of the column in QTableWidget, But when I do setSortingEnabled() true it only sorts it in alphabetic order though I have numbers in my cell. I tried understandign model view article in QtAssistant but believe me could not understand many thing and neither I could find any way how to solve me problem. I would be obliged if any of you experts can help me.



  • sheerin, Dont feel shy ask even if you dont know how create a widget and show, we are here to help you and eventually you will also help others sooner or later.

    To solve your problem, You just re implement bool QTableWidgetItem:: operator<(....) function enable sorting by calling setsortingEnabled(true) for your tableWidget.

    I am sure this will solve your problem, Have Happy Holidays...



  • Oh My god, I never expected such fast reply. I did post couple of question in popular Qt Centre but no reply Yet.
    But I am so sorry what should I implement in operator<<(), I can see this is there but I really dont knwo what what should be the code in this ? can you please help with some hint?



  • [quote author="Immii" date="1293177250"]QTableWidgetItem:: operator<<(....) function [/quote]

    I beleive it was only a typo from Immii, you have to reimplement the @QTableWidgetItem::operator<(...)@ (less than operator), which is used by sorting to compare items.



  • In general, if you implement operator<, yoou should also implement operator>, operator== and the opposit operators (operator!=, operator<=, operator>=) to be on the save way. If sorting ponly uses operator<, it will work, but perhaps some future algorithm will use operator>



  • Yes that was my TYPO <<, of course I wanted to say <.
    This is what you have to do Sheerin copy and paste in your class and you are golden :)
    @
    bool myListItem ::operator< ( const QTableWidgetItem &other ) const
    {
    if( this->text().toInt() < other.text().toInt() )
    return true;
    else
    return false;
    }@



  • [quote author="Gerolf" date="1293178708"]In general, if you implement operator<, yoou should also implement operator>, operator== and the opposit operators (operator!=, operator<=, operator>=) to be on the save way. If sorting ponly uses operator<, it will work, but perhaps some future algorithm will use operator>[/quote]

    Gerolf, No other operators are not required to be reimplemented just for sorting, Yes if some other features is required then it may required to re implement. As far as sorting is concerned < operator will be enough to return true or false to decide which table item should come first.



  • copied your exact code, and removed compiler errors, But I am not getting the result I wanted, it is still not sorting. Please help I have to submit my code today please please help



  • Can you just show the code (use '@' for code formatting) to check what should be the issue.



  • Hmmm.... Okay here is the code I wrote for you I hope you wont be having any problem and will be able integrate this in your project. BTW is this some college project? If you are still unable to get it done take some help with your other friends or write us back with your code

    @

    #include <QtGui>

    class myListItem: public QObject, public QTableWidgetItem
    {
    Q_OBJECT
    public:
    bool myListItem ::operator< ( const QTableWidgetItem &other ) const
    {
    if( this->text().toInt() < other.text().toInt() )
    return true;
    else
    return false;
    }
    };

    class MainWindow : public QMainWindow
    {

    public:
    MainWindow(QWidget *parent = 0)
    : QMainWindow(parent)
    {
    QWidget *w = new QWidget;
    setCentralWidget(w);
    QVBoxLayout *layout = new QVBoxLayout(w);
    QTableWidget *list = new QTableWidget(2,1);
    myListItem *item = new myListItem;
    item->setText(QString("%1").arg(10));
    list->setItem(0,0,item);
    item = new myListItem;
    item->setText(QString("%1").arg(15));
    list->setItem(1,0,item);

        list->setSortingEnabled(true);
        layout->addWidget(list);
    }
    

    };

    #include "main.moc"
    @



  • Thank you very much, Yes this solution works great. Now it looks to me that damn it was damn easy.
    I am sorry I am new to Qt and dont know much of it, Can I bug you guys again with my little questions ??
    This was simple but I did not get any reply from mailing list or from qt centre. so I think this site has more Qt guys and willing to help always.



  • We are here to help you so just ask questions if you have. We learn from them as well.



  • Good to hear finally you got it working. Yes you are most welcome to ask any damn easy or difficult question. we are here to help every one.



  • Alternatively you can use the QTableWidgetItem function to ensure integer sorting:

    virtual void   setData ( int role, const QVariant & value )
    

    e.g.
    // value is the integer value for the cell.
    QTableWidgetItem *it=tableWidget->item(row,col);
    it->setData(0, value);



  • [quote author="nala" date="1340607940"]Alternatively you can use the QTableWidgetItem function to ensure integer sorting:

    virtual void   setData ( int role, const QVariant & value )
    

    e.g.
    // value is the integer value for the cell.
    QTableWidgetItem *it=tableWidget->item(row,col);
    it->setData(0, value);
    [/quote]
    While correct, I doubt that the one asking the question was still looking for a solution. After all, the question is more than a year and a half old...



  • @Immii said:

    this

    here what does this mean? and for multiple tables how do we implement


  • Moderators

    @VInay123 Which posting a you refering to?



  • @Wieland It still not working about my sort problem, saw i tried to use this but still its not working


  • Moderators

    @VInay123 Like we already found out in another thread the reason it doesn't work for you is because you're using an ancient Qt version with a bug.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.