Important: Please read the Qt Code of Conduct -

Stylesheets and sizeFromContents

  • I am trying to get a better understanding of the behavior I describe below.

    I sub-classed QWidget to create a custom widget. For the sake of this question assume all that I did was add a label.

    class MyWidget : public QWidget
            QLabel myLabel;
    MyWidget::MyWidget(QWidget *parent = 0) :
    myLabel(new QLabel("",this)


    I have stylesheet loaded into the application that specifies the minimum and maxiume size of MyWidget and the QLabel.


    MyWidget QLabel {
    min-height: 65;
    max-height: 65:
    min-width: 200;
    max-width: 200;

    I am trying to draw this inside a listview with a custom delegate but the sizeHint function that I wrote does not behave as I would expect.

    QSize MyItemDelegate::sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const
        qDebug() << "sizehint" << endl;
            MyModelItem item = qvariant_cast<MyModelItem>(;
            qDebug() << QApplication::style()->sizeFromContents(QStyle::CT_ItemViewItem, &option, QSize(),  item.getItem()) << endl
            return style->sizeFromContents(QStyle::CT_ItemViewItem, &option, QSize(),  item.getItem());
            qDebug() << "Delegate...something broken" << QStyledItemDelegate::sizeHint( option, index) << endl;
            return QStyledItemDelegate::sizeHint( option, index);

    MyModelItem is just structure that has pointer to a MyWidget object.

    This returns a QSize of 0,37. I am trying to figure out why it is not returning the size that I specified in the stylesheet.

  • I'm not sure if this is going to fix your problem, but I think things like min-width, max-width, etc are supposed to end with px such as 65px; and 200px;. Also, you might want to post this in the General Desktop category. You posted this in Language Bindings which gets a lot less views than General Desktop. But you aren't using PyQt, PySide, etc.

Log in to reply