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 selected row height in QListView

How to change selected row height in QListView

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 3.1k Views
  • 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.
  • Binary SoftB Offline
    Binary SoftB Offline
    Binary Soft
    wrote on last edited by VRonin
    #1

    Hello,
    I created a custom QStyledItemDelegate for QListView. When I click (select) a row of listview, I want to set row height to (eg. double etc. ).

    When I debug it. sizeHint of opt.state & QStyle::State_Selected is never reached. How Can I solve it?
    Thanks

    class ListViewDelegate : public QStyledItemDelegate
    {
    public:
        void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    		QStyleOptionViewItemV4 opt = option;
    		initStyleOption(&opt, index);
    
    		QString str1 = index.model()->data(index.model()->index(index.row(), 1)).toString();
    
    		if (opt.state & QStyle::State_Selected) {
    		   painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
    		}
    	 
    		painter->setPen(Qt::black);
    		painter->drawText(opt.rect,opt.displayAlignment, str1);
        
    	}
    
    	QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index ) const {
    		QSize result = QStyledItemDelegate::sizeHint(option, index);
    		
    QStyleOptionViewItemV4 opt = option;
    		initStyleOption(&opt, index);
    
    		if (opt.state & QStyle::State_Selected) 
    			result.setHeight(result.height()*3);
    		else
    			result.setHeight(result.height()*1);
    			
    		return result;
    	}
    };
    
    
    JonBJ qwasder85Q 2 Replies Last reply
    0
    • Binary SoftB Binary Soft

      Hello,
      I created a custom QStyledItemDelegate for QListView. When I click (select) a row of listview, I want to set row height to (eg. double etc. ).

      When I debug it. sizeHint of opt.state & QStyle::State_Selected is never reached. How Can I solve it?
      Thanks

      class ListViewDelegate : public QStyledItemDelegate
      {
      public:
          void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
      		QStyleOptionViewItemV4 opt = option;
      		initStyleOption(&opt, index);
      
      		QString str1 = index.model()->data(index.model()->index(index.row(), 1)).toString();
      
      		if (opt.state & QStyle::State_Selected) {
      		   painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
      		}
      	 
      		painter->setPen(Qt::black);
      		painter->drawText(opt.rect,opt.displayAlignment, str1);
          
      	}
      
      	QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index ) const {
      		QSize result = QStyledItemDelegate::sizeHint(option, index);
      		
      QStyleOptionViewItemV4 opt = option;
      		initStyleOption(&opt, index);
      
      		if (opt.state & QStyle::State_Selected) 
      			result.setHeight(result.height()*3);
      		else
      			result.setHeight(result.height()*1);
      			
      		return result;
      	}
      };
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Binary-Soft
      Excuse me if this is wrong/obvious, I don't do C++ regularly. But have you (presumably in your .h file?) ensured you have marked your QSize sizeHint() declaration with the override keyword, else it will be a distinct method with the same name but not actually overriding?

      Binary SoftB 1 Reply Last reply
      1
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        Can you confirm whether sizeHint method is called ? This will be first step. Your function signature looks right.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        Binary SoftB 1 Reply Last reply
        0
        • JonBJ JonB

          @Binary-Soft
          Excuse me if this is wrong/obvious, I don't do C++ regularly. But have you (presumably in your .h file?) ensured you have marked your QSize sizeHint() declaration with the override keyword, else it will be a distinct method with the same name but not actually overriding?

          Binary SoftB Offline
          Binary SoftB Offline
          Binary Soft
          wrote on last edited by
          #4

          @JonB said in How to change selected row height in QListView:

          keyword
          Now I change some code. row height is changed when selected. But selected row is overlapped to next row. I think layout is not adjust for selected row.

          Thanks.

          .h

          #ifndef ListViewDelegate _H
          #define listviewdelegate _H
          
          #include <QStyledItemDelegate>
          #include <QWidget>
          #include <QPainter>
          
          #include <QDebug>
          #include <QMouseEvent>
           
          
          class ListViewDelegate  : public QStyledItemDelegate
          {
          
              Q_OBJECT
          public:
              ListViewDelegate (QWidget *parent = 0): QStyledItemDelegate(parent){
                 intSelID=-1;
              }
          
              void paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const Q_DECL_OVERRIDE;
              QSize sizeHint(const QStyleOptionViewItem &option,
                             const QModelIndex &index) const Q_DECL_OVERRIDE;
          
              bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
          
          private:
              int intSelID;
          
          
          
          };
          
          #endif // ListViewDelegate _H
          
          
          #include "listviewdelegate.h"
          
          void ListViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
          {
              if(!index.isValid()) return;
          
              QStyleOptionViewItemV4 opt = option;
          
              QString str1 = index.model()->data(index.model()->index(index.row(), 1)).toString();
          
              QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
              style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
          
              QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
              if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
                  cg = QPalette::Inactive;
          
              painter->setPen(Qt::black);
          
              painter->drawText(opt.rect, opt.displayAlignment,  str1);
              painter->setPen(Qt::yellow);
          
          }
          
          QSize ListViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
          {
              if(!index.isValid())  return QSize();
              QSize result = QStyledItemDelegate::sizeHint(option, index);
          
              if (intSelID == index.row()) {
                  result.setHeight(result.height()*2);
                  qDebug() << " select row";
              }
              else {
                  result.setHeight(result.height()*1);
                  qDebug() << " de select";
              }
              return result;
          }
          
          bool ListViewDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
          {
              QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
              if (event->type() == QEvent::MouseButtonPress)
              {
                  if(intSelID==index.row())
                      intSelID= -1;
                  else
                      intSelID = index.row();
          
                  ListViewDelegate::sizeHint(option,index);
              }
              return QStyledItemDelegate::editorEvent(event, model, option, index);
          }
          
          
          
          1 Reply Last reply
          0
          • dheerendraD dheerendra

            Can you confirm whether sizeHint method is called ? This will be first step. Your function signature looks right.

            Binary SoftB Offline
            Binary SoftB Offline
            Binary Soft
            wrote on last edited by
            #5

            @dheerendra
            Thanks.
            I use debug, sizeHind is work. Now row height is change but layout's height is not change.

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              which layout is not changing ?

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              Binary SoftB 1 Reply Last reply
              1
              • dheerendraD dheerendra

                which layout is not changing ?

                Binary SoftB Offline
                Binary SoftB Offline
                Binary Soft
                wrote on last edited by
                #7

                @dheerendra !

                Please see two pictures (before select and after select)
                I change selected row Item's height *1.8. Item's height is changed but it overlapped on next item.

                [alt text](1_1543984748851_2.png 0_1543984748850_1.jpg image url)

                Binary SoftB 1 Reply Last reply
                0
                • Binary SoftB Binary Soft

                  @dheerendra !

                  Please see two pictures (before select and after select)
                  I change selected row Item's height *1.8. Item's height is changed but it overlapped on next item.

                  [alt text](1_1543984748851_2.png 0_1543984748850_1.jpg image url)

                  Binary SoftB Offline
                  Binary SoftB Offline
                  Binary Soft
                  wrote on last edited by
                  #8

                  @Binary-Soft
                  I already set listview's setUniformItemSizes to false.
                  ui->listView->setUniformItemSizes(false);

                  1 Reply Last reply
                  0
                  • Binary SoftB Binary Soft

                    Hello,
                    I created a custom QStyledItemDelegate for QListView. When I click (select) a row of listview, I want to set row height to (eg. double etc. ).

                    When I debug it. sizeHint of opt.state & QStyle::State_Selected is never reached. How Can I solve it?
                    Thanks

                    class ListViewDelegate : public QStyledItemDelegate
                    {
                    public:
                        void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                    		QStyleOptionViewItemV4 opt = option;
                    		initStyleOption(&opt, index);
                    
                    		QString str1 = index.model()->data(index.model()->index(index.row(), 1)).toString();
                    
                    		if (opt.state & QStyle::State_Selected) {
                    		   painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
                    		}
                    	 
                    		painter->setPen(Qt::black);
                    		painter->drawText(opt.rect,opt.displayAlignment, str1);
                        
                    	}
                    
                    	QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index ) const {
                    		QSize result = QStyledItemDelegate::sizeHint(option, index);
                    		
                    QStyleOptionViewItemV4 opt = option;
                    		initStyleOption(&opt, index);
                    
                    		if (opt.state & QStyle::State_Selected) 
                    			result.setHeight(result.height()*3);
                    		else
                    			result.setHeight(result.height()*1);
                    			
                    		return result;
                    	}
                    };
                    
                    
                    qwasder85Q Offline
                    qwasder85Q Offline
                    qwasder85
                    wrote on last edited by
                    #9

                    @Binary-Soft

                    YourListView->setStyleSheet(QString("::item:selected { height: %1; }").arg(new_height));
                    

                    ...
                    I know, the topic is old. But just to keep future devs from spending too much time on something like this.

                    1 Reply Last reply
                    1

                    • Login

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