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. QStyledItemDelegate & QRect

QStyledItemDelegate & QRect

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 3.4k 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.
  • S Offline
    S Offline
    shahriar25
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shahriar25
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          shahriar25
          wrote on last edited by shahriar25
          #4

          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());
          }
          

          };

          raven-worxR 1 Reply Last reply
          0
          • S shahriar25

            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());
            }
            

            };

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @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
                ....
            }
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • S Offline
              S Offline
              shahriar25
              wrote on last edited by
              #6

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

              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