您好,登錄后才能下訂單哦!
這篇文章主要介紹“C/C++ Qt StringListModel字符串列表映射組件怎么使用”,在日常操作中,相信很多人在C/C++ Qt StringListModel字符串列表映射組件怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C/C++ Qt StringListModel字符串列表映射組件怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
StringListModel 字符串列表映射組件,該組件用于處理字符串與列表框組件中數(shù)據(jù)的轉(zhuǎn)換,通常該組件會配合ListView組件一起使用,例如將ListView組件與Model模型綁定,當(dāng)ListView組件內(nèi)有數(shù)據(jù)更新時,我們就可以利用映射將數(shù)據(jù)模型中的數(shù)值以字符串格式提取出來,同理也可實(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
事件實(shí)現(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列表項(xiàng)被選中時,顯示QModelIndex的行、列號 void MainWindow::on_listView_clicked(const QModelIndex &index) { ui->LabInfo->setText(QString::asprintf("當(dāng)前項(xiàng):row=%d, column=%d", index.row(),index.column())); }
代碼運(yùn)行效果:
添加代碼:需要通過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()); }
代碼運(yùn)行效果:
如果需要實(shí)現(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(",")); } }
代碼運(yùn)行效果:
到此,關(guān)于“C/C++ Qt StringListModel字符串列表映射組件怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。