PDA

View Full Version : Can't change state of checkable QListViewItem with custom widget



pafcu
23rd December 2010, 08:40
I have a QListWidget where I want to add a bunch of items with a custom widget:



#include <QApplication>
#include <QListWidget>
#include <QListWidgetItem>
#include <QPushButton>

int main(int argc, char** argv)
{
QApplication* app = new QApplication(argc, argv);
QListWidget* listWidget = new QListWidget();
QListWidgetItem* item = new QListWidgetItem();
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(Qt::Unchecked);
listWidget->addItem(item);
QWidget* widget = new QPushButton("Example widget");
item->setSizeHint(widget->sizeHint());
listWidget->setItemWidget(item, widget);

listWidget->show();
app->exec();
}



The problem is that I can not use the checkbox that appears in the listwidget next to the added widget. It looks completely normal, but nothing happens when I click on it. If I remove the line with setSizeHint it works correctly, but then my widget is no longer visible. How do I solve this problem?