QStringListModel 绑定到QListView

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);
    else
        ui->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-2
    2. 复制原curRow行到目标位置curRow-2
    3. 删除原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+1
    3. 删除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);
    else
        m_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行号

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现在PyQt5中通过搜索添加文本到QListView中: 1. 创建一个QListView对象: ```python listView = QtWidgets.QListView() ``` 2. 创建一个QStringListModel对象,用于保存搜索结果: ```python listModel = QtCore.QStringListModel() ``` 3. 连接搜索框的文本改变信号到一个回调函数中,在回调函数中根据搜索框的文本更新QStringListModel对象: ```python def onSearchTextChanged(self, text): # 这里的search()函数是根据搜索框的文本查找符合条件的结果的函数 result = search(text) listModel.setStringList(result) ``` 4. 将QStringListModel对象设置为QListView的模型: ```python listView.setModel(listModel) ``` 完整的代码示例: ```python from PyQt5 import QtCore, QtGui, QtWidgets class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() # 创建搜索框 self.searchEdit = QtWidgets.QLineEdit() self.searchEdit.textChanged.connect(self.onSearchTextChanged) # 创建QListView self.listView = QtWidgets.QListView() # 创建QStringListModel self.listModel = QtCore.QStringListModel() # 设置模型 self.listView.setModel(self.listModel) # 创建布局 centralWidget = QtWidgets.QWidget(self) layout = QtWidgets.QVBoxLayout(centralWidget) layout.addWidget(self.searchEdit) layout.addWidget(self.listView) self.setCentralWidget(centralWidget) def onSearchTextChanged(self, text): # 这里的search()函数是根据搜索框的文本查找符合条件的结果的函数 result = search(text) self.listModel.setStringList(result) def search(text): # 这里假设搜索结果是一个字符串列表 return ['Result 1', 'Result 2', 'Result 3'] if __name__ == '__main__': app = QtWidgets.QApplication([]) mainWindow = MainWindow() mainWindow.show() app.exec_() ``` 这个示例中,我们创建了一个搜索框和一个QListView,然后连接了搜索框的文本改变信号到一个回调函数中,在回调函数中根据搜索框的文本更新了QStringListModel对象,并将其设置为QListView的模型。最后在搜索函数中返回了一个字符串列表作为搜索结果。在实际使用中,可以根据具体需求实现自己的搜索函数,例如从数据库中查找符合条件的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值