1.QStringListModel 綁定到listView,從而實現MV模型視圖
2.通過QStringListModel的新增、刪除、插入、上下移動,listView來展示出來
3.下移動一行,傳入curRow+2 的個人理解
布局
.h聲明?
private:QStringList m_strList;QStringListModel *m_model;
.cpp?
#include "listmodelviewexample.h"
#include "ui_listmodelviewexample.h"ListModelViewExample::ListModelViewExample(QWidget *parent): QMainWindow(parent), ui(new Ui::ListModelViewExample)
{ui->setupUi(this);m_strList<<"北京"<<"上海"<<"廣州"<<"深圳"<<"天津"<<"成都"<<"山東"<<"河南"<<"河北";m_model= new QStringListModel(this);m_model->setStringList(m_strList);ui->listView->setModel(m_model);ui->chkEditable->setChecked(true);ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::SelectedClicked);
}ListModelViewExample::~ListModelViewExample()
{delete ui;
}void ListModelViewExample::on_btnIniList_clicked()
{m_model->setStringList(m_strList);//重新載入
}void ListModelViewExample::on_btnListClear_clicked()
{m_model->removeRows(0,m_model->rowCount());
}void ListModelViewExample::on_chkEditable_clicked(bool checked)
{if(checked)ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::SelectedClicked);elseui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}void ListModelViewExample::on_btnListAppend_clicked()
{m_model->insertRow(m_model->rowCount());QModelIndex index= m_model->index(m_model->rowCount()-1,0);m_model->setData(index,"new Item",Qt::DisplayRole);ui->listView->setCurrentIndex(index);
}void ListModelViewExample::on_btnListInsert_clicked()
{QModelIndex index= ui->listView->currentIndex();m_model->insertRow(index.row());m_model->setData(index,Qt::AlignRight,Qt::TextAlignmentRole);ui->listView->setCurrentIndex(index);
}void ListModelViewExample::on_btnListDelete_clicked()
{QModelIndex index= ui->listView->currentIndex();m_model->removeRow(index.row());
}void ListModelViewExample::on_btnListMoveUp_clicked()
{int curRow = ui->listView->currentIndex().row();QModelIndex index = QModelIndex();/*moveRow這個方法,為什么要-1? 我理解如下,1. 在目標位置curRow-1插入一行 插入的新行的行號為curRow-22. 復制原curRow行到目標位置curRow-23. 刪除原curRow行*/m_model->moveRow(index,curRow,index,curRow-1);
}void ListModelViewExample::on_btnListMoveDown_clicked()
{int curRow = ui->listView->currentIndex().row();QModelIndex index = QModelIndex();/*moveRow這個方法,為什么要+2? 我理解如下,1. 在目標位置curRow+2插入一行2. 復制curRow行到目標位置curRow+13. 刪除curRow行*/m_model->moveRow(index,curRow,index,curRow+2);
}void ListModelViewExample::on_btnClearText_clicked()
{ui->plainTextEdit->clear();
}void ListModelViewExample::on_btnListImport_clicked()
{QStringList tmpList = m_model->stringList();for(int i=0;i<tmpList.size();i++){ui->plainTextEdit->appendPlainText(tmpList.at(i));}
}void ListModelViewExample::on_btnListSort_clicked(bool checked)
{if(checked)m_model->sort(0,Qt::AscendingOrder);elsem_model->sort(0,Qt::DescendingOrder);
}void ListModelViewExample::on_listView_clicked(const QModelIndex &index)
{QString str1 = QString::asprintf("模型索引行號:row=%d,column=%d;\t",index.row(),index.column());QVariant var = m_model->data(index,Qt::DisplayRole);QString str2 = var.toString();int curRow = ui->listView->currentIndex().row();QString str3 = QString::asprintf(";\tlistView:row=%d",curRow);ui->statusbar->showMessage(str1+ str2+ str3);}
?QStringListModel->moveRow? 上移傳入curRow-1? 下移傳入curRow+2? 這是為什么?有些別扭
?
inline bool QAbstractItemModel::moveRow(const QModelIndex &sourceParent, int sourceRow,const QModelIndex &destinationParent, int destinationChild)
以下僅為個人理解。
destinationChild:創建了一個新行,該新行需要插入的位置,插入新行后,原行刪除。
如下移一行,就需要在curRow+2的前面插入一行,插入的新行的行號為curRow+2,刪除原行后變成curRow+1
再如上移一行,需要在curRow-1的前面插入一行,插入的新行號變成了curRow-1,而原curRow-1變成了curRow行號