目录
void MainWindow::on_actListIni_triggered()
{ //初始化项
????QListWidgetItem *aItem; //每一行是一个QListWidgetItem
????QIcon aIcon;
????aIcon.addFile(":/images/icons/check2.ico"); //设置ICON的图标
????bool chk=ui->chkBoxListEditable->isChecked();//是否可编辑
????ui->listWidget->clear(); //清除项
????for (int i=0; i<10; i++)
????{
????????QString str=QString::asprintf("Item %d",i);
????????aItem=new QListWidgetItem(); //新建一个项
????????aItem->setText(str); //设置文字标签
????????aItem->setIcon(aIcon);//设置图标
????????aItem->setCheckState(Qt::Checked); //设置为选中状态
????????if (chk) //可编辑, 设置flags
????????????aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
????????else//不可编辑, 设置flags
????????????aItem->setFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
????????ui->listWidget->addItem(aItem); //增加一个项
????}
}
void MainWindow::on_actListInsert_triggered()
{???//插入一个项
//????QListWidgetItem *aItem;
????QIcon aIcon;
????aIcon.addFile(":/images/icons/check2.ico"); //图标
????bool chk=ui->chkBoxListEditable->isChecked(); //是否可比那几
????QListWidgetItem* aItem=new QListWidgetItem("New Inserted Item"); //创建一个项
????aItem->setIcon(aIcon);//设置图标
????aItem->setCheckState(Qt::Checked); //设置为checked
????if (chk) //设置标记
????????aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
????else
????????aItem->setFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
????ui->listWidget->insertItem(ui->listWidget->currentRow(),aItem); //在当前行的上方插入一个项
}
void MainWindow::on_actListDelete_triggered()
{ //删除当前项
????int row=ui->listWidget->currentRow();//当前行
????QListWidgetItem* aItem=ui->listWidget->takeItem(row); //移除指定行的项,但不delete
????delete aItem; //需要手工删除对象
}
void MainWindow::on_actSelALL_triggered()
{ //项全选
????int cnt=ui->listWidget->count();//项个数
????for (int i=0; i<cnt; i++)
????{
????????QListWidgetItem *aItem=ui->listWidget->item(i);//获取一个项
????????aItem->setCheckState(Qt::Checked);//设置为选中
????}
}
void MainWindow::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{ //listWidget当前选中项发生变化
??QString str;
??if (current != NULL) //需要检测变量指针是否为空
??{
????if (previous==NULL)??//需要检测变量指针是否为空
??????str="当前:"+current->text();
????else
??????str="前一项:"+previous->text()+"; 当前项:"+current->text();
????ui->editCutItemText->setText(str);
??}
}
#include <QtGui/QApplication>
#include <QWidget>
#include <QLabel>
#include <QListWidget>
#include <QHBoxLayout>
#include <QIcon>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
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)));
QWidget *widget = new QWidget;
widget->setLayout(mainlayout);
widget->setWindowTitle(QObject::tr("QListWidget Demo"));
widget->show();
return a.exec();
}