QStringListModel

创建模型:

QStringListModel* model = new QStringListModel(this);

初始化列表:

QStringList strList;
    strList << QStringLiteral("北京") << QStringLiteral("上海") << QStringLiteral("天津") << QStringLiteral("河北") << QStringLiteral("山东") << QStringLiteral("四川") << QStringLiteral("重庆") << QStringLiteral("广东") << QStringLiteral("河南");

将列表加进模型:

model->setStringList(strList);

为视图设置模型:

ui.listView->setModel(model);

添加行:

void port::on_addItem_clicked()
{
	model->insertRow(model->rowCount());
	QModelIndex index = model->index(model->rowCount() - 1,0);
	model->setData(index,"new item",Qt::DisplayRole);
	ui.listView->setCurrentIndex(index);
}

插入项:

void port::on_insertItem_clicked()
{
	QModelIndex index;
	index = ui.listView->currentIndex();
	model->insertRow(index.row());
	model->setData(index,"inserted item",Qt::DisplayRole);
	model->setData(index,Qt::AlignRight,Qt::TextAlignmentRole);
	ui.listView->setCurrentIndex(index);
}

删除项:

void port::on_deleteItem_clicked()
{
	QModelIndex index;
	index = ui.listView->currentIndex();
	model->removeRow(index.row());
}

清空项:

void port::on_clearList_clicked()
{
	model->removeRows(0,model->rowCount());
}

获取模型数据:

QStringList strList;
    strList = model->stringList();

清空文本:

ui.plainTextEdit->clear();

将数据显示到文本上:

for (int i = 0; i < strList.count(); i++)
        ui.plainTextEdit->appendPlainText(strList.at(i));

你可能感兴趣的:(java,前端,服务器)