逆枫゛

Qt学习群:1149411109 群文件提供博客源码,定期答疑、更新学习资料。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1,简介

 

刚与几位朋友合伙创业那会,因为我在公司负责软件,销售问我有没有什么软件可以管理他的客户信息,不用太复杂。

我了解了一下具体需求,就是可以把客户按照地域、类型等进行分类,然后存储一些基本信息,比如名称、地址、联系人信息、采购情况等,可以增删、搜索。

我说现成的有些软件太臃肿也不一定适合,我给你写一个吧

他问我多久,太麻烦就算了,我说没事一天应该可以写出来

哎这牛皮吹的,说只要一天,搞不完不好意思啊,我硬是一个人加班加点到凌晨3点给弄出来了

 

2,效果

 

 

窗口高度刚好把地域全部覆盖,省得拖滚动条;

三种过滤方式:点击tree里的节点按地域过滤、在筛选的combobox里按单位类型过滤、在搜索里按关键字过滤;

数据使用sqlite存储;

点按钮增加、删除记录;

双击一行进行修改;

 

3,一些设计思路

 

左侧的地域树,因为都是固定信息,直接用一个QTreeWidget在UI设计器把内容填好了。

中间使用QTableView显示记录。

数据使用程序exe目录下的crm.db文件,sqlite,没有就创建。

数据过滤:开始准备用代理model,后来觉得直接根据过滤条件刷新model就完了,数据量也不大不考虑效率问题。

其中关键字过滤需要把所有列的内容都匹配下。

 

4,代码

 

主要就是一个主窗口+添加/修改记录对话框。

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
#include <QTreeWidget>
#include <QVector>
#include "dlgitem.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void InitMenu();

    void InitModel();
    void InitTable();
    void AddItem(Customer* item);
    void ClearData();

    void OpenDb(QString fileName);
    void SaveToDb(QString fileName);
    void InsertToDb(Customer* item);
    void DeleteFromDb(QString name);

private slots:
    void onMenuOpen(bool checked);
    void onMenuSave(bool checked);

    void on_btnSearch_clicked();        //搜索按钮,关键字过滤
    void on_btnAdd_clicked();           //添加
    void on_btnDelete_clicked();        //删除

    void slotAddItem(Customer* item);   //DlgItem里添加记录后需要不关闭对话框,因此发个信号
    void on_comboBox_currentTextChanged(const QString &arg1);   //按单位类别过滤
    void on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); //改变选择区域
    void on_tableView_doubleClicked(const QModelIndex &index);  //数据一行双击修改

private:
    QString Type2String(int type);
    QString Area2String(int area);
    QString Province2String(int province);
    Customer* FindItem(QString name);
    void DeleteItem(QString name);

private:
    Ui::MainWindow *ui;
    QString mDefaultDB;         //db文件路径
    QStandardItemModel* mModel; //tableview的model
    QVector<Customer*> mItems;  //所有客户数据

    QStringList mAllType;       //所有单位类型
    QStringList mAllArea;       //所有区域
    QStringList mAllProvince;   //所有省份

    QString mFilterType;        //单位类型过滤
    QString mFilterArea;        //地域过滤
    QString mFilterProvince;    //省份过滤
    QString mFilterKey;         //关键字过滤
};

#endif // MAINWINDOW_H

MainWindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QListView>
#include <QFileDialog>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QDebug>
#include <QSortFilterProxyModel>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/myapp.ico"));

    mDefaultDB = QCoreApplication::applicationDirPath() + "\\crm.db";
    mAllType<<"企业"<<"高校"<<"研究所"<<"代理"<<"外"<<"政府机构"<<"部队"<<"第三方";
    mAllArea<<"华东"<<"华南"<<"华中"<<"华北"<<"其他"<<"外";
    mAllProvince<<"山东"<<"浙江"<<"江苏"<<"安徽"<<"上海"<<"福建"
                <<"广东"<<"广西"<<"海南"
                <<"湖北"<<"湖南"<<"河南"<<"江西"
                <<"北京"<<"天津"<<"河北"<<"山西"<<"内蒙"
                <<"宁夏"<<"新疆"<<"青海"<<"陕西"<<"甘肃"<<"四川"<<"云南"<<"贵州"<<"西藏"<<"重庆"<<"辽宁"<<"吉林"<<"黑龙江";
    mFilterType = "";
    mFilterArea = "";
    mFilterProvince = "";
    mFilterKey = "";

    InitMenu();

    OpenDb(mDefaultDB);
    InitTable();

    ui->treeWidget->expandAll();
    ui->treeWidget->setCurrentItem(ui->treeWidget->itemAt(0,0));

    ui->comboBox->setView(new QListView());
}

MainWindow::~MainWindow()
{
    ClearData();
    delete ui;
}

void MainWindow::InitMenu()
{
    QMenu *file = menuBar()->addMenu("&文件");
    file->addAction("打开...",this,SLOT(onMenuOpen(bool)));
    file->addAction("保存",this,SLOT(onMenuSave(bool)));
}

void MainWindow::InitModel()
{
    mModel->removeRows(0,mModel->rowCount());
    for(int i=0;i<mItems.size();i++)
    {
        Customer* p = mItems[i];
        QString sArea = Area2String(p->area);
        QString sProvince = Province2String(p->province);
        QString sType = Type2String(p->type);

        //区域过滤
        if(!mFilterArea.isEmpty())
        {
           if(mFilterArea != sArea)
           {
               continue;
           }
        }
        //省份过滤
        if(!mFilterProvince.isEmpty())
        {
           if(mFilterProvince != sProvince)
           {
               continue;
           }
        }
        //单位类别过滤
        if(!mFilterType.isEmpty())
        {
           if(mFilterType != sType)
           {
               continue;
           }
        }
        //关键字过滤
        if(!mFilterKey.isEmpty())
        {
            QStringList strs;
            strs<<p->name<<sType<<sArea<<sProvince<<p->city<<p->site<<p->contact<<p->buy<<p->research<<p->remark;
            bool bFind = false;
            foreach (QString s, strs)
            {
                if(s.indexOf(mFilterKey) != -1)
                {
                    bFind = true;
                    break;
                }
            }
            if(!bFind)
            {
                continue;
            }
        }

        QList<QStandardItem*> items;
        QStandardItem* item1 = new QStandardItem(p->name);
        QStandardItem* item2 = new QStandardItem(Type2String(p->type));
        QStandardItem* item3 = new QStandardItem(Area2String(p->area));
        QStandardItem* item4 = new QStandardItem(Province2String(p->province) );
        QStandardItem* item5 = new QStandardItem(p->city);
        QStandardItem* item6 = new QStandardItem(p->address);
        QStandardItem* item7 = new QStandardItem(p->site);
        QStandardItem* item8 = new QStandardItem(p->research);
        QStandardItem* item9 = new QStandardItem(p->remark);
        items.append(item1);
        items.append(item2);
        items.append(item3);
        items.append(item4);
        items.append(item5);
        items.append(item6);
        items.append(item7);
        items.append(item8);
        items.append(item9);
        mModel->appendRow(items);
    }
}

void MainWindow::InitTable()
{
    QTableView* t = ui->tableView;
    t->setEditTriggers(QTreeView::NoEditTriggers);			//不能编辑
    t->setSelectionBehavior(QTreeView::SelectRows);			//一次选中整行
    t->setAlternatingRowColors(true);

    QString strTreeStyle = "QTableView::item {height: 25px}";
    t->setStyleSheet(strTreeStyle);

    QStringList headers;
    headers<< QStringLiteral("单位名称")<<QStringLiteral("单位类型")
            << QStringLiteral("区域")<<QStringLiteral("省份")
            << QStringLiteral("城市")<<QStringLiteral("地址")
            << QStringLiteral("网址")<<QStringLiteral("研究方向")
            << QStringLiteral("备注");
    mModel = new QStandardItemModel(t);
    mModel->setHorizontalHeaderLabels( headers );

    InitModel();
    t->setModel(mModel);
    for(int i=0;i<headers.size();i++)
    {
        if(i==0)
        {
            t->horizontalHeader()->resizeSection(i,200);
        }
        else if(i==1)
        {
            t->horizontalHeader()->resizeSection(i,80);
        }
        else if(i>=2 && i<=4)
        {
            t->horizontalHeader()->resizeSection(i,60);
        }
        else
        {
            t->horizontalHeader()->resizeSection(i,150);
        }
    }

    QModelIndex rootIndex = t->rootIndex();
    QModelIndex selIndex = mModel->index(0,0,rootIndex);
    t->setCurrentIndex(selIndex);
//    t->expandAll();
}

void MainWindow::AddItem(Customer *item)
{
    mItems.push_back(item);
}

void MainWindow::ClearData()
{
    for(int i=0;i<mItems.size();i++)
    {
        Customer* p = mItems[i];
        if(p)
        {
            delete p;
            p = NULL;
        }
    }
    mItems.clear();
}

void MainWindow::OpenDb(QString fileName)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(fileName);
    if(db.open())
    {
        qDebug()<<"Database Opened";

        mDefaultDB = fileName;

        QSqlQuery sql_query;
        QString select_all_sql = "select * from Customer";
        //查询所有记录
        sql_query.prepare(select_all_sql);
        if(!sql_query.exec())
        {
            qDebug()<<sql_query.lastError();
        }
        else
        {
            while(sql_query.next())
            {
                Customer* p = new Customer;
                p->name = sql_query.value(0).toString();
                p->type = sql_query.value(1).toInt();
                p->area = sql_query.value(2).toInt();
                p->province = sql_query.value(3).toInt();
                p->city = sql_query.value(4).toString();
                p->address = sql_query.value(5).toString();
                p->site = sql_query.value(6).toString();
                p->contact = sql_query.value(7).toString();
                p->buy = sql_query.value(8).toString();
                p->research = sql_query.value(9).toString();
                p->remark = sql_query.value(10).toString();
                mItems.push_back(p);
            }
        }
    }
    db.close();
}

void MainWindow::SaveToDb(QString fileName)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(fileName);
    if(db.open())
    {
        qDebug()<<"Database Opened";

        QSqlQuery sql_query;
        QString create_sql = "create table if not exists Customer (name varchar(100) primary key, \
                                                    type int ,\
                                                    area int,\
                                                    province int,\
                                                    city varchar(20),\
                                                    address varchar(100),\
                                                    site varchar(200),\
                                                    contact varchar(500),\
                                                    buy varchar(200),\
                                                    research varchar(100),\
                                                    remark varchar(200))"; //创建数据表
//        QString insert_sql = "insert into Customer values(\"公司1\",1,1,1,\"武汉\",\"武汉洪山区XXX\",\"www.xxx.com\",\"aaaaa\",\"bbbbb\",\"ccccc\",\"ddddd\")";    //插入数据
        QString insert_sql = "insert into Customer values(\"%1\",%2,%3,%4,\"%5\",\"%6\",\"%7\",\"%8\",\"%9\",\"%10\",\"%11\")";    //插入数据
        QString select_all_sql = "select * from Customer";

        sql_query.prepare(create_sql); //创建表
        if(!sql_query.exec()) //查看创建表是否成功
        {
            qDebug()<<QObject::tr("Table Create failed");
            qDebug()<<sql_query.lastError();
        }
        else
        {
            qDebug()<< "Table Created" ;

            for(int i=0;i<mItems.size();i++)
            {
                Customer* p = mItems[i];
                QString sql = insert_sql;
                sql.replace("%10",p->research);
                sql.replace("%11",p->remark);
                sql = sql.arg(p->name).arg(p->type).arg(p->area).arg(p->province).arg(p->city).arg(p->address).arg(p->site).arg(p->contact).arg(p->buy);
                sql_query.prepare(sql);
                if(!sql_query.exec())
                {
                    qDebug()<<sql_query.lastError();
                }
                else
                {
                    qDebug()<<"插入记录成功";
                }
            }
        }
    }
    db.close();
}

void MainWindow::InsertToDb(Customer *p)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(mDefaultDB);
    if(db.open())
    {
        qDebug()<<"Database Opened";

        QSqlQuery sql_query;
        QString create_sql = "create table if not exists Customer (name varchar(100) primary key, \
                                                    type int ,\
                                                    area int,\
                                                    province int,\
                                                    city varchar(20),\
                                                    address varchar(100),\
                                                    site varchar(200),\
                                                    contact varchar(500),\
                                                    buy varchar(200),\
                                                    research varchar(100),\
                                                    remark varchar(200))"; //创建数据表
        sql_query.prepare(create_sql);
        sql_query.exec();                   //创建表

        QString sql = "insert into Customer values(\"%1\",%2,%3,%4,\"%5\",\"%6\",\"%7\",\"%8\",\"%9\",\"%10\",\"%11\")";    //插入数据
        sql.replace("%10",p->research);
        sql.replace("%11",p->remark);
        sql = sql.arg(p->name).arg(p->type).arg(p->area).arg(p->province).arg(p->city).arg(p->address).arg(p->site).arg(p->contact).arg(p->buy);
        sql_query.prepare(sql);
        if(!sql_query.exec())
        {
            qDebug()<<sql_query.lastError();
        }
        else
        {
            qDebug()<<"插入记录成功";
        }
    }
    db.close();
}

void MainWindow::DeleteFromDb(QString name)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(mDefaultDB);
    if(db.open())
    {
        qDebug()<<"Database Opened";

        QSqlQuery sql_query;
        QString sql = QString("delete from Customer where name='%1'").arg(name);
        sql_query.prepare(sql);
        if(!sql_query.exec())
        {
            qDebug()<<sql_query.lastError();
        }
        else
        {
            qDebug()<<"删除记录成功";
        }
    }
    db.close();
}

void MainWindow::onMenuOpen(bool checked)
{
    QString fileName = QFileDialog::getOpenFileName(this,"打开文件","","Database Files(*.db)");
    if (fileName.isNull())
    {
        return;
    }
    if(fileName.indexOf(".db") != -1)
    {
        ClearData();
        OpenDb(fileName);
        InitTable();
    }
}

void MainWindow::onMenuSave(bool checked)
{
    QString fileName = QFileDialog::getSaveFileName(this,"保存到文件","","Database Files(*.db)");
    if (fileName.isNull())
    {
        return;
    }
    if(QFile::exists(fileName))
    {
        QFile::remove(fileName);
    }
    SaveToDb(fileName);
}

void MainWindow::on_btnSearch_clicked()
{
    QString s = ui->lineEdit->text();
    mFilterKey = s;
    InitModel();
}

void MainWindow::on_btnAdd_clicked()
{
    DlgItem dlg(NULL);
    connect(&dlg,&DlgItem::sigAddItem,this,MainWindow::slotAddItem);
    dlg.exec();
}

void MainWindow::on_btnDelete_clicked()
{
//    int row = ui->treeView->currentIndex().row();
    int row = ui->tableView->currentIndex().row();
    QString name = mModel->item(row,0)->text();
    DeleteItem(name);

    mModel->removeRow(row);

    DeleteFromDb(name);
}

void MainWindow::slotAddItem(Customer *p)
{
    mItems.push_back(p);

    QList<QStandardItem*> items;
    QStandardItem* item1 = new QStandardItem(p->name);
    QStandardItem* item2 = new QStandardItem(Type2String(p->type));
    QStandardItem* item3 = new QStandardItem(Area2String(p->area));
    QStandardItem* item4 = new QStandardItem(Province2String(p->province) );
    QStandardItem* item5 = new QStandardItem(p->city);
    QStandardItem* item6 = new QStandardItem(p->address);
    QStandardItem* item7 = new QStandardItem(p->site);
    QStandardItem* item8 = new QStandardItem(p->research);
    QStandardItem* item9 = new QStandardItem(p->remark);
    items.append(item1);
    items.append(item2);
    items.append(item3);
    items.append(item4);
    items.append(item5);
    items.append(item6);
    items.append(item7);
    items.append(item8);
    items.append(item9);
    mModel->appendRow(items);

    InsertToDb(p);
}

QString MainWindow::Type2String(int n)
{
    return mAllType[n];
}

QString MainWindow::Area2String(int n)
{
    return mAllArea[n];
}

QString MainWindow::Province2String(int n)
{
    if(n>=0 && n<mAllProvince.size() )
    {
        return mAllProvince[n];
    }
    return "";
}

Customer *MainWindow::FindItem(QString name)
{
    for(int i=0;i<mItems.size();i++)
    {
        if(mItems[i]->name == name)
        {
            return mItems[i];
        }
    }
    return NULL;
}

void MainWindow::DeleteItem(QString name)
{
    for(int i=0;i<mItems.size();i++)
    {
        if(mItems[i]->name == name)
        {
            mItems.removeAt(i);
            return;
        }
    }
}

void MainWindow::on_comboBox_currentTextChanged(const QString &text)
{
    if(text == "所有类型")
    {
        mFilterType = "";
    }
    else
    {
        mFilterType = text;
    }
    InitModel();
}

void MainWindow::on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    QString s = current->text(0);
    if(s == "全部")
    {
        mFilterArea = "";
        mFilterProvince = "";
    }
    else if(mAllArea.contains(s))
    {
        //区域
        mFilterArea = s;
        mFilterProvince = "";
    }
    else if(mAllProvince.contains(s))
    {
        //省份
        mFilterArea = "";
        mFilterProvince = s;
    }
    InitModel();
}

void MainWindow::on_tableView_doubleClicked(const QModelIndex &index)
{
    QString name = mModel->item(index.row(),0)->text();
    Customer *p = FindItem(name);
    if(p)
    {
        DlgItem dlg(p);
        if(QDialog::Accepted == dlg.exec())
        {
            mModel->item(index.row(),0)->setText(p->name);
            mModel->item(index.row(),1)->setText(Type2String(p->type));
            mModel->item(index.row(),2)->setText(Area2String(p->area));
            mModel->item(index.row(),3)->setText(Province2String(p->province));
            mModel->item(index.row(),4)->setText(p->city);
            mModel->item(index.row(),5)->setText(p->address);
            mModel->item(index.row(),6)->setText(p->site);
            mModel->item(index.row(),7)->setText(p->research);
            mModel->item(index.row(),8)->setText(p->remark);

            DeleteFromDb(name);
            InsertToDb(p);
        }
    }
}

 

添加/修改对话框DlgItem:

 

#ifndef DLGITEM_H
#define DLGITEM_H

#include <QDialog>

namespace Ui {
class DlgItem;
}

typedef struct _Customer{
    QString name;
    int type;
    int area;
    int province;
    QString city;
    QString address;
    QString site;
    QString contact;
    QString buy;
    QString research;
    QString remark;
}Customer,*PCustomer;

class DlgItem : public QDialog
{
    Q_OBJECT

public:
    explicit DlgItem(Customer* item,QWidget *parent = 0);
    ~DlgItem();

signals:
    void sigAddItem(Customer*);

private slots:
    void on_btnAdd_clicked();
    void on_btnCancel_clicked();
    void on_combo2_currentTextChanged(const QString &arg1);
    void on_edit1_textChanged(const QString &arg1);

private:
    int String2Province(QString s);
    QString Province2String(int province);

private:
    Ui::DlgItem *ui;
    Customer* mItem;
};

#endif // DLGITEM_H
#include "dlgitem.h"
#include "ui_dlgitem.h"

DlgItem::DlgItem(Customer* item,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgItem)
{
    ui->setupUi(this);

    mItem = item;

    QFont font( "Microsoft YaHei", 10, 75);
    ui->labelTip->setFont(font);
    ui->labelTip->setStyleSheet("QLabel{color:red;}");
    ui->tableWidget->setRowCount(20);
    for(int i=0;i<20;i++)
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(""));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(""));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(""));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(""));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(""));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(""));
    }

    on_combo2_currentTextChanged("华东");
    if(mItem)
    {
        //修改
        ui->btnAdd->setText("修改");

        ui->edit1->setText(mItem->name);
        ui->combo1->setCurrentIndex(mItem->type);
        ui->combo2->setCurrentIndex(mItem->area);
        on_combo2_currentTextChanged(ui->combo2->currentText());
        ui->combo3->setCurrentText(Province2String(mItem->province));
        ui->edit2->setText(mItem->city);
        ui->edit3->setText(mItem->address);
        ui->edit4->setText(mItem->site);
        ui->textEdit->setText(mItem->buy);
        ui->textEdit2->setText(mItem->research);
        ui->textEdit3->setText(mItem->remark);

        QStringList all = mItem->contact.split(";");
        for(int i=0;i<all.size();i++)
        {
            QStringList row = all[i].split(",");
            if(row.size() >= 6)
            {
                bool bRed = !row[5].isEmpty();
                ui->tableWidget->item(i,0)->setText(row[0]);
                ui->tableWidget->item(i,1)->setText(row[1]);
                ui->tableWidget->item(i,2)->setText(row[2]);
                ui->tableWidget->item(i,3)->setText(row[3]);
                ui->tableWidget->item(i,4)->setText(row[4]);
                ui->tableWidget->item(i,5)->setText(row[5]);
                if(!row[5].isEmpty())
                {
                    ui->tableWidget->item(i,0)->setBackground(Qt::red);
                    ui->tableWidget->item(i,1)->setBackground(Qt::red);
                    ui->tableWidget->item(i,2)->setBackground(Qt::red);
                    ui->tableWidget->item(i,3)->setBackground(Qt::red);
                    ui->tableWidget->item(i,4)->setBackground(Qt::red);
                    ui->tableWidget->item(i,5)->setBackground(Qt::red);
                }
            }
        }
    }
}

DlgItem::~DlgItem()
{
    delete ui;
}

void DlgItem::on_btnAdd_clicked()
{
    if(ui->edit1->text().isEmpty())
    {
        ui->labelTip->setText("单位名称不能为空!");
        return;
    }
    Customer* p = new Customer;
    p->name = ui->edit1->text();
    p->type = ui->combo1->currentIndex();
    p->area = ui->combo2->currentIndex();
    p->province = String2Province(ui->combo3->currentText());
    p->city = ui->edit2->text();
    p->address = ui->edit3->text();
    p->site = ui->edit4->text();
    p->buy = ui->textEdit->toPlainText();
    p->research = ui->textEdit2->toPlainText();
    p->remark = ui->textEdit3->toPlainText();

    QString sAll = "";
    QString s = "";
    int columns = ui->tableWidget->columnCount();
    for(int i=0;i<ui->tableWidget->rowCount();i++)
    {
        if(ui->tableWidget->item(i,0)->text().isEmpty())
        {
            continue;
        }
        for(int j=0;j<columns;j++)
        {
            //,和;是用来分隔的标记,禁止用户输入这2个符号,如果不小心输了,程序也替换成空格
            s = ui->tableWidget->item(i,j)->text();
            s.replace(","," ");
            s.replace(";"," ");
            sAll += s;
            if(j == columns-1)
            {
                sAll += ";";
            }
            else
            {
                sAll += ",";
            }
        }
    }
    p->contact = sAll;

    if(mItem)
    {
        //修改
        mItem->name = p->name;
        mItem->type = p->type;
        mItem->area = p->area;
        mItem->province = p->province;
        mItem->city = p->city;
        mItem->address = p->address;
        mItem->site = p->site;
        mItem->contact = p->contact;
        mItem->buy = p->buy;
        mItem->research = p->research;
        mItem->remark = p->remark;
        accept();
    }
    else
    {
        //新增
        emit sigAddItem(p);

        ui->edit1->setText("");
        ui->edit2->setText("");
        ui->edit3->setText("");
        ui->edit4->setText("");
        ui->textEdit->setText("");
        ui->textEdit2->setText("");
        ui->textEdit3->setText("");
        for(int i=0;i<20;i++)
        {
            ui->tableWidget->item(i,0)->setText("");
            ui->tableWidget->item(i,1)->setText("");
            ui->tableWidget->item(i,2)->setText("");
            ui->tableWidget->item(i,3)->setText("");
            ui->tableWidget->item(i,4)->setText("");
            ui->tableWidget->item(i,5)->setText("");
        }
        ui->edit1->setFocus();
        ui->labelTip->setText("增加成功!");
    }

}

void DlgItem::on_btnCancel_clicked()
{
    reject();
}

int DlgItem::String2Province(QString s)
{
    QStringList AllProvince;
    AllProvince<<"山东"<<"浙江"<<"江苏"<<"安徽"<<"上海"<<"福建"
                <<"广东"<<"广西"<<"海南"
                <<"湖北"<<"湖南"<<"河南"<<"江西"
                <<"北京"<<"天津"<<"河北"<<"山西"<<"内蒙"
                <<"宁夏"<<"新疆"<<"青海"<<"陕西"<<"甘肃"<<"四川"<<"云南"<<"贵州"<<"西藏"<<"重庆"<<"辽宁"<<"吉林"<<"黑龙江";
    return AllProvince.indexOf(s);
}

QString DlgItem::Province2String(int n)
{
    QStringList AllProvince;
    AllProvince<<"山东"<<"浙江"<<"江苏"<<"安徽"<<"上海"<<"福建"
                <<"广东"<<"广西"<<"海南"
                <<"湖北"<<"湖南"<<"河南"<<"江西"
                <<"北京"<<"天津"<<"河北"<<"山西"<<"内蒙"
                <<"宁夏"<<"新疆"<<"青海"<<"陕西"<<"甘肃"<<"四川"<<"云南"<<"贵州"<<"西藏"<<"重庆"<<"辽宁"<<"吉林"<<"黑龙江";
    if(n>=0 && n<AllProvince.size())
    {
        return AllProvince[n];
    }
    return "";
}

void DlgItem::on_combo2_currentTextChanged(const QString &text)
{
    ui->combo3->clear();
    if(text == "华东")
    {
        ui->combo3->addItems(QStringList()<<"山东"<<"浙江"<<"江苏"<<"安徽"<<"上海"<<"福建");
    }
    else if(text == "华南")
    {
        ui->combo3->addItems(QStringList()<<"广东"<<"广西"<<"海南");
    }
    else if(text == "华中")
    {
        ui->combo3->addItems(QStringList()<<"湖北"<<"湖南"<<"河南"<<"江西");
    }
    else if(text == "华北")
    {
        ui->combo3->addItems(QStringList()<<"北京"<<"天津"<<"河北"<<"山西"<<"内蒙");
    }
    else if(text == "其他")
    {
        ui->combo3->addItems(QStringList()<<"宁夏"<<"新疆"<<"青海"<<"陕西"<<"甘肃"<<"四川"<<"云南"<<"贵州"<<"西藏"<<"重庆"<<"辽宁"<<"吉林"<<"黑龙江");
    }
}

void DlgItem::on_edit1_textChanged(const QString &arg1)
{
    ui->labelTip->setText("");
}

 

5,源码下载

 

 

源码已上传至群文件,可在学习群免费下载!

群号码:1149411109

群名称:Qt实战派学习群

 

 

posted on 2018-04-21 18:05  逆枫゛  阅读(390)  评论(0编辑  收藏  举报