Layout of item in QListWidget
-
I'm a newbie in Qt and I need to create a list of combined widgets to manage a drone pre-flight checklist.
I decided to use the QListWidget. I defined the widgets compound (testwidget.cpp) trough QT Designer in this way:
I needed to customize the constructor of the compound in this way:
TestWidget::TestWidget(QTest *test, QWidget *parent) : TestWidget(parent) { mTest = test; ui->lblName->setText(test->label()); ui->lblDescription->setText(test->description()); }Then I placed a QListWidget item in my main form and tried to populate it with some items trough this code:
void MainWindow::initUI() { setPageControls(ui->droneStack->currentIndex()); // Initalize checklist initCheckList(); } void MainWindow::initCheckList() { addTest("Test motor 1", "Execute motor1 test", ui->droneTestList); addTest("Test motor 2", "Execute motor2 test", ui->droneTestList); addTest("Test motor 3", "Execute motor3 test", ui->droneTestList); addTest("Test motor 4", "Execute motor4 test", ui->droneTestList); } void MainWindow::addTest(QString name, QString description, QListWidget* list) { QListWidgetItem *_item = new QListWidgetItem(list); list->addItem(_item); QTest* _test = new QTest(name, description); TestWidget *_widget= new TestWidget(_test); _item->setSizeHint(_widget->sizeHint()); list->setItemWidget(_item, _widget); }When I run the app this's the result:

It looks like the list item is not vertically adjusted to fit the content.
How can I fix it? I thought this line of code should do the magic (as suggested in some sample code):_item->setSizeHint(_widget->sizeHint());But it doesn't seem to have any effect.
-
Hi,
Why are you using a QListWidget since you don't even make use of the model view part of it ?
A QScrollArea would suffice for this task.
-
I'm a newbie in Qt and I need to create a list of combined widgets to manage a drone pre-flight checklist.
I decided to use the QListWidget. I defined the widgets compound (testwidget.cpp) trough QT Designer in this way:
I needed to customize the constructor of the compound in this way:
TestWidget::TestWidget(QTest *test, QWidget *parent) : TestWidget(parent) { mTest = test; ui->lblName->setText(test->label()); ui->lblDescription->setText(test->description()); }Then I placed a QListWidget item in my main form and tried to populate it with some items trough this code:
void MainWindow::initUI() { setPageControls(ui->droneStack->currentIndex()); // Initalize checklist initCheckList(); } void MainWindow::initCheckList() { addTest("Test motor 1", "Execute motor1 test", ui->droneTestList); addTest("Test motor 2", "Execute motor2 test", ui->droneTestList); addTest("Test motor 3", "Execute motor3 test", ui->droneTestList); addTest("Test motor 4", "Execute motor4 test", ui->droneTestList); } void MainWindow::addTest(QString name, QString description, QListWidget* list) { QListWidgetItem *_item = new QListWidgetItem(list); list->addItem(_item); QTest* _test = new QTest(name, description); TestWidget *_widget= new TestWidget(_test); _item->setSizeHint(_widget->sizeHint()); list->setItemWidget(_item, _widget); }When I run the app this's the result:

It looks like the list item is not vertically adjusted to fit the content.
How can I fix it? I thought this line of code should do the magic (as suggested in some sample code):_item->setSizeHint(_widget->sizeHint());But it doesn't seem to have any effect.
@Dragoon said in Layout of item in QListWidget:
Then I placed a QListWidget item in my main form
How? In the designer? Is it in a layout?
-
@Dragoon said in Layout of item in QListWidget:
Then I placed a QListWidget item in my main form
How? In the designer? Is it in a layout?
@jsulm yes the QListWidget is placed in the tab of a QTabWidget and the layout of the page is set as a vertical layout.
<widget class="QWidget" name="uav1checkList"> <property name="font"> <font> <pointsize>11</pointsize> <bold>false</bold> </font> </property> <attribute name="title"> <string>Check List</string> </attribute> <layout class="QVBoxLayout" name="verticalLayout_9"> <item> <widget class="QListWidget" name="uav1TestList"/> </item> </layout> </widget> -
@jsulm yes the QListWidget is placed in the tab of a QTabWidget and the layout of the page is set as a vertical layout.
<widget class="QWidget" name="uav1checkList"> <property name="font"> <font> <pointsize>11</pointsize> <bold>false</bold> </font> </property> <attribute name="title"> <string>Check List</string> </attribute> <layout class="QVBoxLayout" name="verticalLayout_9"> <item> <widget class="QListWidget" name="uav1TestList"/> </item> </layout> </widget>@Dragoon said in Layout of item in QListWidget:
the QListWidget is placed in the tab of a QTabWidget
and is uav1checkList also in a layout? Does it resize when you're resizing the tab?
-
@Dragoon said in Layout of item in QListWidget:
the QListWidget is placed in the tab of a QTabWidget
and is uav1checkList also in a layout? Does it resize when you're resizing the tab?
@jsulm uav1checkList is just one of the page of the container QTabWidget (that's part of a more complex hierarchy).
At run time it seems that all the widgets are resized correctly.<item> <widget class="QTabWidget" name="uav1Stack"> <property name="font"> ... </property> <property name="styleSheet">... </property> <property name="tabPosition">... </property> <property name="currentIndex">... </property> <property name="iconSize">... </property> <widget class="QWidget" name="uav1home"> <widget class="QWidget" name="uav1checkList"> <widget class="QWidget" name="uav1manualControl"> <widget class="QWidget" name="uav1video"> </widget> </item> -
@jsulm uav1checkList is just one of the page of the container QTabWidget (that's part of a more complex hierarchy).
At run time it seems that all the widgets are resized correctly.<item> <widget class="QTabWidget" name="uav1Stack"> <property name="font"> ... </property> <property name="styleSheet">... </property> <property name="tabPosition">... </property> <property name="currentIndex">... </property> <property name="iconSize">... </property> <widget class="QWidget" name="uav1home"> <widget class="QWidget" name="uav1checkList"> <widget class="QWidget" name="uav1manualControl"> <widget class="QWidget" name="uav1video"> </widget> </item>This post is deleted! -
Hi,
Why are you using a QListWidget since you don't even make use of the model view part of it ?
A QScrollArea would suffice for this task.
-
Hi,
Why are you using a QListWidget since you don't even make use of the model view part of it ?
A QScrollArea would suffice for this task.
@SGaist because I cannot find QScrollArea examples while I found many QListWidget ones.

uhm ... it doesn't seem to look the way I meant...:
void MainWindow::addTestItem(QString name, QString description, QScrollArea *list) { QTest *_test = new QTest(name, description); TestWidget *_widget = new TestWidget(_test); list->setWidget(_widget); } -
@SGaist because I cannot find QScrollArea examples while I found many QListWidget ones.

uhm ... it doesn't seem to look the way I meant...:
void MainWindow::addTestItem(QString name, QString description, QScrollArea *list) { QTest *_test = new QTest(name, description); TestWidget *_widget = new TestWidget(_test); list->setWidget(_widget); }@Dragoon said in Layout of item in QListWidget:
because I cannot find QScrollArea examples
https://doc.qt.io/qt-6/qtwidgets-widgets-imageviewer-example.html
-
@Dragoon said in Layout of item in QListWidget:
because I cannot find QScrollArea examples
https://doc.qt.io/qt-6/qtwidgets-widgets-imageviewer-example.html
@jsulm uhm... it doesn't seems to make what I need... this example is an appplication that loads one image inside the window and will manage scrolling bars if needed.
If you load a second image it just replace the first one, or at least that's what it seems to happen from the user point of view...
I haven't digged much on that code since it seems to lack the basic requirement I need.
I need to add several different istances of the same compound of widgets (TestWidget) to a TabWidget page.
I don't doubt that QScrollArea can do it but I'm guessing how much effort it needs in comparision of QListWidget? -
Hi,
Why are you using a QListWidget since you don't even make use of the model view part of it ?
A QScrollArea would suffice for this task.
@SGaist I found a possibile solution using QScrollArea.. I needed to add a VerticalLayout into ScrollAreaContentWidget and then add the compound widget to such layout:
void MainWindow::addTestItem(QString name, QString description, QVBoxLayout *layout) { QTest *_test = new QTest(name, description); TestWidget *_widget = new TestWidget(_test); layout->addWidget(_widget); }Now it seems to display correctly:

-
D Dragoon has marked this topic as solved on