[SOLVED]Lines between QTreeWidget items



  • 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!



  • 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();
    

    }@



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.