[Qt-interest] Sorting widgets in QTableWidget

Vincent Huffaker vince at enventive.com
Thu Nov 19 18:35:07 CET 2009


Hi,

I have a QTableWidget with several columns including:

1) A column where each cell contains a class derived from QToolButton
2) A column where each cell is contains a checkbox
3) A normal "text" column
4) A double-value column

Sorting only works textually by default (so #3 is easy).  I was able to 
sort the numeric column by subclassing QTableWidgetItem and overriding 
the "<" operator, as follows:

    class NumericTableWidgetItem :
         public QTableWidgetItem
    {
    public:
        NumericTableWidgetItem():QTableWidgetItem()    {}
        bool NumericTableWidgetItem::operator <(const QTableWidgetItem&
    theItem) const
        {
            return this->text().toDouble() < theItem.text().toDouble();
        }
    };


This leaves me with the toolbutton-column and the checkbox-column.  I 
want to be able to sort by the state of the toolbutton, or the state of 
the checkbox.  I can't find a straightforward way to make this work. 

I could subclass QTableWidgetItem and make it contain a QToolButton 
pointer, but I don't see how to tell the QTableWidgetItem to use the 
QToolButton for display.  I could create a new subclass of 
QTableWidgetItem that could calculate the "<" value for all columns in 
my table, but I don't know how to make the table use my new class as the 
standard QTableWidgetItem (I couldn't get 
QTableWidget->setItemPrototype() to work -- are there any examples?)

Can anybody help? 

Thanks,
Vince




-- 
------------------------------------------------------------------------

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091119/1784cace/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vince.vcf
Type: text/x-vcard
Size: 308 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091119/1784cace/attachment.vcf 


More information about the Qt-interest-old mailing list