Sorting QTableWidget
-
[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.
-
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. -
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... -
@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
-
@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.