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. How to change Qtreeview expand or collapse icon position
Forum Updated to NodeBB v4.3 + New Features

How to change Qtreeview expand or collapse icon position

Scheduled Pinned Locked Moved Unsolved General and Desktop
27 Posts 3 Posters 10.8k Views 2 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.
  • A abdullahzubair109

    @mrjj can you kindly post a pseudo code?? i tried modifying QStyleOptionViewItem rect under paint function.. but got weird result

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #17

    @abdullahzubair109
    well that was my idea too but altering the options rect might do strange stuff and
    we might end up needing to draw the text manually for it to be possible.

    • but got weird result
      in what way ?
    A 1 Reply Last reply
    0
    • mrjjM mrjj

      @abdullahzubair109
      well that was my idea too but altering the options rect might do strange stuff and
      we might end up needing to draw the text manually for it to be possible.

      • but got weird result
        in what way ?
      A Offline
      A Offline
      abdullahzubair109
      wrote on last edited by
      #18

      @mrjj i am using painter->drawText();

      mrjjM 1 Reply Last reply
      0
      • A abdullahzubair109

        @mrjj i am using painter->drawText();

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #19

        @abdullahzubair109
        Hi
        tested a bit.
        seems to be possible
        alt text

        class TextDelegate : public QStyledItemDelegate
        {
        
        public:
            TextDelegate(QObject *parent)
                : QStyledItemDelegate(parent)
            {
        
            }
            QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
                QSize ret = QStyledItemDelegate::sizeHint(option, index);
                return ret;
            }
            void paint(QPainter *painter, const QStyleOptionViewItem &option,
                       const QModelIndex &index) const
            {
                QStyleOptionViewItem itemOption(option);
        
                initStyleOption(&itemOption, index);
                itemOption.rect.adjust(130, 0, 0, 0);
                QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &itemOption, painter, nullptr);
        
            }
        };
        

        ui->treeWidget->setStyle( new MyStyleProxy );
        ui->treeWidget->setItemDelegate( new TextDelegate(this) );

        A 1 Reply Last reply
        1
        • mrjjM mrjj

          @abdullahzubair109
          Hi
          tested a bit.
          seems to be possible
          alt text

          class TextDelegate : public QStyledItemDelegate
          {
          
          public:
              TextDelegate(QObject *parent)
                  : QStyledItemDelegate(parent)
              {
          
              }
              QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
              {
                  QSize ret = QStyledItemDelegate::sizeHint(option, index);
                  return ret;
              }
              void paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const
              {
                  QStyleOptionViewItem itemOption(option);
          
                  initStyleOption(&itemOption, index);
                  itemOption.rect.adjust(130, 0, 0, 0);
                  QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &itemOption, painter, nullptr);
          
              }
          };
          

          ui->treeWidget->setStyle( new MyStyleProxy );
          ui->treeWidget->setItemDelegate( new TextDelegate(this) );

          A Offline
          A Offline
          abdullahzubair109
          wrote on last edited by
          #20

          @mrjj i tried something like this, but didn't work

          void StyledDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
          {
          
          	if (index.column() == 0) {
          	
          		QString qs = index.data().toString();
          		QPoint pt = option.rect.center(); 
          		pt += QPoint(40, 0);
          		painter->drawText( pt, qs);
          	}
          	QStyledItemDelegate::paint(painter, option, index);
          
          }
          
          mrjjM 1 Reply Last reply
          0
          • A abdullahzubair109

            @mrjj i tried something like this, but didn't work

            void StyledDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
            
            	if (index.column() == 0) {
            	
            		QString qs = index.data().toString();
            		QPoint pt = option.rect.center(); 
            		pt += QPoint(40, 0);
            		painter->drawText( pt, qs);
            	}
            	QStyledItemDelegate::paint(painter, option, index);
            
            }
            
            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #21

            @abdullahzubair109
            ok, but what was wrong ? it didnt move text ?

            A 1 Reply Last reply
            0
            • mrjjM mrjj

              @abdullahzubair109
              ok, but what was wrong ? it didnt move text ?

              A Offline
              A Offline
              abdullahzubair109
              wrote on last edited by
              #22

              @mrjj nope

              mrjjM 1 Reply Last reply
              0
              • A abdullahzubair109

                @mrjj nope

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #23

                @abdullahzubair109
                well the style+delegate works fine but selection starts at text.
                If you really want seletion to start at icon, you have to dig into
                drawControl and implement the part you want.
                you can use
                https://code.woboq.org/qt5/qtbase/src/widgets/styles/qcommonstyle.cpp.html
                to easy dig around source.

                A 1 Reply Last reply
                0
                • mrjjM mrjj

                  @abdullahzubair109
                  well the style+delegate works fine but selection starts at text.
                  If you really want seletion to start at icon, you have to dig into
                  drawControl and implement the part you want.
                  you can use
                  https://code.woboq.org/qt5/qtbase/src/widgets/styles/qcommonstyle.cpp.html
                  to easy dig around source.

                  A Offline
                  A Offline
                  abdullahzubair109
                  wrote on last edited by
                  #24

                  @mrjj thanks for your time.. i will check tomorrow .. when you are free, can you look at my code in more details why drawText didn't work??

                  mrjjM 1 Reply Last reply
                  0
                  • A abdullahzubair109

                    @mrjj thanks for your time.. i will check tomorrow .. when you are free, can you look at my code in more details why drawText didn't work??

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #25

                    @abdullahzubair109
                    well you call both
                    painter->drawText( pt, qs); and
                    QStyledItemDelegate::paint
                    so maybe it just overrode your text.

                    A 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @abdullahzubair109
                      well you call both
                      painter->drawText( pt, qs); and
                      QStyledItemDelegate::paint
                      so maybe it just overrode your text.

                      A Offline
                      A Offline
                      abdullahzubair109
                      wrote on last edited by
                      #26

                      @mrjj hmm i get it

                      mrjjM 1 Reply Last reply
                      0
                      • A abdullahzubair109

                        @mrjj hmm i get it

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #27

                        @abdullahzubair109
                        hi
                        using else it does move text for me. ( to the center )

                        
                                if (index.column() == 0) {
                        
                                    QString qs = index.data().toString();
                                    QPoint pt = option.rect.center();
                                    pt += QPoint(0, 0);
                                    painter->drawText( pt, qs);
                                } else
                                    QStyledItemDelegate::paint(painter, option, index);
                        
                        

                        but it kills the selection drawing.

                        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