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. QListWiget item scroll off behaviour
Forum Updated to NodeBB v4.3 + New Features

QListWiget item scroll off behaviour

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 377 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.
  • F Offline
    F Offline
    Flock Andrew
    wrote on last edited by Flock Andrew
    #1

    I have a QListWidget with multiple items with QLables with images set as the itemWidgets when the list is in scrollperpixel mode the items when they are scrolled off the top of the list do not leave the visible area as expected when the code is run on iOS while they do leave the visible area as expected on macOS. Is there some setting I need to set to change the behaviour for items leaving the screen?

    list = new QListWidget(this);
    list->setGeometry(50,50,300,500);
    list->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    for(int i=0;i<15;i++){
    	QListWidgetItem *temp = new QListWidgetItem();
    	QLabel *widget = new QLabel(this);
    	widget->setGeometry(0,0,250,100);
    	widget->setPixmap(QPixmap(":/waveofthefuture.jpg")); //random jpg to make the issue evident
    	 temp->setSizeHint(QSize(250,100));
    	list->addItem(temp);
    	list->setItemWidget(temp,widget);
    }
    

    alt text
    (using Qt version 5.12.11 but same issue is present of 6.2.0)

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Super with video.
      I have not seen any settings like that.

      It seems the QLabel gets compressed. (maybe)
      You could try set Fixed-size matching the image and see if that alters it.

      That said, a Delegate would be much more efficient than using QLabels.

      Update:
      Tested your code on Win 10, Qt 5.15.2 and it didn't do like that. (Looked like Ios)

      Update 2:

      Could you try a delegate ?

      Also if you test please do (also)
      // ui->listWidget->setItemDelegate(new ImageDelegate);
      and see how close the result are just using the DecorationRole :)

      #include <QStyledItemDelegate>
      #include <qpainter.h>
      
      class ImageDelegate : public QStyledItemDelegate
      {
      public:
          ImageDelegate(QObject *poParent = nullptr) :
              QStyledItemDelegate(poParent)  {}
      public:
          virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
                             const QModelIndex &index) const override
          {
              QPixmap img = index.data(Qt::DecorationRole).value<QPixmap>();
              if ( ! img.isNull()) {
                  painter->drawPixmap(option.rect, img);
              }
          }
      };
      
      

      using it:

           auto list = ui->listWidget;
          list->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
          for (int i = 0; i < 15; i++) {
              QListWidgetItem *item = new QListWidgetItem();
              item->setData(Qt::DecorationRole, QPixmap(":/test.png"));
              list->addItem(item);
          }
          ui->listWidget->setItemDelegate(new ImageDelegate);
      
      F 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        Super with video.
        I have not seen any settings like that.

        It seems the QLabel gets compressed. (maybe)
        You could try set Fixed-size matching the image and see if that alters it.

        That said, a Delegate would be much more efficient than using QLabels.

        Update:
        Tested your code on Win 10, Qt 5.15.2 and it didn't do like that. (Looked like Ios)

        Update 2:

        Could you try a delegate ?

        Also if you test please do (also)
        // ui->listWidget->setItemDelegate(new ImageDelegate);
        and see how close the result are just using the DecorationRole :)

        #include <QStyledItemDelegate>
        #include <qpainter.h>
        
        class ImageDelegate : public QStyledItemDelegate
        {
        public:
            ImageDelegate(QObject *poParent = nullptr) :
                QStyledItemDelegate(poParent)  {}
        public:
            virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
                               const QModelIndex &index) const override
            {
                QPixmap img = index.data(Qt::DecorationRole).value<QPixmap>();
                if ( ! img.isNull()) {
                    painter->drawPixmap(option.rect, img);
                }
            }
        };
        
        

        using it:

             auto list = ui->listWidget;
            list->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
            for (int i = 0; i < 15; i++) {
                QListWidgetItem *item = new QListWidgetItem();
                item->setData(Qt::DecorationRole, QPixmap(":/test.png"));
                list->addItem(item);
            }
            ui->listWidget->setItemDelegate(new ImageDelegate);
        
        F Offline
        F Offline
        Flock Andrew
        wrote on last edited by
        #3

        @mrjj Thank you the setData and using the DecorationRole was sufficient to solve the problem on initial testing with the Delegate also working but making it so I didn't need to set the correct size of my pixmap. Never knew that the data on a list widget item could be a pixmap and have it render.

        1 Reply Last reply
        1
        • F Offline
          F Offline
          Flock Andrew
          wrote on last edited by
          #4

          Note for if somebody else runs into this problem in the future and finds this thread you may need "Qt::BackgroundRole" instead of Qt::DecorationRole if you still need to have sub widgets to the item to have other behaviour.

          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