Results 1 to 8 of 8

Thread: Horizontal scroll bar on QTreeView

  1. #1
    Join Date
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Horizontal scroll bar on QTreeView

    Hello - I hope someone can help, I am stuck. I have a QTreeView, it displays data using a delegate with only a Paint function (it is not editable data) in a QDialog window. I cannot seem to get a horizontal scroll bar. The horizontal scroll bar policy is AsNeeded. I have set the width on column 0 (there is only one column) to wider than the width of the widget, no luck (after setting the model). I have set the size hint for the row in the model to wider than the width of the widget, no luck. I have set the SizeHint for the option/index in the delegate to wider than the width of the widget, no luck. I have set the paint rectangle to wider than the widget width - still no horizontal scroll bar. It truncates the text display (which is wider than the widget width). No one seems to have trouble getting a horizontal scroll bar... but I sure am! I get a vertical scroll bar without any problem.

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

    Default Re: Horizontal scroll bar on QTreeView

    May we see some code?
    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
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Horizontal scroll bar on QTreeView

    Here is some code, I've cut and pasted so if I left out something interesting, please let me know. Thanks in advance for your kind assistance.

    This is adding data rows to the model:
    Qt Code:
    1. {
    2. int the_level = outline->GetLevel();
    3.  
    4. XiObjectListIter<XiRwOutputColumn> iter( outline->GetColumnIter() );
    5. for (iter.Head(); iter.Valid(); iter.Next())
    6. iter.Data()->SetColumnText( iter.Data()->Evaluate() );
    7.  
    8. QVariant rpt_item_data;
    9. rpt_item_data.setValue<XiRwOutputLine>( outline);
    10. row->setData( rpt_item_data, Qt::DisplayRole );
    11. [B]QSize size = row->sizeHint();
    12. size.setWidth( 1500 );
    13. row->setSizeHint( size );[/B]
    14. row->setFlags( Qt::NoItemFlags );
    15. while (the_level <= indentations.last() && indentations.last() != 0 && (!outline->IsPreviewHdr().isEmpty() || outline->IsPreviewFtr() ) && rptItems.count() > 0) {
    16. rptItems.pop_back(); //line that is the current header
    17. rptItems.pop_back(); //that line's parent
    18. indentations.pop_back();
    19. }
    20.  
    21. if ( !outline->IsPreviewHdr().isEmpty() )
    22. {
    23. //append the header line, and then set me as the new parent
    24. rptItems.last()->appendRow( row );
    25. row->setData( outline->IsPreviewHdr(), Qt::UserRole+1 );
    26. indentations << the_level;
    27. if (rptItems.last()->rowCount() > 0) { //parent->rowCount() > 0
    28. rptItems << rptItems.last()->child(rptItems.last()->rowCount()-1); //parent = parent->child( parent->rowCount()-1 );
    29. rptItems << rptItems.last();
    30. }
    31. }
    32. else
    33. rptItems.last()->appendRow( row );
    34.  
    35. //after append:
    36. int rpt_cnt = rptItems.count();
    37. int child_cnt = rptItems.last()->rowCount();
    38. }
    To copy to clipboard, switch view to plain text mode 

    This is the size hint in the delegate:
    Qt Code:
    1. if (qVariantCanConvert<XiRwOutputLine>(index.model()->data(index, Qt::DisplayRole )))
    2. {
    3. XiRwOutputLine outline = qVariantValue<XiRwOutputLine>(index.model()->data( index, Qt::EditRole ));
    4.  
    5. QSize row_height = QStyledItemDelegate::sizeHint(option, index);
    6. row_height.setHeight( outline->GetFHeight() );
    7. [B]row_height.setWidth( 1500 );[/B]
    8. return row_height;
    9. }
    10. else{
    11. QSize row_height = QStyledItemDelegate::sizeHint(option, index);
    12. row_height.setHeight( row_height.height() );
    13. [B] row_height.setWidth( 1500 );[/B]
    14. return row_height;
    15. }
    To copy to clipboard, switch view to plain text mode 
    This is the paint function:
    Qt Code:
    1. QRect paint_rect = option.rect;
    2. XiRwOutputLine outline = qVariantValue<XiRwOutputLine>(index.model()->data( index, Qt::EditRole ));
    3. [B]paint_rect.setWidth( outline->GetWidth() );[/B]
    4. painter->fillRect(paint_rect, qVariantValue<QBrush>(index.model()->data( index, Qt::BackgroundRole )));
    5.  
    6. QPen orig_pen = painter->pen();
    7. QFont orig_font = painter->font();
    8. //set line font,
    9. QFont qfont = outline->GetQFont();
    10. QColor qcolor = outline->GetColor();
    11. QRect r = option.rect;
    12. //iterate through columns and create a print line for painting
    13. XiObjectListIter<XiRwOutputColumn> iter( outline->GetColumnIter() );
    14. for (iter.Head(); iter.Valid(); iter.Next())
    15. {
    16. painter->setFont( iter.Data()->GetColumnFont() );
    17. if( iter.Data()->GetColumnColor().isValid() )
    18. painter->setPen( iter.Data()->GetColumnColor() );
    19. else
    20. painter->setPen( qcolor );
    21.  
    22. r.setWidth( iter.Data()->GetWidth() );
    23. QString col_text( (char*)iter.Data()->GetColumnText());
    24. if( col_text.contains( "<!DOCTYPE html" ) )
    25. {
    26. q_txt.setHtml( col_text );
    27. QAbstractTextDocumentLayout::PaintContext context;
    28. q_txt.setPageSize( r.size());
    29. painter->translate(r.x(),r.y());
    30. q_txt.documentLayout()->draw(painter, context);
    31. painter->restore();
    32. }
    33. else
    34. {
    35. switch (iter.Data()->GetJustification()) {
    36. case XiRwOutputColumn::Right:
    37. painter->drawText(r,Qt::AlignRight,col_text);
    38. break;
    39. case XiRwOutputColumn::Center:
    40. painter->drawText(r,Qt::AlignCenter,col_text);
    41. break;
    42. case XiRwOutputColumn::Wrap:
    43. painter->drawText(r,Qt::TextWordWrap,col_text);
    44. break;
    45. default:
    46. painter->drawText(r,col_text);
    47. }
    48. }
    49. r.setLeft( r.right()+3);
    50. }
    51. painter->setFont( orig_font );
    52. painter->setPen( orig_pen ) ;
    53. }
    54.  
    55. else
    56. {
    57. QStyledItemDelegate::paint(painter, option, index);
    58. }
    To copy to clipboard, switch view to plain text mode 
    this is the code for setting the model in the rpt_view TreeView:
    Qt Code:
    1. if (realized)
    2. {
    3. report->finished = FALSE;
    4. rpt_view->setItemDelegate( new RWPreviewDelegate( ));
    5. rpt_view->setModel( rpt_model );
    6. [B]rpt_view->setColumnWidth( 0,1500 );[/B]
    7. if( report->finished )
    8. report->UnRealize();
    9. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Horizontal scroll bar on QTreeView

    How is your view set up? Does it have stretchLastColumn property set to true? What are the resize policies of the columns?
    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.


  5. #5
    Join Date
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Horizontal scroll bar on QTreeView

    Thank you so much for your quick response - it is appreciated. I'm using the Qt designer for this dialog, and all the items are set to the default except that I turned off word wrap and set the horizontal scroll bar to 'Always On'. I see a stretchHeaderLastSection, and that's checked. But I don't have a header, do I need one? The size policy for the QTreeView is Preferred, Expanding. I don't see a stretchLastColumn property or resize policies for individual columns - I must be looking in the wrong place, where do I find those? I set the resizeColumnToContents(0), but it doesn't seem to make a difference. Thanks again for your help.

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

    Default Re: Horizontal scroll bar on QTreeView

    Quote Originally Posted by LynneV View Post
    I see a stretchHeaderLastSection, and that's checked.
    Switch it off.
    But I don't have a header, do I need one?
    With a tree view you always have a header, it might not be visible though.
    I don't see a stretchLastColumn property or resize policies for individual columns - I must be looking in the wrong place, where do I find those?
    In the docs of QHeaderView

    I set the resizeColumnToContents(0)
    Bad idea.
    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. The following user says thank you to wysota for this useful post:

    LynneV (9th November 2010)

  8. #7
    Join Date
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Horizontal scroll bar on QTreeView

    The solution ended up to be: QStandardItemModel->SetHorizontalHeaderLabels( QStringList) AND QTreeView->setColumnWidth( 0, line_width ). Ether alone would not work..but together they do the trick! Why is resizeColumntoContents not a good idea? Just curious.

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

    Default Re: Horizontal scroll bar on QTreeView

    Quote Originally Posted by LynneV View Post
    Why is resizeColumntoContents not a good idea?
    Because you are probably calling it before the sizes are calculated so it's likely a no-op.
    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.


  10. The following user says thank you to wysota for this useful post:

    LynneV (10th November 2010)

Similar Threads

  1. Replies: 0
    Last Post: 5th July 2010, 09:05
  2. Resizing QTableView to eliminate horizontal scroll bar?
    By russford in forum Qt Programming
    Replies: 7
    Last Post: 18th March 2010, 07:21
  3. Vertical scroll bar, but resizable horizontal content
    By minimoog in forum Qt Programming
    Replies: 0
    Last Post: 14th January 2009, 20:51
  4. ListWidget horizontal scroll.
    By patrick772goh in forum Qt Tools
    Replies: 3
    Last Post: 17th July 2007, 07:32
  5. Replies: 1
    Last Post: 18th March 2006, 10:18

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.