Is there a way to calculate the height of the QStandardItem?

942 views Asked by At

I have created an expandable ListView which extends from the QListView, everything works well when I just want to show the Header data (Item which is not expanded) because I gave it a hard-coded height which is 64, the details appear when expanding the item. But the problem is I do not know the exact height of the details because the details can one line or more, I want to fit the Item height according to the item content.

Here the code which is handling click listener when the item expanding or collapsing:

LogListItemDelegate *delegate = static_cast<LogListItemDelegate *>(itemDelegate());
QStandardItem *item = static_cast<QStandardItemModel *>(model())->itemFromIndex(index);
bool expand = delegate->isExpandable() && mapFromGlobal(QCursor::pos()).x() >= visualRect(index).width() - 48;
bool expanded = index.data(LogListItemDelegate::DT_Expanded).toBool();

// here the height returned is header height, no containing the details which it is in expanding mode
int height = item->sizeHint().height();

        if (!expanded) {
            item->setData(true, LogListItemDelegate::DT_Expanded);
            item->setSizeHint(QSize(0, 150)); // 150 here must be dynamically calculated
        } else {
            item->setData(false, LogListItemDelegate::DT_Expanded);
            item->setSizeHint(QSize(0, 64)); // 64 is the header height, no prolem
        }

Now the question is: How to calculate the height when item expanded?

Result:

enter image description here

Edit:

It is when I want to add the message to the list
void LogListView::addMessage(const QJsonObject &msg, const bool append)
{
    static int id = 1; // unique id for log items
    auto *item = new QStandardItem();
    item->setEditable(false);
    item->setData(QString("%1").arg(id++, 5, 10, QChar('0')), LogListItemDelegate::DT_Id);
    item->setData(msg["icon"], LogListItemDelegate::DT_ICON);
    item->setData(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"), LogListItemDelegate::DT_Timestamp);
    item->setData(msg["title"], LogListItemDelegate::DT_Title);
    item->setData(msg["subtitle"], LogListItemDelegate::DT_Subtitle);
    item->setData(msg["details"], LogListItemDelegate::DT_Details);
    item->setData(false, LogListItemDelegate::DT_Expanded);
    // here I am unable to calculate the height, because the details does not have a specific height to set here, 
    // so when append the item to the list it is unvisible. If set the height 64, it is the exact height of the item without details, which is good
    //item->setSizeHint(QSize(0, 64));

    static_cast<QStandardItemModel *>(model())->appendRow(item);
    scrollToBottom();
}

It is the code in sizeHint()

QSize LogListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    bool expanded = index.data(DT_Expanded).toBool();
    QFont fntDetials = option.font;
    fntDetials.setPointSize(12);
    QRect r = option.rect;
    QFontMetrics fm(fntDetials);
    QString details = index.data(DT_Details).toString();
    QRect br = fm.boundingRect(r, Qt::TextWordWrap, details);
    return QSize(option.rect.width(), br.height()+64);
}

Unfortunately not working..., I think Qt can look the Android ListView and its recycle functionality to solve the ListView problem, in this way, I think it is very very painful.

1

There are 1 answers

9
eyllanesc On

If you want to set a custom size you should use the sizeHint method of QStyledItemDelegate, for example:

#include <QApplication>
#include <QStyledItemDelegate>
#include <QListView>
#include <QStandardItemModel>

class HeightDelegate: public QStyledItemDelegate
{
public:
    using QStyledItemDelegate::QStyledItemDelegate;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override{
        QSize s = QStyledItemDelegate::sizeHint(option, index);
        // some calculation
        int h = (index.row()+1)*20;
        s.setHeight(h);
        return s;
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QListView w;
    QStandardItemModel model;
    HeightDelegate delegate;
    w.setItemDelegate(&delegate);
    w.setModel(&model);
    for(int i=0; i<8; i++){
        QStandardItem *it = new QStandardItem(QString::number(i));
        it->setBackground(QBrush(QColor(qrand()%255, qrand()%255, qrand()%255)));
        model.appendRow(it);
    }
    w.show();
    return a.exec();
}

enter image description here