QListView
与QTreeView
一样,也是采取的mvc模式,添加数据时,用相应的模式
1、初始化时添加数据 1 2 3 4 5 6 7 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、根据用户操作来添加数据 1 2 3 4 QStandardItem *item = new QStandardItem("item3" ); QStandardItemModel *model = dynamic_cast <QStandardItemModel*>(ui->listViewTask->model()); model->appendRow(item);
3、删除数据 1 2 QStandardItemModel *model = dynamic_cast <QStandardItemModel*>(ui->listViewTask->model()); model->removeRow(ui->listViewTask->currentIndex().row());
4.获取行数; 1 int row_num = ui->listViewTask->model()->rowCount();
5.选中指定的行号 1 2 QModelIndex qindex = m_listviewmodel_->index(index,0 ); ui->listViewTask->setCurrentIndex(qindex);
6.设置滚动方式 1 2 3 4 5 6 7 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.初始化并设置隔行彩色显示; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1. standardItemModel = new QStandardItemModel(this ); 2. 3. QStringList strList; 4. strList.append("string1" ); 5. strList.append("string2" ); 6. strList.append("string3" ); 7. strList.append("string4" ); 8. strList.append("string5" ); 9. strList.append("string6" ); 10. strList.append("string7" ); 11. strList << "string8" ; 12. strList += "string9" ; 13. int nCount = strList.size(); 14. for (int i = 0 ; i < nCount; i++) 15. { 16. QString string = static_cast <QString>(strList.at(i)); 17. QStandardItem *item = new QStandardItem(string ); 18. if (i % 2 == 1 ) 19. { 20. QLinearGradient linearGrad (QPointF(0 , 0 ), QPointF(200 , 200 )) ; 21. linearGrad.setColorAt(0 , Qt::darkGreen); 22. linearGrad.setColorAt(1 , Qt::yellow); 23. QBrush brush (linearGrad) ; 24. item->setBackground(brush); 25. } 26. standardItemModel->appendRow(item); 27. } 28. listView->setModel(standardItemModel); 29. listView->setFixedSize(200 ,300 ); 30. connect(listView,SIGNAL(clicked(QModelIndex)),this ,SLOT(itemClicked(QModelIndex))); 31. }
8、更新某一行; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 int row=listview->currentIndex().row();if (row!=-1 ){ QModelIndex index=listmodel->index(row); QString str=listmodel->data(index,Qt::DisplayRole).toString(); 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" )); }
QListWidget
是派生于QListView
的。有了QListWidget ,QListView便是废了;因为QListWidget 能完成所有QListView的。没有必要再用listview
添加 1 2 3 4 5 6 7 8 9 10 QString strInput = XGlobal::mGetInputString("请输入任务名称,比如上传省局节点机" ); if (strInput.isEmpty()) { return ; } QListWidgetItem *pItem = new QListWidgetItem(); pItem->setText(strInput); ui->listViewTask->addItem(pItem);
或者这样也行
1 2 3 4 QStringList strList; strList<< "Item1" <<"Item2" << "Item3" <<"Item4" ; this ->addItems(strList);
删除 1 2 3 4 5 6 7 void MainWindow::deletebtn(){ QListWidgetItem *item = ui->listWidget->takeItem(ui->listWidget->currentRow()); delete item; }
1 2 3 4 5 6 7 8 9 10 11 void MainWindow::delallbtn(){ int num = ui->listWidget->count(); ui->listWidget->setFocus(); for (int i=0 ;i<num;i++) { QListWidgetItem *item = ui->listWidget->takeItem(ui->listWidget->currentRow()); delete item; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 void MainWindow::addallbtn(){ QStringList FileNames = QFileDialog::getOpenFileNames(this ,"打开" ,QDir::currentPath(),"所有文件(*.*);;文本文档(*.txt)" ); int index=0 ,count=0 ; count = FileNames.count(); for (index=count-1 ;index>=0 ;index--) { QListWidgetItem *item = new QListWidgetItem; item->setText(FileNames.takeAt(index)); ui->listWidget->addItem(item); } }
点击 1 2 3 4 5 void MainWindow::singleclicked(QListWidgetItem* item){ QMessageBox::information(this ,"单击消息" ,"单击" +item->text()); }
1 2 3 4 5 void MainWindow::doubleclicked(QListWidgetItem* item){ QMessageBox::information(this ,"双击消息" ,"双击" +item->text()); }