0

I need to create a button beside header text like this for sorting. enter image description here

The requirements are:

  • the button and the header text should be in a group to be aligned together
  • when I click on the button up_arrow, the list will be sorted (in the picture it is not sorted :D), and the icon changes into down_arrow for sorting in the opposite order.

enter image description here

How can I do this?

gnase
  • 580
  • 2
  • 10
  • 25
  • I think you don't need a button, well, if Username column get sorted, what about Adress column? Take a look at https://stackoverflow.com/questions/372391/how-to-automatically-sort-a-qtreewidget-column – Phiber Aug 16 '18 at 12:31

1 Answers1

0

If you're happy with the default indicator's look and behaviour the following code should be sufficient:

//Set up QTreeView, add model etc...
tree_view->setSortingEnabled(true);
QHeaderView* header = tree_view->header();
header->setSortIndicatorShown(true);

If you would like to customise the behaviour of the sorting, have a look at QSortFilterProxyModel.

To style the indicators you could perhaps use Qt's Style Sheet:

QHeaderView::down-arrow {
  image: url(down_arrow.png);
}

QHeaderView::up-arrow {
  image: url(up_arrow.png);
}
A.Fagrell
  • 1,052
  • 8
  • 21
  • thank you. But I am not satisfied with default indicator. Besides, I think in this case I want to put text of header and the icon in a group together. So I guess I have create something like delegate for the header :D – gnase Aug 17 '18 at 12:46
  • @gnase I'm not sure what you mean by "put text of header and the icon in a group". Currently, if you use the default behaviour the indicator and the text are grouped - clicking on the header will also change the sorting. What is it that you need that the default behaviour/look doesn't give you? – A.Fagrell Aug 17 '18 at 13:06
  • Currently, when I use the default indicator, then the position of that indicator is `above` the header text, but I want the indicator `besides` the header text. Therefore I said I want to group the text and the indicator together. – gnase Aug 17 '18 at 13:18
  • Have you applied a style? which OS are you on? I guess that's the default location for your style. It's always possible to override using QStyleSheet or using QProxyStyle. Have you tried something like `QHeaderView::up-arrow { subcontrol-origin:padding; subcontrol-position: center right;}`? – A.Fagrell Aug 17 '18 at 15:43
  • I use Windows. I thought about it again. We need something more flexibel. So what i am trying to do now is create a Delegate.and then use `header()->setItemDelegateForRow( 0, new HeaderDelegate( this ) )` :) – gnase Aug 20 '18 at 07:11