Table of contents
In the Qt framework, QListWidget is a widget class used to display list views. It provides a convenient way to display and manage project lists. QListWidget can display text, images and other customized items and allow users to select and interact with them.
QListWidget can be used to create functions similar to file browsers, playlists, menus, etc. It can display items in list form and supports multiple operations such as adding, deleting, moving, selecting and editing items.
The following are some of the main features and usage of QListWidget:
Display a list of items: QListWidget can display a list of items, and each item can contain text, images, or other custom widgets.
Selection operations: The user can select items in the list using the mouse or keyboard. You can set the selection mode such as single selection, multiple selection, or extended selection.
Interactive operations: QListWidget supports common interactive operations, such as double-click, right-click and drag-and-drop. You can capture these interaction events and take appropriate actions as needed.
Editing operations: QListWidget allows users to edit the text content of items. Edit mode can be enabled manually or started automatically so that the user can edit the project.
Signal and slot mechanism: QListWidget provides notification of various events and operations through the signal and slot mechanism. You can connect to these signals and perform custom actions as needed.
In summary, QListWidget is a flexible and easy-to-use Qt widget for displaying and managing lists of items in your application. It provides a wealth of functions and operations to meet various list display and interaction needs.
QLabel *label = new QLabel;
label->setFixedWidth(100);
QListWidget *listWidget = new QListWidget;
listWidget->addItem(new QListWidgetItem(QIcon(QObject::tr("images/china.png")),
QObject::tr("China")));
listWidget->addItem(new QListWidgetItem(QIcon(QObject::tr("images/hk.png")),
QObject::tr("Hong Kong")));
listWidget->addItem(new QListWidgetItem(QIcon(QObject::tr("images/macau.png")),
QObject::tr("Macau")));
QHBoxLayout *mainlayout = new QHBoxLayout;
mainlayout->addWidget(listWidget);
mainlayout->addWidget(label);
QObject::connect(listWidget, SIGNAL(currentTextChanged(QString)), label,
SLOT(setText(QString)));
Other functions use:
1. Add objects
QString listString = ui->lEditUserName->text() + "\n"+ui->lEditChipID->text();
QListWidgetItem *item = new QListWidgetItem(QIcon(":/images/images/user.png"),
listString);
ui-> listWidget->addItem(item);
2. Set the spacing
ui->listWidget->setIconSize(QSize(50,50));
3. Get content
QString str = ui->listWidget->item(currentRow)->text(); //获取当前行的内容
QStringList listStr = str.split('\n');
currentUserName = listStr.at(0);
currentChipID = listStr.at(1);
ui->lEditUserName->setText(currentUserName);
ui->lEditChipID->setText(currentChipID);
4. Delete objects
QListWidgetItem* item = ui->listWidget->takeItem(currentWidgetRow); //删除当前
行
delete item;
ui->listWidget->update(); //更新显示
5.Change object content
if(ui->listWidget->currentItem()!=Q_NULLPTR){
QString listString = newUserName + "\n"+currentChipID;
ui->listWidget->currentItem()->setText(listString);
}
Finish! ! !