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. Qt: Item Delegate With Different Height And Width
Forum Updated to NodeBB v4.3 + New Features

Qt: Item Delegate With Different Height And Width

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 967 Views 1 Watching
  • 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.
  • R Offline
    R Offline
    Riko
    wrote on 4 Feb 2022, 18:39 last edited by
    #1

    I'm currently working with listview and delegates. For better understanding, please look at the illustration below.

    Illustration

    I'm trying to recreate this by using the paint method of the delegate, but I'm stuck at how to size the rectangles in accordance to the texts. The shorter the text is, the rectangle's width will become smaller and vice versa. The same applies with the text's number of lines.

    The texts are retrieved from MYSQL database. How do I input the data to the DB? I'm using QTextEdit for that and pass the content (textEdit->toPlainText()) to the DB. In short, it's a QString. However, here comes another problem. I don't know how to determine the number of lines the QString has and if the text is split into three lines as in rectangle 3 (the one which says "Hello Oh Yeah! Great!"), how can I determine the longest sentence/phrase as the point of reference for the rectangle width to be calculated later on?

    BTW, I'm still new to Qt's delegates and stuff, so I sincerely ask for the earnest guidance. Thanks.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 4 Feb 2022, 20:10 last edited by
      #2

      Hi,

      The QFontMetrics class comes to mind for that.

      See here for a discussion about its use with multi-line text.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply 5 Feb 2022, 06:28
      0
      • S SGaist
        4 Feb 2022, 20:10

        Hi,

        The QFontMetrics class comes to mind for that.

        See here for a discussion about its use with multi-line text.

        R Offline
        R Offline
        Riko
        wrote on 5 Feb 2022, 06:28 last edited by
        #3

        @SGaist It doesn't help since I don't know where to implement it. I'm using the paint and the sizeHint method. I don't know where should I use the QFontMetrics and how to calculate for the width and height. It'll be helpful if you can give me the code example. Thx.

        R 1 Reply Last reply 5 Feb 2022, 07:29
        0
        • R Riko
          5 Feb 2022, 06:28

          @SGaist It doesn't help since I don't know where to implement it. I'm using the paint and the sizeHint method. I don't know where should I use the QFontMetrics and how to calculate for the width and height. It'll be helpful if you can give me the code example. Thx.

          R Offline
          R Offline
          Riko
          wrote on 5 Feb 2022, 07:29 last edited by Riko 2 May 2022, 15:18
          #4

          @Riko Ok, I did it somehow with the paint method. Here's my code.

          void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
          {
              const QStandardItemModel* model = static_cast<const QStandardItemModel*>(index.model());
              QStandardItem* item = model->item(index.row());
              QString text = item->text();
          
              QRect rect = option.rect;
              rect.center();
          
              QColor gray(Qt::gray);
              QColor white(Qt::white);
          
              painter->setRenderHint(QPainter::Antialiasing);
              painter->setBrush(gray);
              painter->setPen(white);
              painter->setFont(font);
          
              QRect rectNew = painter->boundingRect(rect, int(Qt::AlignLeft), text).marginsAdded(QMargins(10,10,10,10));
              painter->drawRoundedRect(rectNew, 10, 10, Qt::AbsoluteSize);
              QRect rectNew2 = rectNew.marginsRemoved(QMargins(10,10,10,10));
              painter->drawText(rectNew2, int(Qt::AlignLeft), text);} //I don't know why, but to "center" the text to the box drawn, I have to make it like this.
          
          }
          
          QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
          {
              const QStandardItemModel* model = static_cast<const QStandardItemModel*>(index.model());
              QStandardItem* item = model->item(index.row());
              QString text = item->text();
              QRect rect = option.rect;
              rect.center();
              QRect rectNew = option.fontMetrics.boundingRect(rect, 0, text);
              QSize size = rectNew.size();
              return QSize(size); //I think the code here is absurd since I'm just testing things out.
          }
          

          Now, it's all working quite fine except for something. I don't know where it comes from, but I guess it's from the sizeHint method. I don't know how to determine its height, so I just 'yolo-ed' it. Take a look at the screenshot below.

          A picture...

          The longer the lines, the shorter the spacing becomes. Another thing to consider is that whenever texts of different number of lines are inserted, the spacing becomes "broken" or to say that it becomes overlapping. Where does the problem lay do you think? Thx.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 5 Feb 2022, 20:32 last edited by
            #5

            There's not need to go through the model, you already have the index and can retrieve the data from it.

            You can set the spacing QListView::setSpacing.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            1/5

            4 Feb 2022, 18:39

            • Login

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