Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Stylesheets and sizeFromContents
Qt 6.11 is out! See what's new in the release blog

Stylesheets and sizeFromContents

Scheduled Pinned Locked Moved Unsolved Language Bindings
2 Posts 2 Posters 1.3k 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.
  • PemdasP Offline
    PemdasP Offline
    Pemdas
    wrote on last edited by
    #1

    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
    {
        Q_OBJECT
        
        public:
            MyWidget();
            ~MyWidget();
        private:
            QLabel myLabel;
    }
    
    MyWidget::MyWidget(QWidget *parent = 0) :
     Qwidget(parent),
    myLabel(new QLabel("",this)
    {
    }
    

    ....

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

    Example:

    MyWidget,
    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;
        if(index.data().canConvert<MyModelItem>())
        {
            MyModelItem item = qvariant_cast<MyModelItem>(index.data());
            qDebug() << QApplication::style()->sizeFromContents(QStyle::CT_ItemViewItem, &option, QSize(),  item.getItem()) << endl
            
            return style->sizeFromContents(QStyle::CT_ItemViewItem, &option, QSize(),  item.getItem());
        }
        else
        {
            
            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.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      elveatles
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

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