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. Custom Delegate

Custom Delegate

Scheduled Pinned Locked Moved Solved General and Desktop
qstyleditemdelemusic
12 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi
    You could add \n (newline) between text
    and it will be drawn as one item like:
    My Song Title
    Artist

    QString Row=QString("%1\n%2").arg("My Song Title").arg("Artist");
    ui->listWidget->addItem(Row);
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      shahriar25
      wrote on last edited by
      #3

      Hi,
      thank you. I did it. but still is there a simple example about custom delegates?

      And also is it possible to sort a QListWidget with item's userRole Data ( listWidgetItem->Data(Qt::UserRole) ) ?

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #4

        Here's a little example of a delegate that takes title and artist from user roles of the index:

        struct MyDelegate: public QStyledItemDelegate
        {
            MyDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}
        
            void paint(QPainter* p, const QStyleOptionViewItem& o, const QModelIndex& idx) const override
            {
                // get the data from the index
                auto title  = idx.data(Qt::UserRole).toString();
                auto artist = idx.data(Qt::UserRole+1).toString();
        
                //split cell rectangle into upper and lower half
                auto titleRect  = o.rect.adjusted(0, 0, 0, -o.rect.height()/2);
                auto artistRect = o.rect.adjusted(0, o.rect.height()/2, 0, 0);
        
                //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
                auto sh = QStyledItemDelegate::sizeHint(o, idx);
                //modify the hint to fit 2 lines of text
                return QSize(sh.width(), sh.height()*2);
            }
        };
        

        You can play around with the painter, fonts, alignment etc. but that's the basic idea.

        AFAIK you can't select a role for sorting in a QListWidget, but you could use a QListView with a QStandardItemModel instead. Should be pretty similar to use and that model allows you to set a role used for sorting.

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

          Hi,
          Thank you for the example it helped so much.
          and for the listView:
          I'm using QListWdgetItem for the albums but there is no addItem or insertItem in QListView.
          What should I do about that?

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #6

            When using a QListView you need to provide a model via setModel(). I suggest a QStandardItemModel. In that setup you add items to the model, not to the view, i.e. use setItem() to insert a QStandardItem.

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

              Hi,
              setModel in QListView takes an QAbstractItemModel not a QStandardModel. and I looked at the QAbstractItemModel and I got confused. Where are it's items?

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #8

                QAbstractItemModel is an abstract base class for models. It does not have items because not all models need them (e.g. QSqlTableModel).

                QStandardModel is one of the implementations of QAbstractItemModel that Qt provides. You can pass it to the setModel() method. It uses QStandardItem as the item type.

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

                  Yes you're right I made a mistake.
                  thank you. you helped so much

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

                    Hi,
                    I did as you said but it doesn't sort

                    QStandardItemModel *model = new QStandardItemModel;
                    QStandardItem *item = new QStandardItem("Daze");
                    QStandardItem *item2 = new QStandardItem("Jealous Gods");
                    model->setItem(0,item2);
                    model->setItem(1, item);
                    model->setSortRole(0);
                    ui->listView->setModel(model);

                    it shows:
                    Jealous Gods
                    daze

                    1 Reply Last reply
                    0
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by Chris Kawa
                      #11

                      You need to call sort() to sort the model. Also you should use enums and not raw role numbers in case the value changes in the future:

                      QStandardItemModel *model = new QStandardItemModel(someParent); //remember to give it a parent or it leaks!
                      model->setItem(0, new QStandardItem("Jealous Gods"));
                      model->setItem(1, new QStandardItem("Daze"));
                      model->setSortRole(Qt::DisplayRole); // Qt::DisplayRole is default anyway, so this changes nothing
                      model->sort(0);
                      ui->listView->setModel(model);
                      
                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        shahriar25
                        wrote on last edited by
                        #12

                        Yes thank you so much

                        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