QT6
QStringListModel和QListView
QStringListModel是处理字符串列表的模型类,其实例可以作为QListView组件的数据模型,这两个类结合,构成模型/视图结构,可以在界面上显示和编辑字符串列表
常用属性和方法
常用属性
| 属性名称 | 类型 | 描述 | 
|---|---|---|
batchSize | 
int | 
当 layoutMode 设置为 Batched 时,控制每批布局的项目数量 | 
flow | 
QListView::Flow | 
项目布局方向(LeftToRight 或 TopToBottom) | 
gridSize | 
QSize | 
布局项目时使用的网格大小 | 
isWrapping | 
bool | 
指示项目布局是否应换行 | 
itemAlignment | 
Qt::Alignment | 
项目中每个单元格的对齐方式 | 
layoutMode | 
QListView::LayoutMode | 
项目布局模式(SinglePass 或 Batched) | 
modelColumn | 
int | 
模型中可见的列 | 
movement | 
QListView::Movement | 
项目移动方式(Static, Free, Snap) | 
resizeMode | 
QListView::ResizeMode | 
调整视图大小时的行为(Fixed 或 Adjust) | 
spacing | 
int | 
布局中项目周围的空间大小 | 
uniformItemSizes | 
bool | 
指示所有项目是否具有相同的大小 | 
viewMode | 
QListView::ViewMode | 
视图显示模式(ListMode 或 IconMode) | 
wordWrap | 
bool | 
指示项目文本是否应在单词边界处换行 | 
selectionRectVisible | 
bool | 
选择矩形是否可见 | 
alternatingRowColors | 
bool | 
是否启用交替行颜色 | 
常用方法
| 方法名称 | 返回类型 | 描述 | 
|---|---|---|
setModel(QAbstractItemModel *model) | 
void | 
设置视图要显示的模型 | 
setRootIndex(const QModelIndex &index) | 
void | 
设置模型的根索引 | 
setSelectionModel(QItemSelectionModel *selectionModel) | 
void | 
设置视图的选择模型 | 
indexAt(const QPoint &p) const | 
QModelIndex | 
返回位于给定点处的项目模型索引 | 
scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible) | 
void | 
滚动视图以使模型索引可见 | 
visualRect(const QModelIndex &index) const | 
QRect | 
返回索引对应项的可视矩形区域 | 
setRowHidden(int row, bool hide) | 
void | 
设置指定行是隐藏还是显示 | 
isRowHidden(int row) const | 
bool | 
返回指定行是否已隐藏 | 
clearPropertyFlags() | 
void | 
清除设置的 ViewMode 属性标志 | 
常用信号
| 信号名称 | 描述 | 
|---|---|
clicked(const QModelIndex &index) | 
鼠标在项目上点击时发出 | 
doubleClicked(const QModelIndex &index) | 
鼠标在项目上双击时发出 | 
activated(const QModelIndex &index) | 
用户激活项目时发出 | 
entered(const QModelIndex &index) | 
鼠标光标进入项目时发出 | 
pressed(const QModelIndex &index) | 
鼠标在项目上按下时发出 | 
indexesMoved(const QModelIndexList &indexes) | 
指定索引在视图中移动时发出 | 
代码测试
            
            
              cpp
              
              
            
          
          #include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QStringListModel>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_strList << "北京" << "上海" << "南昌" << "南京" << "成都";
    m_model = new QStringListModel(this);
    // 和数据进行关联
    m_model->setStringList(m_strList);
    // 给ListView设置模型
    ui->listView->setModel(m_model);
    ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::SelectedClicked);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_btnRecoveryList_clicked()
{
    m_model->setStringList(m_strList);
}
void MainWindow::on_btnClearList_clicked()
{
    m_model->removeRows(0,m_model->rowCount());
}
void MainWindow::on_checkBox_clicked(bool checked)
{
    if(checked){
        ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::SelectedClicked);
    }else{
        ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}
void MainWindow::on_btnAddItem_clicked()
{
    m_model->insertRow(m_model->rowCount());
    QModelIndex index = m_model->index(m_model->rowCount() - 1);
    m_model->setData(index,"new item",Qt::DisplayRole);
    ui->listView->setCurrentIndex(index);
}
void MainWindow::on_btnInsterItem_clicked()
{
    QModelIndex index = ui->listView->currentIndex();
    m_model->insertRow(index.row());
    m_model->setData(index,"ins item",Qt::DisplayRole);
    ui->listView->setCurrentIndex(index);
}
void MainWindow::on_btndelItem_clicked()
{
    QModelIndex index = ui->listView->currentIndex();
    m_model->removeRow(index.row());
    ui->listView->setCurrentIndex(index);
}
void MainWindow::on_btnUpMove_clicked()
{
    QModelIndex index;
    int curRow = ui->listView->currentIndex().row();
    m_model->moveRow(index,curRow,index,curRow-1 );
}
void MainWindow::on_btnDownMove_clicked()
{
    QModelIndex index;
    int curRow = ui->listView->currentIndex().row();
    m_model->moveRow(index,curRow,index,curRow+2 );
}
void MainWindow::on_btnSort_clicked(bool checked)
{
    if(checked){
        m_model->sort(0,Qt::AscendingOrder);
    }else{
        m_model->sort(0,Qt::DescendingOrder);
    }
}
void MainWindow::on_btnClearText_clicked()
{
    ui->plainTextEdit->clear();
}
void MainWindow::on_btnDisplayList_clicked()
{
    ui->plainTextEdit->clear();
    QStringList tempList = m_model->stringList();
    for(int i = 0 ; i < tempList.size() ; i++){
        ui->plainTextEdit->appendPlainText(tempList.at(i));
    }
}
void MainWindow::on_listView_clicked(const QModelIndex &index)
{
    QString str = QString::asprintf("模型索引:row=%d,colum=%d",index.row(),index.row());
    str+=m_model->data(index,Qt::DisplayRole).toString();
    ui->statusbar->showMessage(str);
}
        效果展示
