[solved] QListWidget::setItemWidget() problem
-
@
#include <QtGui>int main(int argc, char *argv[])
{
QApplication a(argc, argv);QListWidget* lw = new QListWidget(); QListWidgetItem* lwi = new QListWidgetItem("Item"); lw->addItem(lwi); QWidget* w = new QWidget(); QVBoxLayout* vbl = new QVBoxLayout(); QLabel* lab_01 = new QLabel("label 01"); QLabel* lab_02 = new QLabel("label 03"); QPushButton* pb = new QPushButton("button"); vbl->addWidget(lab_01); vbl->addWidget(lab_02); vbl->addWidget(pb); w->setLayout(vbl); lw->setItemWidget(lwi, w); lw->show(); return a.exec();
}
@Why don't replace my "simple" QListWidgetItem with the "complicated" QWidget ?
-
Finally, I have found a solution:
@
#include <QtGui>int main(int argc, char *argv[])
{
QApplication a(argc, argv);QListWidget* lw = new QListWidget(); for (int var = 0; var < 10; ++var) { QWidget* w = new QWidget(); QVBoxLayout* vbl = new QVBoxLayout(w); QLabel* lab_01 = new QLabel("label 01"); QLabel* lab_02 = new QLabel("label 03"); QPushButton* pb = new QPushButton("button"); vbl->addWidget(lab_01); vbl->addWidget(lab_02); vbl->addWidget(pb); vbl->setSizeConstraint( QLayout::SetFixedSize ); w->setLayout(vbl); QListWidgetItem* lwi = new QListWidgetItem; lwi->setSizeHint( w->sizeHint() ); lw->addItem(lwi); lw->setItemWidget(lwi, w); } lw->show(); return a.exec();
}
@setSizeConstraint and setSizeHint are must... (but why? :))