C/C++?Qt?StringListModel?字符串列表映射組件詳解
QStringListModel簡介
QStringListModel 用于處理字符串列表的數(shù)據(jù)模型,它可以作為 QListView 的數(shù)據(jù)模型,在界面上顯示和編輯字符串列表。
QStringListModel 的 setStringList() 函數(shù)可以初始化數(shù)據(jù)模型的字符串列表的內(nèi)容,stringList() 函數(shù)返回數(shù)據(jù)模型內(nèi)的字符串列表,在關(guān)聯(lián)的 ListView 組件里編輯修改數(shù)據(jù)后,數(shù)據(jù)都會及時更新到數(shù)據(jù)模型內(nèi)的字符串列表里。
QStringListModel 提供編輯和修改字符串列表數(shù)據(jù)的函數(shù),如 insertRows()、removeRows()、setData() 等,這些操作直接影響數(shù)據(jù)模型內(nèi)部的字符串列表,并且修改后的數(shù)據(jù)會自動在關(guān)聯(lián)的 ListView 組件里刷新顯示。
StringListModel 字符串列表映射組件,該組件用于處理字符串與列表框組件中數(shù)據(jù)的轉(zhuǎn)換,通常該組件會配合ListView組件一起使用,例如將ListView組件與Model模型綁定,當(dāng)ListView組件內(nèi)有數(shù)據(jù)更新時,我們就可以利用映射將數(shù)據(jù)模型中的數(shù)值以字符串格式提取出來,同理也可實現(xiàn)將字符串賦值到指定的ListView組件內(nèi)。
首先在UI界面中排版

默認(rèn)的MainWindow::MainWindow構(gòu)造函數(shù)中,我們首先初始化一個QStringList字符串鏈表并對該鏈表賦值,通過new QStringListModel(this);創(chuàng)建一個數(shù)據(jù)模型,并通過ui->listView->setModel(model);屬性將模型與ListView組件綁定,當(dāng)ListView組件被選中是則觸發(fā)on_listView_clicked事件實現(xiàn)輸出當(dāng)前選中行,其初始化代碼部分如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStringList>
#include <QStringListModel>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 初始化一個StringList字符串列表
QStringList theStringList;
theStringList << "北京" << "上海" << "廣州";
// 創(chuàng)建并使用數(shù)據(jù)模型
model = new QStringListModel(this); // 創(chuàng)建模型
model->setStringList(theStringList); // 導(dǎo)入模型數(shù)據(jù)
ui->listView->setModel(model); // 為listView設(shè)置模型
ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked |
QAbstractItemView::SelectedClicked);
}
MainWindow::~MainWindow()
{
delete ui;
}
// 當(dāng)ListView列表項被選中時,顯示QModelIndex的行、列號
void MainWindow::on_listView_clicked(const QModelIndex &index)
{
ui->LabInfo->setText(QString::asprintf("當(dāng)前項:row=%d, column=%d",
index.row(),index.column()));
}
代碼運行效果:

添加代碼:需要通過model->index()獲取到最后一行的索引,然后使用model->setData()追加寫入數(shù)據(jù)到最后一條索引位置。
插入代碼: 需要通過ui->listView->currentIndex()獲取到當(dāng)前光標(biāo)位置,并調(diào)用model->setData()插入到指定位置。
刪除代碼: 直接調(diào)用model->removeRows()等函數(shù)即可將指定位置刪除。
// 添加一行
void MainWindow::on_btnListAppend_clicked()
{
model->insertRow(model->rowCount()); // 在尾部插入一行
QModelIndex index = model->index(model->rowCount()-1,0); // 獲取最后一行的索引
QString LineText = ui->lineEdit->text();
model->setData(index,LineText,Qt::DisplayRole); // 設(shè)置顯示文字
ui->listView->setCurrentIndex(index); // 設(shè)置當(dāng)前行選中
ui->lineEdit->clear();
}
// 插入一行數(shù)據(jù)到ListView
void MainWindow::on_btnListInsert_clicked()
{
QModelIndex index;
index= ui->listView->currentIndex(); // 獲取當(dāng)前選中行
model->insertRow(index.row()); // 在當(dāng)前行的前面插入一行
QString LineText = ui->lineEdit->text();
model->setData(index,LineText,Qt::DisplayRole); // 設(shè)置顯示文字
model->setData(index,Qt::AlignRight,Qt::TextAlignmentRole); // 設(shè)置對其方式
ui->listView->setCurrentIndex(index); // 設(shè)置當(dāng)前選中行
}
// 刪除當(dāng)前選中行
void MainWindow::on_btnListDelete_clicked()
{
QModelIndex index;
index = ui->listView->currentIndex(); // 獲取當(dāng)前行的ModelIndex
model->removeRow(index.row()); // 刪除選中行
}
// 清除當(dāng)前列表
void MainWindow::on_btnListClear_clicked()
{
model->removeRows(0,model->rowCount());
}
代碼運行效果:

如果需要實現(xiàn)將ListView數(shù)據(jù)模型中的數(shù)據(jù)導(dǎo)出到plaintextEdit組件中,則需要通過model->stringList()獲取到ListView中的每行并將其賦值到QStringList字符串鏈表中,最后通過循環(huán)的方式依次插入到plainTextEdit中即可,插入時默認(rèn)會以逗號作為分隔符。
// 顯示數(shù)據(jù)模型文本到QPlainTextEdit
void MainWindow::on_btnTextImport_clicked()
{
QStringList pList;
pList = model->stringList(); // 獲取數(shù)據(jù)模型的StringList
ui->plainTextEdit->clear(); // 先清空文本框
// 循環(huán)追加數(shù)據(jù)
for(int x=0;x< pList.count();x++)
{
ui->plainTextEdit->appendPlainText(pList.at(x) + QString(","));
}
}
代碼運行效果:

到此這篇關(guān)于C/C++ Qt StringListModel 字符串列表映射組件的文章就介紹到這了,更多相關(guān)C++ Qt StringListModel 字符串列表映射組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

