码迷,mamicode.com
首页 > 其他好文 > 详细

城市列表

时间:2019-06-22 10:41:13      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:include   return   dial   ==   图标   art   continue   城市列表   win   

#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());
}



城市列表

标签:include   return   dial   ==   图标   art   continue   城市列表   win   

原文地址:https://www.cnblogs.com/countryboy666/p/11067831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!