Results 1 to 11 of 11

Thread: Custom widget table sort

  1. #1
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Custom widget table sort

    I have QTableWidget with custom progress bars (derived from QWidget), inserted into table with setCellWidget. Each bar have "progress" property and I need to sort by it. I reimplemented "<" operator, but sorting doesn't work and operator "<" not even called.

    Header:
    Qt Code:
    1. bool operator < (const ProgressBar &other);
    To copy to clipboard, switch view to plain text mode 
    CPP:
    Qt Code:
    1. bool ProgressBar::operator < (const ProgressBar &other)
    2. {
    3. return progress < other.progress;
    4. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Custom widget table sort

    What exactly are you "sorting"? How are you calling the sort? If you are trying to sort widgets then it won't work Well... actually it will sort them but nothing in Qt expects widgets to be sorted so if you're hoping for some automatic behaviour, nothing like that will happen.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget table sort

    Is it possible to sort QTableWidget by custom widget?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Custom widget table sort

    Yes it is, One way to do so is to create a CustomTableWidgetItem like this

    Qt Code:
    1. class CustomTableWidgetItem : public QWidget, public QTableWidgetItem
    2. {
    3. ...
    4. public:
    5. virtual bool operator<(const QTableWidgetItem& other) const
    6. {
    7. ; // re-implement this, as per you sorting requirement
    8. }
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 

    CustomTableWidgetItem gives you an interface for both you Widget (QProgressBar in your case) and QTableWidgetItem
    Warning: You need to properly implement operator<(), as the the parameter "other" is not guarantied to be a CustomTableWidgetItem (unless all the items in your QTableWidget are of type CustomTableWidgetItem)

    You can also inherit CustomTableWidgetItem from QProgressBar instead of QWidget, it's up to you.

  5. #5
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget table sort

    All elements in given column will be custom progress bars.
    In operator "<" you pass "const QTableWidgetItem&", but I need to access to the custom progress bar's property, it seems that I need to cast "this" and "other" to custom progress bar type, but dynamic_cast or "(ProgressBar*) this" doen't work.

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

    Default Re: Custom widget table sort

    If you want to do it properly then store the value of progress in your model (aka item data) and apply a custom delegate that will render the looks of a progress bar to the element. Search the forum for information how to implement a custom delegate showing a progress bar.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Custom widget table sort

    You don't need to cast this, you just to cast other, your operator will look like this
    Qt Code:
    1. class CustomTableWidgetItem : public QProgressBar, public QTableWidgetItem{
    2. ...
    3. }
    4. bool CustomTableWidgetItem::operator<(const QTableWidgetItem& other) const
    5. {
    6. const QProgressBar *that = dynamic_cast<const QProgressBar *>(&other);
    7. if(that != 0)
    8. {
    9. if(this->value() < that->value())
    10. return true;
    11. }
    12. return false;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 10th June 2011 at 12:03. Reason: updated contents

  8. The following user says thank you to Santosh Reddy for this useful post:

    giantdragon (10th June 2011)

  9. #8
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget table sort

    Sorting now works, thanks for your answer.
    But another problem emerged - I use custom progress bar (not QProgressBar) and reimplemented paintEvent to draw my own progress bar. If I add my bar to table with "setCellWidget" function sorting works, but I see white area instead of progress indicator. If I add it with "setItem" function painting works well, but sorting doesn't work.

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

    Default Re: Custom widget table sort

    Then do it properly (see #6).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Custom widget table sort

    You need to use both setItem(), and setCellWidget().
    You need set the CustomTableWidgetItem on the table using setItem(), and use setCellWidget() somewhere in the ctor (or relevant method) of CustomTableWidgetItem

  12. The following user says thank you to Santosh Reddy for this useful post:

    giantdragon (10th June 2011)

  13. #11
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget table sort

    It now both paints and sorts. Thanks.

    In my custom table's add method:
    Qt Code:
    1. ...
    2. setCellWidget(i, 1, bar);
    3. setItem(i, 1, bar);
    To copy to clipboard, switch view to plain text mode 
    Last edited by giantdragon; 10th June 2011 at 12:40.

Similar Threads

  1. Custom widget won't size properly in a table cell
    By zeroknowledge in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2011, 16:32
  2. QTableWidget custom sort
    By mattc in forum Qt Programming
    Replies: 1
    Last Post: 24th August 2010, 11:01
  3. how to SORT table of numbers with QT
    By tommy in forum Qt Programming
    Replies: 4
    Last Post: 29th May 2009, 09:34
  4. sort mysql table
    By eleanor in forum Qt Programming
    Replies: 2
    Last Post: 10th October 2007, 16:42
  5. Custom sort with QTableWidget
    By nicolas44 in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 00:47

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.