Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Qt StringListModel string list mapping component

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use Qt StringListModel string list mapping component". In daily operation, I believe that many people have doubts about how to use the Qt StringListModel string list mapping component. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about how to use the Qt StringListModel string list mapping component. Next, please follow the editor to study!

StringListModel string list mapping component, which is used to handle the conversion of strings and data in the listbox component, which is usually used with ListView components, such as binding the ListView component to the Model model, when there is a data update in the ListView component, we can use mapping to extract the values in the data model in string format, and similarly, we can assign strings to the specified ListView component.

First typesetting in the UI interface

In the default MainWindow::MainWindow constructor, we first initialize a list of QStringList strings and assign values to the list through new QStringListModel (this); create a data model and bind the model to the ListView component through ui- > listView- > setModel (model). When the ListView component is selected, the on_listView_clicked event is triggered to output the currently selected row. The initialization code is as follows:

# include "mainwindow.h" # include "ui_mainwindow.h" # include # include MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); / / initialize a list of StringList strings QStringList theStringList; theStringList setModel (model); / / set the model ui- > listView- > setEditTriggers (QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked) for listView } MainWindow::~MainWindow () {delete ui;} / / when the ListView list item is selected, the row and column numbers of QModelIndex are displayed: void MainWindow::on_listView_clicked (const QModelIndex & index) {ui- > LabInfo- > setText (QString::asprintf ("current item: row=%d, column=%d", index.row (), index.column ());}

The running effect of the code:

Add code: you need to get the index of the last row through model- > index (), and then append the write data to the last index location using model- > setData ().

Insert code: you need to get the current cursor position through ui- > listView- > currentIndex () and insert it to the specified location by calling model- > setData ().

Delete code: you can delete the specified location by directly calling functions such as model- > removeRows ().

/ / add a row void MainWindow::on_btnListAppend_clicked () {model- > insertRow (model- > rowCount ()); / / insert a line QModelIndex index = model- > index (model- > rowCount ()-1) at the end; / / get the index QString LineText = ui- > lineEdit- > text () of the last row; model- > setData (index,LineText,Qt::DisplayRole) / / set the display text ui- > listView- > setCurrentIndex (index); / / set the current line to select ui- > lineEdit- > clear ();} / / insert a row of data into ListViewvoid MainWindow::on_btnListInsert_clicked () {QModelIndex index; index= ui- > listView- > currentIndex (); / / get the currently selected row model- > insertRow (index.row ()) / insert a line QString LineText = ui- > lineEdit- > text () before the current line; model- > setData (index,LineText,Qt::DisplayRole); / / set the display text model- > setData (index,Qt::AlignRight,Qt::TextAlignmentRole); / / set the mode ui- > listView- > setCurrentIndex (index) / / set the currently selected row} / / delete the currently selected row void MainWindow::on_btnListDelete_clicked () {QModelIndex index; index = ui- > listView- > currentIndex (); / / get the ModelIndex model- > removeRow (index.row ()) of the current row / / Delete the selected line} / / clear the current list void MainWindow::on_btnListClear_clicked () {model- > removeRows (0dm model-> rowCount ());}

The running effect of the code:

If you need to export the data from the ListView data model to the plaintextEdit component, you need to get each row in the ListView through model- > stringList () and assign it to the QStringList string linked list, and then insert it into the plainTextEdit in turn through a loop, with a comma as the delimiter by default.

/ / display the text of the data model to QPlainTextEditvoid MainWindow::on_btnTextImport_clicked () {QStringList pList; pList = model- > stringList (); / / get the StringList ui- > plainTextEdit- > clear () of the data model; / / clear the text box first / / cycle append data for (int xchang0terx)

< pList.count();x++) { ui->

PlainTextEdit- > appendPlainText (pList.at (x) + QString (",");}}

The running effect of the code:

This is the end of the study on "how to use the Qt StringListModel string list mapping component". I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report