QListView 不显示模型中的数据

QListView not showing data in model

当我 运行 程序时,列表视图不显示我设置的模型中的数据。我在做什么missing/where我哪里出错了?

QStandardItemModel mymodel(this);

    QStandardItem *item1 = new QStandardItem("This is item one");
    item1->setData("item", Qt::UserRole + 1);
    mymodel.appendRow(item1);
    QStandardItem *item2 = new QStandardItem("This is item two");
    item2->setData("item", Qt::UserRole + 1);
    mymodel.appendRow(item2);
    QStandardItem *item3 = new QStandardItem("This is item three");
    item3->setData("item", Qt::UserRole + 1);
    mymodel.appendRow(item3);

    ui->listView->setResizeMode(QListView::Adjust);
    ui->listView->setWordWrap(true);
    ui->listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    ui->listView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listView->setModel(&mymodel);
    ui->listView->showNormal();

感谢@vahancho - 你应该从堆中分配。这是修改后的代码段:

QStandardItemModel *mymodel = new QStandardItemModel(this);

    QStandardItem *item1 = new QStandardItem("This is item one");
    item1->setData("item", Qt::UserRole + 1);
    mymodel->appendRow(item1);
    QStandardItem *item2 = new QStandardItem("This is item two");
    item2->setData("item", Qt::UserRole + 1);
    mymodel->appendRow(item2);
    QStandardItem *item3 = new QStandardItem("This is item three");
    item3->setData("item", Qt::UserRole + 1);
    mymodel->appendRow(item3);

    ui->listView->setResizeMode(QListView::Adjust);
    ui->listView->setWordWrap(true);
    ui->listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    ui->listView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listView->setModel(mymodel);
    ui->listView->showNormal();