Method QListWidget::insertItem for QListWidget which is painting with ItemDelegate class [SOLVED]
-
@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. -
Hi and welcome to devnet,
Do you have any sorting active on your QListWidget ?
-
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?
-
@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.
-
Can you share your custom class ? There might be something to spot in it.
-
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 ± } 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]); }
-
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.