Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Drawing custom Widgets



  • I am looking for some advice on how to implement a list view with custom content in each row. I have implemented exactly what I want using QLIstWidget, but the QListwidget "eats" any widgets that I pass to it and I don't want that behavior. Also, this list could be "long" and I don't want to allocate 100s of widgets.

    The easiest why to describe what I am trying to accomplish is to look at my delegate paint function. This is just a simple example for the sake of this question.

    void CustomItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    const QModelIndex &index) const
    {

    qDebug() << "Painting something" << endl;
    if(index.data().canConvert<CustomModelItem>())
    {
        CustomModellItem item = qvariant_cast<CustmomModelItem>(index.data());
        QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &option, painter, item.getItem());
    }
    else
    {
        qDebug() << "Delegate...something broken 2" << endl;
        QStyledItemDelegate::paint(painter, option, index);
    
    }
    

    }
    CustomModelItem is nothing more than a wrapper structure that holds a pointer a custom widget.

    I realize that I could just manual draw the control since it is nothing more than a collection of labels and icons, but I want to draw the item with properties that are defined in a stylesheet. Any advice would be appreciated.


Log in to reply