标签:dex ext tar 数据 his data cas 添加 文件
QListView与QTreeView一样,也是采取的mvc模式,添加数据时,用相应的模式
1、初始化时添加数据
QStandardItemModel *model = new QStandardItemModel(this); //可以把这行放到头文件里面
QStandardItem *item = new QStandardItem("item1");
model->appendRow(item);
item = new QStandardItem("item2");
model->appendRow(item);
ui->listViewTask->setModel(model);
2、根据用户操作来添加数据
QStandardItem *item = new QStandardItem("item3");
QStandardItemModel *model = dynamic_cast<QStandardItemModel*>(ui->listViewTask->model());
model->appendRow(item);
3、删除数据
QStandardItemModel *model = dynamic_cast<QStandardItemModel*>(ui->listViewTask->model());
model->removeRow(ui->listViewTask->currentIndex().row());
4.获取行数;
int row_num = ui->listViewTask->model()->rowCount();
5.选中指定的行号
QModelIndex qindex = m_listviewmodel_->index(index,0); //默认选中 index
ui->listViewTask->setCurrentIndex(qindex);
6.设置滚动方式
enum QAbstractItemView::ScrollMode
Constant Value Description
QAbstractItemView::ScrollPerItem 0 The view will scroll the contents one item at a time.
QAbstractItemView::ScrollPerPixel 1 The view will scroll the contents one pixel at a time.
//设置为像素滚动;
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
7.初始化并设置隔行彩色显示;
8。更新某一行;
int row=listview->currentIndex().row();
if(row!=-1)
{
QModelIndex index=listmodel->index(row);
QString str=listmodel->data(index,Qt::DisplayRole).toString();//get data
bool ok;
QString text = QInputDialog::getText(this, tr("edit item"),
tr("please edit item"), QLineEdit::Normal,
str, &ok);
if (ok && !text.isEmpty())
{
listmodel->setData(index,text,Qt::EditRole);
listview->setCurrentIndex(index);
}
}
else
{
QMessageBox::information(NULL,tr("information"),tr("please select a item"));
}
标签:dex ext tar 数据 his data cas 添加 文件
原文地址:http://www.cnblogs.com/zhoujgssp/p/7782278.html