[Solved] Detecting sort on QTableWidget
-
Hi,
I want to call a slot whenever the user clicks my QTableWidget's header to sort by that column, but none of the default signals seem to apply to this. Any suggestions? -
QTableWidget has a member signal named itemClicked(). The parameter is a pointer to a QTableWidgetItem object that was clicked. I would suggest you to compare the value the pointer points to with the QTableWidgetItems in the header part of the QTableWidget to find out which column was clicked. Then you can enable sorting by this column, I think sortItems() would be the best way to do this.
-
Hi!
There are no signals, but you can easily derive class from QTableWidget and override sortItem() method.
Example:
@class TableWidget : QTableWidget
{
Q_OBJECTpublic:
void sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder );signals:
void sort();public slots:
void onBeforeSort();
};void TableWidget::sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder )
{
emit sort();QTableWidget::sortItems(column, order);
}@Regards,
Jake -
[quote author="master of disaster" date="1356539132"]QTableWidget has a member signal named itemClicked(). The parameter is a pointer to a QTableWidgetItem object that was clicked. I would suggest you to compare the value the pointer points to with the QTableWidgetItems in the header part of the QTableWidget to find out which column was clicked. Then you can enable sorting by this column, I think sortItems() would be the best way to do this.[/quote]
Thanks for the suggestion, but I've tried that; clicking on the header items doesn't emit this signal.
[quote author="Jake007" date="1356540185"]Hi!
There are no signals, but you can easily derive class from QTableWidget and override sortItem() method.
Example:
@class TableWidget : QTableWidget
{
Q_OBJECTpublic:
void sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder );signals:
void sort();public slots:
void onBeforeSort();
};void TableWidget::sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder )
{
emit sort();QTableWidget::sortItems(column, order);
}@Regards,
Jake[/quote]Simple solution but sounds like it should work, I'll give it a try. Cheers!
-
It does not emit a signal, also if column is not sortable, code above will not emit a signal. You emit it manually only if sortItems is called.
Two notes of the code above:
- void onBeforeSort() was meant to connect it with a sort() signal. You can remove it if you don't need it.
- It will emit a signal even if it's trigger from code. Use blockSignals in this case.
-
[quote author="Jake007" date="1356558735"]It does not emit a signal, also if column is not sortable, code above will not emit a signal. You emit it manually only if sortItems is called.
Two notes of the code above:
- void onBeforeSort() was meant to connect it with a sort() signal. You can remove it if you don't need it.
- It will emit a signal even if it's trigger from code. Use blockSignals in this case. [/quote]
Okay, I see that now. But the problem is I only need to perform a particular action if the user sorts the table by clicking on the header; any sorting already performed in the code I have under control.
-
[quote author="wssddc" date="1356562858"]I think the signal sectionClicked from the horizontalHeader of your table should do what you want. (I use this, but I have table sorting disabled and do my own sorts.)
[/quote]So assuming it does do what I want, I would need to extend QTableWidget to create a custom table widget, that uses a horizontal header which is itself a custom widget?
-
-
[quote author="wssddc" date="1356650243"]I don't extend QTableWidget. The horizontal header is part of it. I just do
@
connect(ui->tableWidget->horizontalHeader(), SIGNAL(sectionClicked(int)),
this, SLOT(slot_table_clicked(int)));
@
and slot_table_clicked gets the column number that was clicked.
[/quote]I hadn't realised I could connect signals and slots in this way. That solved my problem perfectly, thanks!