Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Lines between QTreeWidget items
Qt 6.11 is out! See what's new in the release blog

[SOLVED]Lines between QTreeWidget items

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 10.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I'm trying to add some lines between items form a QTreeWidget( with only a column ) but when I'm hovering over the items, the lines disappear( only if I'm hovering from top to bottom!!! ). What I'm missing here?

    @void MyTreeWidget::drawRow( QPainter* p, const QStyleOptionViewItem &opt, const QModelIndex &idx ) const
    {
    QTreeWidget::drawRow( p, opt, idx );

    QModelIndex s = idx.sibling( idx.row(), 0 );
    if ( s.isValid() )
    {
    QRect rect = visualRect( s );
    int py = rect.y();
    int ph = rect.height();
    int pw = rect.width();
    p->setPen( QColor( 82, 82, 82 ) );
    p->drawLine( 0, py + ph, pw + 10, py + ph );
    }
    }@

    Thanks!

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      added a small working app for better viewing:

      @#include<QApplication>
      #include<QTreeWidget>
      #include<QHeaderView>
      #include<QPainter>

      class MyTreeWidget : public QTreeWidget
      {
      public:
      MyTreeWidget( QWidget* parent = 0 ) : QTreeWidget( parent )
      {
      setContentsMargins( 0, 0, 0, 0 );
      setColumnCount( 1 );

          header()->setVisible( false );
      }
      
      void drawRow( QPainter* p, const QStyleOptionViewItem &opt, const QModelIndex &idx ) const
      {
         QTreeWidget::drawRow( p, opt, idx );
      
         QModelIndex s = idx.sibling( idx.row(), 0 );
             if ( s.isValid() )
             {
                  QRect rect = visualRect( s );
                  int py = rect.y();
                  int ph = rect.height();
                  int pw = rect.width();
                  p->setPen( QColor( 82, 82, 82 ) );
                  p->drawLine( 0, py + ph, pw + 10, py + ph );
         }
      }
      

      };

      int main( int argc, char* argv[] )
      {
      QApplication app( argc, argv );
      MyTreeWidget* treeWidget = new MyTreeWidget;

      QTreeWidgetItem* treeItem1 = new QTreeWidgetItem( treeWidget, 0 );
      treeItem1->setText( 0, "test1" );
      
      QTreeWidgetItem* treeItem2 = new QTreeWidgetItem( treeWidget, 0 );
      treeItem2->setText( 0, "test2" );
      
      QTreeWidgetItem* treeItem3 = new QTreeWidgetItem( treeWidget, 0 );
      treeItem3->setText( 0, "test3" );
      
      QTreeWidgetItem* treeItem4 = new QTreeWidgetItem( treeWidget, 0 );
      treeItem4->setText( 0, "test4" );
      
      QTreeWidgetItem* treeItem5 = new QTreeWidgetItem( treeWidget, 0 );
      treeItem5->setText( 0, "test5" );
      
      treeWidget->show();
      
      return app.exec();
      

      }@

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Pfff, found the answer, just add @p->drawLine( 0, py, pw + 10, py );@ to drawRow method

        1 Reply Last reply
        0
        • I Offline
          I Offline
          infoctopus
          wrote on last edited by
          #4

          Consider using CSS styling, http://doc.qt.nokia.com/latest/stylesheet-reference.html see QTreeWidget/View

          Qt rulez

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved