Results 1 to 3 of 3

Thread: QTreeWidget fixed column size

  1. #1
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QTreeWidget fixed column size

    Hi,
    I want to add an additional icon in the QTreeItem to have option to set something visible or not when clicking on the icon.
    I have tried the delegate but the text of the item go behind the icon and I didn't found a way to have the "..." before the icon.
    Here the code of the delegate :
    Qt Code:
    1. class CItemDelegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. CItemDelegate( QObject* Parent = NULL ) :
    8. QStyledItemDelegate( Parent )
    9. {
    10. m_VisibleIcon = QPixmap( QIcon( "Images/Visible.png" ).pixmap( 20, 20 ) );
    11. }
    12.  
    13. void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
    14. {
    15. QStyledItemDelegate::paint( painter, option, index );
    16. painter->drawPixmap( ComputeVisibleIconPos( option ), m_VisibleIcon );
    17. }
    18.  
    19. bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
    20. {
    21. if( event->type() == QEvent::MouseButtonRelease )
    22. {
    23. QMouseEvent* MouseEvent = static_cast< QMouseEvent* >( event );
    24. const QRect VisibleButtonRect = m_VisibleIcon.rect().translated( ComputeVisibleIconPos( option ) );
    25. if( VisibleButtonRect.contains( MouseEvent->pos() ) )
    26. emit VisibleIconClicked( index );
    27. }
    28. return false;
    29. }
    30.  
    31. signals:
    32.  
    33. void VisibleIconClicked( const QModelIndex& );
    34.  
    35. private:
    36.  
    37. QPoint ComputeVisibleIconPos( const QStyleOptionViewItem& option ) const
    38. {
    39. return QPoint( option.rect.right() - m_VisibleIcon.width() - 2,
    40. option.rect.center().y() - m_VisibleIcon.height() / 2 );
    41. }
    42.  
    43. private:
    44.  
    45. QPixmap m_VisibleIcon;
    46. };
    To copy to clipboard, switch view to plain text mode 
    The second option is to have 2 columns but I didn't found a way to have the second column fixed width and the first column stretch correctly.
    The second column could have a QToolButton with a stylesheet on it to only show the image.
    I tried to have the icon on the first column and text on the second but decoration didn't worked good because I didn't found a way to have it on the second column.
    The delegate version is more clean because a no name column is not needed.
    How can I have the "..." before the icon ?
    If the solution is to have icon before the icon already on the item, i like the solution but I don't know how do it.
    Thanks for the help
    Last edited by Alundra; 7th January 2015 at 05:51.

  2. #2
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeWidget fixed column size

    I have tried :
    Qt Code:
    1. void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
    2. {
    3. QStyleOptionViewItem NewOption = option;
    4. NewOption.rect.setWidth( NewOption.rect.width() - m_VisibleIcon.width() - 2 );
    5. QStyledItemDelegate::paint( painter, NewOption, index );
    6. painter->drawPixmap( ComputeVisibleIconPos( option ), m_VisibleIcon );
    7. }
    To copy to clipboard, switch view to plain text mode 
    That works but the row selection color stops before the icon, that's the last issue.

  3. #3
    Join Date
    Aug 2011
    Location
    Lyon(France)
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Lightbulb Re: QTreeWidget fixed column size

    Hi,
    looking for a long time the web to fix a column width in a QTreeWidget, i finally found a workaround like that:

    Derive your own class from QTreeWidget, and implement a slot to handle the resize signals emitted by the QHeaderView.

    Qt Code:
    1. class MyTreeWidget : public QTreeWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MyTreeWidget( QWidget *parent = 0 );
    7. ...
    8.  
    9. protected slots:
    10. void onSectionResized ( int column, int oldSize, int newSize );
    11. ...
    12.  
    13. };
    To copy to clipboard, switch view to plain text mode 

    In the implementation file
    Qt Code:
    1. MyTreeWidget::MyTreeWidget( QWidget *parent ) : QTreeWidget( parent )
    2. {
    3. // reconnect the QHeaderView signal sectionResized to be managed by our class
    4. QHeaderView headerView = header();
    5. disconnect( headerView, SIGNAL(sectionResized(int,int,int)) );
    6. connect( headerView, SIGNAL(sectionResized(int,int,int)), SLOT(onSectionResized(int,int,int)) );
    7. ...
    8. }
    9.  
    10. void MyTreeWidget::onSectionResized ( int column, int oldSize, int newSize )
    11. {
    12. // Set the first column width fixed
    13. if( column <= 1 )
    14. {
    15. if( columnWidth( 0 ) != indentation() * 2 )
    16. setColumnWidth( 0, indentation() * 2 );
    17. }
    18. else
    19. QTreeWidget::columnResized( column, oldSize, newSize );
    20. }
    To copy to clipboard, switch view to plain text mode 

    Notice that the column width is set only one time;

    I hope this could help

Similar Threads

  1. QTreeWidget column size problem
    By Cruz in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2012, 14:36
  2. Replies: 0
    Last Post: 14th February 2012, 16:43
  3. Fixed (Freeze) column in QTableWidget
    By chatarpal in forum Qt Programming
    Replies: 4
    Last Post: 21st January 2012, 07:03
  4. Replies: 4
    Last Post: 20th November 2009, 13:25
  5. Fixed Column in QTreeview
    By ormonde in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 08:49

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.