城市列表
Posted countryboy666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了城市列表相关的知识,希望对你有一定的参考价值。
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QListWidgetItem>
namespace Ui
class MainWindow;
class MainWindow : public QMainWindow
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_addbutton_clicked();
void on_deletebutton_clicked();
void on_deleteAll_clicked();
void on_showdir_clicked();
void singleclickslot(QListWidgetItem*);
void doubleclickslot(QListWidgetItem*);
private:
Ui::MainWindow *ui;
;
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
//QListWidget QListWidgetItem
#include <QDebug>
#include <QListWidgetItem>//项
#include <QStringList>
#include <QDir>
#include <QMessageBox>
#include <QFileDialog> //获得文件
#include <QDebug>
//添加图标
//双击信号接受不到
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui->setupUi(this);
//默认列表显示 修改为图标显示
ui->listWidget->setViewMode(QListView::IconMode);
//连接 enter健
connect(ui->lineEdit,SIGNAL(returnPressed()),this,SLOT(on_addbutton_clicked()));
connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(singleclickslot(QListWidgetItem*)));
connect(ui->listWidget,SIGNAL(itemDoubleClicked(QListWidgetItem*)),this,SLOT(doubleclickslot(QListWidgetItem*)));
MainWindow::~MainWindow()
delete ui;
void MainWindow::on_addbutton_clicked()
//create item
QString cityname = ui->lineEdit->text();
QListWidgetItem *item= new QListWidgetItem;
//set item
item->setText(cityname);
//add item to listwidget
ui->listWidget->addItem(item);
ui->lineEdit->clear();
void MainWindow::on_deletebutton_clicked()
//delete 获得当前行的行的项
QListWidgetItem *item=ui->listWidget->takeItem(ui->listWidget->currentRow());
if(item!=NULL)
delete item;
void MainWindow::on_deleteAll_clicked()
int count=0;
while(1)
//从第一行开始删除
QListWidgetItem *item=ui->listWidget->takeItem(count);
if(item!=NULL)
delete item;
if(ui->listWidget->count()==0)
qDebug()<<"clear ok!";
break;
void MainWindow::on_showdir_clicked()
//获得当前路径的文件
// QStringList filenames = QFileDialog::getOpenFileNames(this,"Open files",QDir::currentPath());
ui->listWidget->clear();
//获得文件夹路径
QDir dir=QDir::current();
QStringList filenames=dir.entryList();
if (filenames.size() == 0)
QMessageBox::information(this,"Error","No file select");
return;
for(int index=0;index<filenames.size();index++)
//不显示 .. .
if(filenames.at(index)=="."||filenames.at(index)=="..")
continue;
QListWidgetItem *item = new QListWidgetItem;
item->setText(filenames.at(index));
//添加到部件列表
ui->listWidget->addItem(item);
void MainWindow::singleclickslot(QListWidgetItem* item)
QMessageBox::information(this,"information","single click"+item->text());
void MainWindow::doubleclickslot(QListWidgetItem* item)
QMessageBox::information(this,"information","double click"+item->text());
以上是关于城市列表的主要内容,如果未能解决你的问题,请参考以下文章