如何添加QListView项目的复选框?

时间:2017-02-22 07:16:30

标签: c++ qt

如何使用QListView项添加复选框?我正在尝试,但它没有出现在qlistview中。以下是我的代码:

for(int i = 0;i<myParent->listItem.size();i++) {
    QHash<QString, QString> hash;
    hash = myParent->listItem.at(i);      
    QStandardItem* item = new QStandardItem;
    item->setText(hash.value("text"));
    item->setCheckable(true);
     model->appendRow(item);
}
ui->listView->setModel(model);

1 个答案:

答案 0 :(得分:0)

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);

  // Add QListWidgets in to yout ui dialog box. and that that object here as "ui->listWidget"

  QListWidgetItem * item1 = new QListWidgetItem();
  QListWidgetItem * item2 = new QListWidgetItem();
  QListWidgetItem * item3 = new QListWidgetItem();
  QListWidgetItem * item4 = new QListWidgetItem();
  QListWidgetItem * item5 = new QListWidgetItem("Text 1");


  ui->listWidget->addItem(item1);
  ui->listWidget->addItem(item2);
  ui->listWidget->addItem(item3);
  ui->listWidget->addItem(item4);
  ui->listWidget->addItem(item5);

  ui->listWidget->setItemWidget(item1,new QCheckBox("Test Widget"));
  ui->listWidget->setItemWidget(item2,new QPushButton("Test Widget"));
  ui->listWidget->setItemWidget(item3,new QRadioButton("Test Widget"));
  ui->listWidget->setItemWidget(item4,new QCheckBox("Test Widget"));


  ui->listWidget->setWindowTitle("QListWidget Add Widgets Example");

}