QStyledItemDelegate & QRect



  • Hi,

    I'm wirking on a music player and I have asked here before for a custom delegate for a QListView with two text displays:

    #ifndef ALBUMDELEGATE
    #define ALBUMDELEGATE
    #include <QMainWindow>
    #include <QRect>
    #include <QStyledItemDelegate>
    #include <QPainter>

    struct AlbumDelegate: public QStyledItemDelegate
    {
    AlbumDelegate(QObject* parent = NULL) : QStyledItemDelegate(parent) {}

    void paint(QPainter* p, const QStyleOptionViewItem& o, const QModelIndex& idx) const override
    {
        // get the data from the index
        QString title  = idx.data(Qt::UserRole+2).toString();
        QString artist = idx.data(Qt::UserRole+3).toString();
    
        QRect titleRect  = o.rect.adjusted(0, o.rect.height()-40, o.rect.width(), o.rect.height()-20);
        QRect artistRect = o.rect.adjusted(0, o.rect.height()-20, o.rect.width(), o.rect.height());
    
        //draw the data in their rectangles
        p->drawText(titleRect, Qt::AlignLeft, title);
        p->drawText(artistRect, Qt::AlignLeft, artist);
    }
    
    QSize sizeHint(const QStyleOptionViewItem& o, const QModelIndex& idx) const override
    {
        //get the default size hint
        QSize sh = QStyledItemDelegate::sizeHint(o, idx);
        //modify the hint to fit 2 lines of text
        return QSize(sh.width(), sh.height()*2);
    }
    

    };

    #endif // ALBUMDELEGATE

    this is the answer I got (from chris kawa). but when I apply this delegate to my listVew the Icon of the item disappears. what can I do to fix it?


  • Moderators

    get the icon with idx.data(Qt::DecorationRole) and paint it on the appropriate place.



  • Hi, I tried to do that but I don't know much about QRecr an QPainter

    #ifndef ALBUMDELEGATE
    #define ALBUMDELEGATE

    #include <QMainWindow>
    #include <QRect>
    #include <QStyledItemDelegate>
    #include <QPainter>

    struct AlbumDelegate: public QStyledItemDelegate
    {
    AlbumDelegate(QObject* parent = NULL) : QStyledItemDelegate(parent) {}

    void paint(QPainter* p, const QStyleOptionViewItem& o, const QModelIndex& idx) const override
    {
        // get the data from the index
        QString title  = idx.data(Qt::UserRole+2).toString();
        QString artist = idx.data(Qt::UserRole+3).toString();
        QIcon icon = idx.data(Qt::DecorationRole).value<QIcon>();
    
        //split cell rectangle into upper and lower half
        //QRect titleRect  = o.rect.adjusted(0, 0, 0, -o.rect.height()/2);
        //QRect artistRect = o.rect.adjusted(0, o.rect.height()/2, 0, 0);
    
        QRect iconRect = o.rect.adjusted(0, 0, o.rect.width(), o.rect.height()-40);
        QRect titleRect  = o.rect.adjusted(0, o.rect.height()-40, o.rect.width(), o.rect.height()-20);
        QRect artistRect = o.rect.adjusted(0, o.rect.height()-20, o.rect.width(), o.rect.height());
    
        //draw the data in their rectangles
        p->drawPixmap(iconRect, icon.pixmap(o.rect.width(), o.rect.width()),o.rect);
        p->drawText(titleRect, Qt::AlignLeft, title);
        p->drawText(artistRect, Qt::AlignLeft, artist);
    }
    
    QSize sizeHint(const QStyleOptionViewItem& o, const QModelIndex& idx) const override
    {
        //get the default size hint
        QSize sh = QStyledItemDelegate::sizeHint(o, idx);
        //modify the hint to fit 2 lines of text
        return QSize(sh.width(), sh.height()*2);
    }
    

    };

    #endif // ALBUMDELEGATE

    and also I have set shortcuts to resize icons of items by triggreing them. I want those to work properly.
    What should I do?



  • Hi, I was working on another delegate that doesn't need icon to learn and I saw another problem:
    the QListView with this delegate doesn't show the selected items. when you click on the item the signal slot that I set for it activates but the item doesn't get selected. what should I do about that?

    struct TrackDelegate: public QStyledItemDelegate
    {
    TrackDelegate(QObject* parent = NULL) : QStyledItemDelegate(parent) {}

    void paint(QPainter* p, const QStyleOptionViewItem& o, const QModelIndex& idx) const override
    {
        // get the data from the index
        QString track  = idx.data(Qt::UserRole+2).toString();
        QString artist = idx.data(Qt::UserRole+6).toString();
        QString title = idx.data(Qt::UserRole+3).toString();
    
        QRect trackRect  = o.rect.adjusted(0, 0, 30, o.rect.height());
        QRect titleRect = o.rect.adjusted(40, 0, o.rect.width(), o.rect.height()/2);
        QRect artistRect = o.rect.adjusted(40, o.rect.height()/2, o.rect.width(), o.rect.height());
    
        //draw the data in their rectangles
        p->drawText(trackRect, Qt::AlignLeft, track);
        p->drawText(artistRect, Qt::AlignLeft, artist);
        p->drawText(titleRect, Qt::AlignLeft, title);
    }
    
    QSize sizeHint(const QStyleOptionViewItem& o, const QModelIndex& idx) const override
    {
        //get the default size hint
        QSize sh = QStyledItemDelegate::sizeHint(o, idx);
        //modify the hint to fit 2 lines of text
        return QSize(sh.width(), sh.height());
    }
    

    };


  • Moderators

    @shahriar25 said:

    Hi, I was working on another delegate that doesn't need icon to learn and I saw another problem:
    the QListView with this delegate doesn't show the selected items. when you click on the item the signal slot that I set for it activates but the item doesn't get selected. what should I do about that?

    Let Qt do the actual drawing of all the stuff except the icon and the text:

    void MyItemDelegate::paint(QPainter *painter,
            const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QStyleOptionViewItemV4 opt = option;
        initStyleOption(&opt, index);
        opt.icon = QIcon();
        opt.text = QString();
    
        const QWidget *widget = option.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
    
        // draw icon and text
        ....
    }
    


  • Hi, thank you it worked. but I still have problem with the icon


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.