I dont understand the layout mechanism :( (i came from WPF)



  • Hello guys,

    i have an custom widget, this contains a QLabel as headline , and the rest of the widget space shoud used by a QListWidget.

    my code:

    QVBoxLayout *root = new QVBoxLayout(this);
    	root->setSizeConstraint(QLayout::SetMinimumSize); /* mybay be this */
    	setLayout(root);
    
    	_headLine = new QLabel("HeadLine", this);
    	_headLine->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); /* or maybe that */
    	root->addWidget(_headLine, Qt::AlignTop); /* and align top? */
    
    	_alarmListView = new QListWidget(this);
    	_alarmListView->setItemDelegate(new ItemDelegate(_alarmListView));
    	root->addWidget(_alarmListView);
    

    but both elems in my layout takes the same space 50/50 ! But i want, that the head line (QLabel) only takes its neede height, an the rest for the QListWidget.
    Do i need a another layout?

    For example , in WPF i can take a Dockpanel, and dock the Label to the top, and the reset space is uses by the list control. But in QT it seem must more complicated... to layout :(

    can


  • Qt Champions 2018

    quick solution: just don't overdo it:

    	QVBoxLayout *root = new QVBoxLayout(this);
    	_headLine = new QLabel("HeadLine", this);
    	root->addWidget(_headLine);
    	_alarmListView = new QListWidget(this);
    	_alarmListView->setItemDelegate(new ItemDelegate(_alarmListView));
    	root->addWidget(_alarmListView);
    

    Explanation:
    QSizePolicy::Minimum does not do what you think it does:

    from https://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum

    The sizeHint() is minimal, and sufficient. The widget can be expanded, but there is no advantage to it being larger (e.g. the horizontal direction of a push button). It cannot be smaller than the size provided by sizeHint().

    What you want is QSizePolicy::Maximum but, again, you don't need to do anything, the default already does what you want



  • Oh thank;) it works.. but when i add "Qt::AlignTop" , does a 50/50 allignment.. thats confusing :(


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.