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.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.
  • L Offline
    L Offline
    luke147
    wrote on last edited by luke147
    #1

    Hello.
    I need advice how I can repaint a list with type QListWidget.
    I created ItemDelegate class which inherits from QAbstractItemDelegate class. The ItemDelegate class instance is set as itemDelegete for the list.

    header file code with my ItemDelegate class:

    class ItemDelegate : public QAbstractItemDelegate
    {
    public:
        ItemDelegate(QListWidget *l) :QAbstractItemDelegate(l) {}
        ~ItemDelegate() {}
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
        QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
    private:
        ...
    };
    

    Commands in class constructor for creating main window:

    ItemDelegate *itemDelegate = new ItemDelegate(ui->eventlist);
    ui->eventlist->setItemDelegate(itemDelegate);
    

    Command which executed after left mouse click on a button:

    ...
    ui->eventlist->insertItem(itemIndex, item);
    

    This command adds item to end of the list even when itemIndex is less than index of last item.

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

      Hi and welcome

      Im not sure what your issue is.

      After you insert a new items, it is not shown in the list ?

      You could do
      ui->eventlist->viewport()->update() to make it redraw it all

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

        Hi. Thanks.
        I tried your advice but the problem isn't solved.

        Command which executed after left mouse click on a button:

        ui->eventlist->insertItem(itemIndex, item);
        

        This command adds item to end of the list even when itemIndex variable is less than index of last item.
        What do I could do to new item was added to right place.

        ui->eventlist is the list with QListWidget type.

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

          @luke147 said:

          What do I could do to new item was added to right place.

          Hi Im afraid I do not understand what is not working for you ?

          Could you describe what you want or what you expect to happen and what happens instead.

          // add new entry above the selected
              int indx=ui->listWidget->currentIndex().row();
              ui->listWidget->insertItem( indx, "im new" );
          
              // add new to the end of list
              ui->listWidget->addItem( "last" );
          
          1 Reply Last reply
          0
          • L Offline
            L Offline
            luke147
            wrote on last edited by luke147
            #5

            I defined method ItemDelegate::paint which paints a list item.
            Inserted item is added always to end of the list.
            See my screen-shots:
            illustration with indexes: http://www.imagebam.com/image/ebda1b425977807
            current status: http://www.imagebam.com/image/123d85425977806

            mrjjM 1 Reply Last reply
            0
            • L luke147

              I defined method ItemDelegate::paint which paints a list item.
              Inserted item is added always to end of the list.
              See my screen-shots:
              illustration with indexes: http://www.imagebam.com/image/ebda1b425977807
              current status: http://www.imagebam.com/image/123d85425977806

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

              @luke147

              Ok, so the issue is that the added item is put last in the list
              and you want it to be after the first in the list ?

              so
              insertItem( 1 , "im new" );

              will not put it in 2 place ?

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

                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 1 Reply Last reply
                0
                • 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