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.
Re: Custom widget table sort
Is it possible to sort QTableWidget by custom widget?
Re: Custom widget table sort
Yes it is, One way to do so is to create a CustomTableWidgetItem like this
Code:
{
...
public:
virtual bool operator<(const QTableWidgetItem& other) const
{
; // re-implement this, as per you sorting requirement
}
...
}
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.
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.
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.
Re: Custom widget table sort
You don't need to cast this, you just to cast other, your operator will look like this
Code:
...
}
bool CustomTableWidgetItem::operator<(const QTableWidgetItem& other) const
{
if(that != 0)
{
if(this->value() < that->value())
return true;
}
return false;
}
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.
Re: Custom widget table sort
Then do it properly (see #6).
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
Re: Custom widget table sort
It now both paints and sorts. Thanks.
In my custom table's add method:
Code:
...
setCellWidget(i, 1, bar);
setItem(i, 1, bar);