- 手写一个界面
- 新建项
- 删除项
- 删除所有项
- 获取当前路径内所有文件名,
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
private:
Ui::Widget *ui;
QListWidget *listWidget;
QPushButton *addButton;
QPushButton * deleteButton;
QHBoxLayout *h1;
QVBoxLayout *v1;
QLineEdit *lineEdit;
QPushButton *deleteAllButton;
QPushButton *getFileButton;
void setUi();
private slots:
void addItemSlot();
void deleteItemSlot();
void deleteAllSlot();
void getFileSlot();
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setUi();
QObject::connect(lineEdit,SIGNAL(returnPressed()),this,SLOT(addItemSlot()));
QObject::connect(deleteButton,SIGNAL(clicked()),this,SLOT(deleteItemSlot()));
QObject::connect(deleteAllButton,SIGNAL(clicked()),this,SLOT(deleteAllSlot()));
QObject::connect(getFileButton,SIGNAL(clicked()),this,SLOT(getFileSlot()));
}
void Widget::setUi()
{
listWidget =new QListWidget;
addButton =new QPushButton;
addButton->setText("add");
deleteButton=new QPushButton;
deleteButton->setText("delete");
h1 =new QHBoxLayout;
v1 =new QVBoxLayout;
lineEdit =new QLineEdit;
h1->addWidget(lineEdit);
h1->addWidget(addButton);
h1->addWidget(deleteButton);
deleteAllButton=new QPushButton;
deleteAllButton->setText("delete all items");
getFileButton=new QPushButton;
getFileButton->setText("get file context");
v1->addWidget(listWidget);
v1->addLayout(h1);
v1->addWidget(deleteAllButton);
v1->addWidget(getFileButton);
this->setLayout(v1);
}
void Widget::getFileSlot()
{
qDebug()<<"get file name";
QDir dir=QDir::current();
QStringList fileNames=dir.entryList();
listWidget->clear();
for(int i=0;i<fileNames.size();i++)
{
QListWidgetItem *item=new QListWidgetItem();
item->setText(fileNames.at(i));
listWidget->addItem(item);
}
}
void Widget::deleteAllSlot()
{
while(listWidget->count())
{
QListWidgetItem *item=listWidget->takeItem(0);
delete item;
}
}
void Widget::addItemSlot()
{
QString name=lineEdit->text();
QListWidgetItem *item=new QListWidgetItem;
item->setText(name);
listWidget->addItem(item);
lineEdit->clear();
}
void Widget::deleteItemSlot()
{
QListWidgetItem * item=listWidget->takeItem(listWidget->currentRow());
delete item;
}
Widget::~Widget()
{
delete listWidget;
delete addButton;
delete deleteButton;
delete deleteAllButton;
delete getFileButton;
delete h1;
delete v1;
delete lineEdit;
delete ui;
}
listWidget->addItem(QListWidgetItem* item)
添加listWidget子项目;
item=listWidget->takeItem(listWidget->currentRow())
获取当前选中项目的指针
listWidget->count()
获得当前listWidget内的文件数目;
QDir dir=QDir::current()
QStirngList fileNames=dir.entryList()
获得当前路径内所有文件名的List列表;
QString name=fileNames.at(int index)
返回list中指定位置元素的内容,为QString 类型;