学习了上一条博客的关于QListWidget的成员函数的用法,下面就有一个相关的简单小程序来练下手:
程序完成如下图:
Qt学习: QListWidget程序示例_ico
第一步,我们先通过ui设计师拖拽出我们想要的布局:
Qt学习: QListWidget程序示例_ico_02
下面是”c.cpp”下的代码:我将逐句讲解:

#include "c.h"

c::c(QWidget *parent)
: QMainWindow(parent)
{
//应用ui设计师布局.
ui.setupUi(this);
//把两个按钮设置为选中.
ui.listButton->setChecked(true);
ui.upButton->setChecked(true);
//列表部件采用排序.默认采用AAA-ZZZ排序.
ui.listWidget->setSortingEnabled(true);
//往列表部件中加入项组.一共3个.
ui.listWidget->addItems(QStringList()<<QString::fromLocal8Bit("北京")<<QString::fromLocal8Bit("上海")<<QString::fromLocal8Bit("杭州"));
//设置标题为"QListWidget".
this->setWindowTitle(QString::fromLocal8Bit("QListWidget"));
//对于前面的三个项设置图标.可根据实际情况使用.
ui.listWidget->item(0)->setIcon(QIcon("Icons/1.jpg"));
ui.listWidget->item(1)->setIcon(QIcon("Icons/2.jpg"));
ui.listWidget->item(2)->setIcon(QIcon("Icons/3.png"));
//设置项的选择情况.
ui.listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);

//连接信号与槽.
connect(ui.addButton, SIGNAL(clicked()), this, SLOT(addItemSlot()));
connect(ui.delButton, SIGNAL(clicked()), this, SLOT(delItemSlot()));
connect(ui.delAllButton, SIGNAL(clicked()), this, SLOT(delAllItemSlot()));
//QLineEdit的信号:void returnPressed()的意思是:当焦点在lineEdit内时,敲击enter键,发出信号.
connect(ui.lineEdit, SIGNAL(returnPressed()), this, SLOT(addItemSlot()));
connect(ui.listButton, SIGNAL(clicked()), this, SLOT(changeListWidgetViewModeSlot()));
connect(ui.iconButton, SIGNAL(clicked()), this, SLOT(changeListWidgetViewModeSlot()));
connect(ui.upButton, SIGNAL(clicked()), this, SLOT(changeSortModeSlot()));
connect(ui.downButton, SIGNAL(clicked()), this, SLOT(changeSortModeSlot()));
}

c::~c()
{

}
void c::addItemSlot()
{
//创建一个QListWidget的项.
QListWidgetItem *ite = new QListWidgetItem;
//从lineEdit行编辑框中获取输入的文字.
QString iteText = ui.lineEdit->text();
//一定要检查是否空,否则也会输入进去!!!
if (iteText.isEmpty())
return;
//然后设置项的文本.
ite->setText(iteText);
//最后把这个项加入到列表部件中去.
ui.listWidget->addItem(ite);
//清空行编辑器,坐下善后工作.
ui.lineEdit->clear();
}
void c::delItemSlot()
{
//直接使用QListWidget部件的删除函数,通过currentItem()成员函数来获取当前的选择项.
ui.listWidget->removeItemWidget(ui.listWidget->currentItem());
}
void c::delAllItemSlot()
{
/*//通过循环一个个删除QListWidget内的项.
int count = ui.listWidget->count();
for (int i = count - 1; i >= 0; --i)
{
QListWidgetItem* ite = ui.listWidget->takeItem(i);
if (ite != 0)
delete ite;
}*/
//也可以直接:
ui.listWidget->clear();
}
void c::changeListWidgetViewModeSlot()
{
//判断对于列表显示模式的选择情况.如果选择了列表模式,则设置列表模式.
if (ui.listButton->isChecked())
{
ui.listWidget->setViewMode(QListView::ListMode);
}
//否则选择图标模式.
else
{
ui.listWidget->setViewMode(QListView::IconMode);
}
}
void c::changeSortModeSlot()
{
//判断循环模式的选择情况.
if (ui.upButton->isChecked())
{
ui.listWidget->sortItems(Qt::AscendingOrder);
}
else
{
ui.listWidget->sortItems(Qt::DescendingOrder);
}
}

最后贴下”c.h”下的代码:

#ifndef C_H
#define C_H

#include <QtWidgets/QMainWindow>
#include "ui_c.h"
#include <QListWidget>
#include <QListWidgetItem>
#include <QMessageBox>

class c : public QMainWindow
{
Q_OBJECT

public:
c(QWidget *parent = 0);
~c();

private slots:
void addItemSlot();
void delItemSlot();
void delAllItemSlot();
void changeListWidgetViewModeSlot();
void changeSortModeSlot();
private:
Ui::cClass ui;
};

#endif // C_H

main.cpp下的代码:

#include "c.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
c w;
w.show();
return a.exec();
}