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. Method QListWidget::insertItem for QListWidget which is painting with ItemDelegate class [SOLVED]

Method QListWidget::insertItem for QListWidget which is painting with ItemDelegate class [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
qlistwidgetqlistwidgetinseqabstractitemde
17 Posts 3 Posters 6.2k 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.
  • L luke147

    I need to put the item by the specified index.
    The new item is inserted always only at the end.
    First, second etc. or last but always by the specified index.
    I use:

    insertItem( index , item );
    

    But item is put always at the end.

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

    @mrjj said:

    and how do you get the index ?
    you use for
    insertItem( index , item );

    Also just for test, tell me if
    insertItem( 0 , "im new" );
    also be inserted last ?

    Just tested here and insertitem does what expected. As far as I know, having
    a Delegate should not change that.

    L 1 Reply Last reply
    0
    • mrjjM mrjj

      @mrjj said:

      and how do you get the index ?
      you use for
      insertItem( index , item );

      Also just for test, tell me if
      insertItem( 0 , "im new" );
      also be inserted last ?

      Just tested here and insertitem does what expected. As far as I know, having
      a Delegate should not change that.

      L Offline
      L Offline
      luke147
      wrote on last edited by
      #9

      Yes, I tried command:

      insertItem( 0, item );
      

      But the item is inserted at the end.

      mrjjM 1 Reply Last reply
      0
      • L luke147

        Yes, I tried command:

        insertItem( 0, item );
        

        But the item is inserted at the end.

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

        @luke147
        Ok that sounds really strange.

        Need to test some code to have suggestions. My Delegate is inherited from
        QStyledItemDelegate but should makes no difference.

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

          Hi and welcome to devnet,

          Do you have any sorting active on your QListWidget ?

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

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luke147
            wrote on last edited by
            #12

            No, I have no sorting active.
            My item is my type. EventListItem class inherits from QListWidgetItem class. The list is QListWidget type.

            Now I tried create new QListWidget and to insert item with QListWidgetItem type.
            This work fine. Even if I set itemDelegate. It's OK.

            Can a item to be my type which inherits from QListWidgetItem class?

            mrjjM 1 Reply Last reply
            0
            • L luke147

              No, I have no sorting active.
              My item is my type. EventListItem class inherits from QListWidgetItem class. The list is QListWidget type.

              Now I tried create new QListWidget and to insert item with QListWidgetItem type.
              This work fine. Even if I set itemDelegate. It's OK.

              Can a item to be my type which inherits from QListWidgetItem class?

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

              @luke147
              I see no reason that you cannot make your own QListWidgetItem and
              since your list is not sorted, it should make no difference.
              (but seems it does)

              On thing you could try is to override
              virtual bool operator<(const QListWidgetItem &other) const;

              for you class and see if that changes where it gets inserted.

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

                Can you share your custom class ? There might be something to spot in it.

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

                1 Reply Last reply
                0
                • mrjjM mrjj

                  @luke147
                  I see no reason that you cannot make your own QListWidgetItem and
                  since your list is not sorted, it should make no difference.
                  (but seems it does)

                  On thing you could try is to override
                  virtual bool operator<(const QListWidgetItem &other) const;

                  for you class and see if that changes where it gets inserted.

                  L Offline
                  L Offline
                  luke147
                  wrote on last edited by luke147
                  #15

                  Sorry. My answers will be delayed this week.

                  @mrjj
                  I try override the operator.

                  @SGaist
                  the header file:

                  #include <QPixmap>
                  #include <QListWidget>
                  #include <QListWidgetItem>
                  #include "pts.h"
                  
                  class EventListItem : public QListWidgetItem
                  {
                  public:
                      enum eventtype { none, start, stop, chapter, bookmark };
                  
                  public:
                      EventListItem( QListWidget *listbox, const QPixmap &pixmap, eventtype type, int picture, int picturetype, pts_t _pts );
                      ~EventListItem();
                  
                      const QPixmap *pixmap() const
                      {
                          return &pm;
                      }
                  
                      int getpicture() const
                      {
                          return pic;
                      }
                  
                      pts_t getpts() const
                      {
                          return pts;
                      }
                  
                      enum eventtype geteventtype() const
                      {
                          return evtype;
                      }
                  
                      void seteventtype(enum eventtype type)
                      {
                          evtype=type;
                          return;
                      }
                  
                      int  height( const QListWidget *lb ) const;
                      int  width( const QListWidget *lb )  const;
                  
                      int rtti() const;
                      static int RTTI()
                      {
                          return 294268923;
                      }
                  
                      QString getstring() const;
                  
                  private:
                      QPixmap pm;
                      enum eventtype evtype;
                      int pic;
                      int pictype;
                      pts_t pts;
                  };
                  

                  the source file:

                  #include <QApplication>
                  #include <QIcon>
                  #include "eventlistitem.h"
                  #include "settings.h"
                  
                  EventListItem::EventListItem(QListWidget *listbox, const QPixmap &pixmap,
                                               eventtype type, int picture, int picturetype, pts_t _pts ) :
                      QListWidgetItem(listbox), pm(pixmap), evtype(type), pic(picture), pictype(picturetype), pts(_pts)
                  {
                  
                      if (pm.width()>160 || pm.height()>90)
                          pm=pm.scaled(130, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation);
                  
                      int h = height(listbox);
                      setSizeHint(QSize(sizeHint().width(), h));
                      setIcon(QIcon(pm));
                      setText(getstring());
                  }
                  
                  EventListItem::~EventListItem()
                  {}
                  
                  int EventListItem::rtti() const
                  {
                      return RTTI();
                  }
                  
                  int EventListItem::height(const QListWidget *  ) const
                  {
                      int h=0;
                  
                      if (!pm.isNull())
                          h = pm.height();
                  
                  
                      return qMax( h+6, QApplication::globalStrut().height() );
                  }
                  
                  int EventListItem::width(const QListWidget *lb ) const
                  {
                      int width=3;
                  
                      if (!pm.isNull())
                          width += pm.width()+3;
                  
                      /*
                      // I will move this code other place and edit it. It specify exact width of text.
                      if (lb) {
                          QLabel rt(getstring());
                          rt.setFont(lb->font())
                          rt.setFixedWidth(1000); //drawinglistbox->width());
                          width+=rt.width() + 3;
                      }
                      */
                  
                      return qMax( width, QApplication::globalStrut().width() );
                  }
                  
                  QString EventListItem::getstring() const
                  {
                      QString label;
                      if (evtype==start)
                          label = settings()->start_label;
                      else if (evtype==stop)
                          label = settings()->stop_label;
                      else if (evtype==chapter)
                          label = settings()->chapter_label;
                      else if (evtype==bookmark)
                          label = settings()->bookmark_label;
                  
                      return label + QString().sprintf("<br><font color=\"%%normal-text-colour%%\">%02d:%02d:%02d.%03d</font><br><font color=\"%%normal-text-colour%%\">%d (%c)</font>",
                                                       int(pts/(3600*90000)),
                                                       int(pts/(60*90000))%60,
                                                       int(pts/90000)%60,
                                                       int(pts/90)%1000,
                                                       pic,
                                                       ((const char *)".IPB....")[pictype&7]);
                  }
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by SGaist
                    #16

                    Ok, I see the problem: as recommended in the QListWidgetItem constructor doc, don't give it a parent if you want to insert your item at a given position.

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

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      luke147
                      wrote on last edited by
                      #17

                      Thanks to both.
                      @SGaist
                      I don't give a parent to QListWidgetItem constructor if I use insertItem method.

                      @mrjj
                      I overridden operator<.

                      It works now.

                      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