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
QtWS25 Last Chance

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.
  • S Offline
    S Offline
    shahriar25
    wrote on 5 Jan 2016, 17:13 last edited by
    #1

    Hi,
    II'm working on a music player and I want to build a custom Delegate for my album QListWidget. I just want to add a artist name under the QListWidgetItem's title.
    I have looked at some custom Delegate examples in Qt examples but they are too complicated.
    Can someone tell me the simplest way?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 5 Jan 2016, 21:13 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 6 Jan 2016, 04:36 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
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 6 Jan 2016, 11:30 last edited by Chris Kawa 1 Jun 2016, 11:37
          #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 6 Jan 2016, 18:08 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
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 6 Jan 2016, 18:24 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 7 Jan 2016, 04:30 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
                • C Offline
                  C Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on 7 Jan 2016, 07:04 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 7 Jan 2016, 08:35 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 7 Jan 2016, 10:39 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
                      • C Offline
                        C Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on 7 Jan 2016, 11:11 last edited by Chris Kawa 1 Jul 2016, 11:12
                        #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 7 Jan 2016, 11:39 last edited by
                          #12

                          Yes thank you so much

                          1 Reply Last reply
                          0

                          10/12

                          7 Jan 2016, 10:39

                          • Login

                          • Login or register to search.
                          10 out of 12
                          • First post
                            10/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved