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. QListWidget::setItemWidget The Selection area behind the label image

QListWidget::setItemWidget The Selection area behind the label image

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 3.9k Views 3 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.
  • joeQJ Offline
    joeQJ Offline
    joeQ
    wrote on last edited by joeQ
    #1
    1. i use the QListWidget to preview some images files. I did not use the setIcon. I did inherit QWidget, QWidget include two QLabel to load imags and to show some text.
    2. i use the following code to add the item;
        pItemWidget = new ListItemWidget(this); // My Widget
        pItemWidget->SetPixmap( pixmap ); // My customize function: Qlabel load pixmap
        pItemWidget->SetImgPath( qsFilePath );// My customize function: show some text
    
        pItem = new QListWidgetItem(this);
        pItem->setSizeHint( pItemWidget->size() );
        pItem->setFlags( Qt::ItemIsEnabled |
                         Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
    
        setItemWidget(pItem, pItemWidget);
    
    1. when i use the mouse to choose the list widget item, i find the selection area behind the label image. i do not know how to do ? help me...

    2. My Problem image The Images about my problem.

    3. My Code. My Zip code, You can click and download My code Demo

    Just do it!

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @joeQ Try setSelectionRectVisible(false)

      157

      joeQJ 1 Reply Last reply
      0
      • p3c0P p3c0

        @joeQ Try setSelectionRectVisible(false)

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #3

        @p3c0 i want to have the selection area. but the selection area behind the label image. i want the selection area before the label image. Thank u.

        Just do it!

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @joeQ The selection rect is already at foreground. There might be some other problem in your code.
          Here a simple test I did:

          for(int i=0; i<10; i++) {
              QListWidgetItem *item = new QListWidgetItem;
              item->setText("Item");
              item->setIcon(QIcon("image.png"));
              ui->listWidget->insertItem(row, item);
          }
          

          157

          joeQJ 1 Reply Last reply
          0
          • p3c0P p3c0

            @joeQ The selection rect is already at foreground. There might be some other problem in your code.
            Here a simple test I did:

            for(int i=0; i<10; i++) {
                QListWidgetItem *item = new QListWidgetItem;
                item->setText("Item");
                item->setIcon(QIcon("image.png"));
                ui->listWidget->insertItem(row, item);
            }
            
            joeQJ Offline
            joeQJ Offline
            joeQ
            wrote on last edited by
            #5

            @p3c0 Yes, i used the other way is ok. but, i used the setItemWidget to QListWidget, it is wrong. i don't know why. you can download my demo. step 4. Please help me. Thank u.

            Just do it!

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @joeQ Unfortunately I too am not aware of this kind of behaviour. I have reduced the problem to the following for anyone else to test:

                  listWidget->setViewMode(QListView::IconMode);
                  listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
              
                  for(int i=0; i<10; i++) {
                      QPushButton *label = new QPushButton;
                      label->resize(50,50);
                      label->setText("Button"+QString::number(i));
              
                      QListWidgetItem *item = new QListWidgetItem;
                      item->setSizeHint(label->size());
                      listWidget->insertItem(i, item);
              
                      listWidget->setItemWidget(item, label);
                  }
              

              For eg. following works:

              Use QLabel instead and set only the text. But if we set pixmap for label then its the same behavior.
              Is there any other setting ? Or only subclassing might solve this problem ?

              157

              joeQJ 1 Reply Last reply
              0
              • p3c0P p3c0

                @joeQ Unfortunately I too am not aware of this kind of behaviour. I have reduced the problem to the following for anyone else to test:

                    listWidget->setViewMode(QListView::IconMode);
                    listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
                
                    for(int i=0; i<10; i++) {
                        QPushButton *label = new QPushButton;
                        label->resize(50,50);
                        label->setText("Button"+QString::number(i));
                
                        QListWidgetItem *item = new QListWidgetItem;
                        item->setSizeHint(label->size());
                        listWidget->insertItem(i, item);
                
                        listWidget->setItemWidget(item, label);
                    }
                

                For eg. following works:

                Use QLabel instead and set only the text. But if we set pixmap for label then its the same behavior.
                Is there any other setting ? Or only subclassing might solve this problem ?

                joeQJ Offline
                joeQJ Offline
                joeQ
                wrote on last edited by
                #7

                @p3c0 Thank u very much, I do not give it up. when i solved it , i will share my way for u.

                Just do it!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  It's because you are setting a widget on a cell, the widget is "above" the cell.

                  If you only want to show an image then you should use a custom QStyledItemDelegate.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  joeQJ 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Hi,

                    It's because you are setting a widget on a cell, the widget is "above" the cell.

                    If you only want to show an image then you should use a custom QStyledItemDelegate.

                    joeQJ Offline
                    joeQJ Offline
                    joeQ
                    wrote on last edited by
                    #9

                    @SGaist Hi, Thank u. and if I don't want to use a custom QStyledItemDelegate. What should I do ?

                    My Widget has image label and text label, The important thing is that some of my structure data is stored in the Widget class members, eg: structure pointer.

                    I also used try setItemData function, but Data type mismatch. so, i subclass the QWidget.

                    Just do it!

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      From the setItemWidget documentation: it's to display static content. It's not meant to handle D&D or editing.

                      Why do you need to embed custom data in that widget ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      joeQJ 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        From the setItemWidget documentation: it's to display static content. It's not meant to handle D&D or editing.

                        Why do you need to embed custom data in that widget ?

                        joeQJ Offline
                        joeQJ Offline
                        joeQ
                        wrote on last edited by
                        #11

                        @SGaist I think i was wrong, i chosed the wrong solution. Thank u, and , i found some demo about delegate, i know , i should to use the QStyledItemDelegate class. Thank u very much. and i am new bird in Qt. so ...

                        Just do it!

                        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