QT学习之路四(信号与槽----通讯录的再次改版)

   学了好几天的qt,今天又学了信号与槽函数,心中总有个想法-----把之前的通讯录用qt改一下。 

   信号与槽:都是函数,比如单机窗口上的一个按钮想要弹出一个对话框,那么就可以将这个按钮的单击信号和定义的槽关联起来,在这个槽中可以创建一个对话框并且显示,这样单击这个按钮就会发射信号,进而执行槽来显示一个对话框。

话不多说,还是先上代码吧:

/***********addr.h************************/
#ifndef ADDR_H
#define ADDR_H


#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <QString>

using namespace std;

class Addr
{
public:
    int m_ID;
    QString m_name;
    QString m_tel;
    QString m_address;
    QString m_company_phone;
};

#endif // ADDR_H

/************address_book.h*********************/
#ifndef ADDRESS_BOOK_H
#define ADDRESS_BOOK_H

#include <QWidget>
#include <QTextCodec>
#include <QString>
#include <QInputDialog>
#include <QLineEdit>
#include <qDebug>
#include <QVector>
#include "addr.h"

namespace Ui {
class address_book;
}

class address_book : public QWidget
{
    Q_OBJECT
    
public:
    explicit address_book(QWidget *parent = 0);
    ~address_book();
public slots:
    void on_addfriend_clicked();
    void on_Pint_clicked();
    void on_select_clicked();
    void on_Delete_clicked();
private:
    Ui::address_book *ui;
    QVector<Addr> tong;
    QVector<Addr>::iterator it;
};

#endif // ADDRESS_BOOK_H


/********************************************************************************
** Form generated from reading UI file 'address_book.ui'
**
** Created: Tue Feb 14 20:28:06 2017
**      by: Qt User Interface Compiler version 4.8.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_ADDRESS_BOOK_H
#define UI_ADDRESS_BOOK_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_address_book
{
public:
    QWidget *gridLayoutWidget;
    QGridLayout *gridLayout;
    QPushButton *addfriend;
    QPushButton *Pint;
    QPushButton *select;
    QPushButton *Delete;
    QLabel *label;

    void setupUi(QWidget *address_book)
    {
        if (address_book->objectName().isEmpty())
            address_book->setObjectName(QString::fromUtf8("address_book"));
        address_book->resize(411, 315);
        gridLayoutWidget = new QWidget(address_book);
        gridLayoutWidget->setObjectName(QString::fromUtf8("gridLayoutWidget"));
        gridLayoutWidget->setGeometry(QRect(80, 20, 251, 271));
        gridLayout = new QGridLayout(gridLayoutWidget);
        gridLayout->setSpacing(6);
        gridLayout->setContentsMargins(11, 11, 11, 11);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        gridLayout->setContentsMargins(0, 0, 0, 0);
        addfriend = new QPushButton(gridLayoutWidget);
        addfriend->setObjectName(QString::fromUtf8("addfriend"));

        gridLayout->addWidget(addfriend, 1, 0, 1, 1);

        Pint = new QPushButton(gridLayoutWidget);
        Pint->setObjectName(QString::fromUtf8("Pint"));

        gridLayout->addWidget(Pint, 2, 0, 1, 1);

        select = new QPushButton(gridLayoutWidget);
        select->setObjectName(QString::fromUtf8("select"));

        gridLayout->addWidget(select, 3, 0, 1, 1);

        Delete = new QPushButton(gridLayoutWidget);
        Delete->setObjectName(QString::fromUtf8("Delete"));

        gridLayout->addWidget(Delete, 4, 0, 1, 1);

        label = new QLabel(gridLayoutWidget);
        label->setObjectName(QString::fromUtf8("label"));
        label->setMinimumSize(QSize(130, 30));
        label->setMaximumSize(QSize(130, 30));
        label->setAlignment(Qt::AlignCenter);

        gridLayout->addWidget(label, 0, 0, 1, 1);


        retranslateUi(address_book);

        QMetaObject::connectSlotsByName(address_book);
    } // setupUi

    void retranslateUi(QWidget *address_book)
    {
        address_book->setWindowTitle(QApplication::translate("address_book", "address_book", 0, QApplication::UnicodeUTF8));
        addfriend->setText(QApplication::translate("address_book", "\346\267\273\345\212\240\345\245\275\345\217\213", 0, QApplication::UnicodeUTF8));
        Pint->setText(QApplication::translate("address_book", "\345\210\227\350\241\250\345\245\275\345\217\213", 0, QApplication::UnicodeUTF8));
        select->setText(QApplication::translate("address_book", "\346\237\245\350\257\242\345\245\275\345\217\213", 0, QApplication::UnicodeUTF8));
        Delete->setText(QApplication::translate("address_book", "\345\210\240\351\231\244\345\245\275\345\217\213", 0, QApplication::UnicodeUTF8));
        label->setText(QApplication::translate("address_book", "\351\200\232\350\256\257\345\275\225", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class address_book: public Ui_address_book {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_ADDRESS_BOOK_H



/**************address_book.cpp******************************/
#include "address_book.h"
#include "addr.h"
#include "ui_address_book.h"
#include <QMessageBox>
#include <QProgressDialog>
#include <QCoreApplication>
#include <QErrorMessage>
#include <QVBoxLayout>
#include <QFont>

int i = 0;

address_book::address_book(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::address_book)
{
    ui->setupUi(this);
}

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

void address_book::on_addfriend_clicked()
{
    bool ok;
    i++;
    QInputDialog ecf;
    ecf.resize(1000,1000);
    QString name = ecf.getText(this, tr("添加好友"),tr("请输入用户名:"),QLineEdit::Normal, NULL,&ok);
    if(ok)
    {
        qDebug()<<"string:"<<name;
    }

    QString tel = ecf.getText(this, tr("添加好友"),tr("请输入手机号:"),QLineEdit::Normal, NULL,&ok);
    if(ok)
    {
        qDebug()<<"string:"<<tel;
    }

    int len = tel.length();

    while(len != 11)                //判断输入的手机号码是不是11位的
    {
        tel = QInputDialog::getText(this, tr("添加好友"),tr("请输入11位手机号:"),QLineEdit::Normal, NULL,&ok);
        len = tel.length();
    }

    QString address = ecf.getText(this, tr("添加好友"),tr("请输入家庭住址:"),QLineEdit::Normal, NULL,&ok);
    if(ok)
    {
        qDebug()<<"string:"<<address;
    }

    QString company_phone = ecf.getText(this, tr("添加好友"),tr("请输入家庭号码:"),QLineEdit::Normal, NULL,&ok);
    if(ok)
    {
        qDebug()<<"string:"<<company_phone;
    }

    Addr *one = new Addr;
    one->m_ID = i;
    one->m_name = name;
    one->m_tel = tel;
    one->m_address = address;
    one->m_company_phone = company_phone;

    tong.push_back(*one);

    QProgressDialog dialog(tr("正在添加好友"),tr("取消"), 0, 3000, this);
    dialog.setWindowTitle(tr("进度"));
    dialog.setWindowModality(Qt::WindowModal);
    dialog.show();
    for(int k = 0; k < 3000; k++)
    {
        dialog.setValue(k);
        QCoreApplication::processEvents();
        if(dialog.wasCanceled())
        {
            break;
        }
    }
    dialog.setValue(3000);
    qDebug()<<tr("添加好友成功");

//    QLabel *label = new QLabel(tr("添加好友成功"));
//    label->resize(100,100);
//    label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//    label->show();
     QMessageBox::information(this, tr("添加结果"), tr("添加好友成功"), QMessageBox::Ok);
}

void address_book::on_Pint_clicked()
{
    if(tong.size() == 0)
    {
        QErrorMessage *err = new QErrorMessage(this);
        err->setWindowTitle(tr("错误信息"));
        err->showMessage(tr("通讯录中暂时无人!"));
        return;
    }

    QLabel *label = new QLabel;
    label->adjustSize();label->resize(500,300);
    label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    QString str;
    QFont ft;
    ft.setPointSize(13);                //设置字体大小
    ft.setBold(true);                   //设置字体加粗,类似的还有斜体等等
    label->setFont(ft);

    for(int k = 0; k < tong.size(); k++)
    {
        str.append(tong[k].m_name);
        str.append("   ");
        str.append(tong[k].m_tel);
        str.append("   ");
        str.append(tong[k].m_address);
        str.append("   ");
        str.append(tong[k].m_company_phone);
        str.append("\n");
    }
    label->setText(str);
    label->show();
}

void address_book::on_select_clicked()
{
    if(tong.size() == 0)
    {
        QErrorMessage *err = new QErrorMessage(this);
        err->setWindowTitle(tr("错误信息"));
        err->showMessage(tr("通讯录中暂时无人!"));
        return;
    }

    bool ok;
    QString name = QInputDialog::getText(this, tr("查询好友"),tr("请输入用户名:"),QLineEdit::Normal, NULL,&ok);
    if(ok)
    {
        qDebug()<<"string:"<<name;
    }

    QProgressDialog dialog(tr("正在查询好友"),tr("取消"), 0, 3000, this);
    dialog.setWindowTitle(tr("进度"));
    dialog.setWindowModality(Qt::WindowModal);
    dialog.show();
    for(int k = 0; k < 3000; k++)
    {
        dialog.setValue(k);
        QCoreApplication::processEvents();
        if(dialog.wasCanceled())
        {
            break;
        }
    }
    dialog.setValue(3000);

    int flag = 1;
    QString str;
    QLabel *label = new QLabel;
    label->adjustSize();label->resize(500,300);
    label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    QFont ft;
    ft.setPointSize(13);                //设置字体大小
    ft.setBold(true);                   //设置字体加粗,类似的还有斜体等等
    label->setFont(ft);

    for(int k = 0; k < tong.size(); k++)
    {
        if(tong[k].m_name == name)
        {
            flag = 0;
            str.append(tong[k].m_name);
            str.append("   ");
            str.append(tong[k].m_tel);
            str.append("   ");
            str.append(tong[k].m_address);
            str.append("   ");
            str.append(tong[k].m_company_phone);
            str.append("\n");
        }
    }

    if(flag == 0)
    {
        label->setText(str);
        label->show();
    }
    else
    {
        QErrorMessage *err = new QErrorMessage(this);
        err->setWindowTitle(tr("错误信息"));
        err->showMessage(tr("查无此人!"));
    }
}

void address_book::on_Delete_clicked()
{
    bool ok;
    QString name = QInputDialog::getText(this, tr("删除好友"),tr("请输入用户名:"),QLineEdit::Normal, NULL,&ok);
    if(ok)
    {
        qDebug()<<"string:"<<name;
    }

    QProgressDialog dialog(tr("正在删除好友"),tr("取消"), 0, 3000, this);
    dialog.setWindowTitle(tr("进度"));
    dialog.setWindowModality(Qt::WindowModal);
    dialog.show();
    for(int k = 0; k < 3000; k++)
    {
        dialog.setValue(k);
        QCoreApplication::processEvents();
        if(dialog.wasCanceled())
        {
            break;
        }
    }
    dialog.setValue(3000);

    if(tong.size() == 0)
    {
        QErrorMessage *err = new QErrorMessage(this);
        err->setWindowTitle(tr("错误信息"));
        err->showMessage(tr("通讯录中暂时无人!"));
        return;
    }

    int flag = 1;
    QLabel *label = new QLabel;
    label->adjustSize();
    label->resize(400,300);
    label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    QFont ft;
    ft.setPointSize(13);                //设置字体大小
    ft.setBold(true);                   //设置字体加粗,类似的还有斜体等等
    label->setFont(ft);

    for(it = tong.begin(); it != tong.end(); it++)
    {
        if(it->m_name == name)
        {
            flag = 0;
            tong.erase(it);
            QMessageBox::information(this, tr("删除结果"), tr("删除成功"), QMessageBox::Ok);
            return;
        }
    }

    if(flag)
    {
        QErrorMessage *err = new QErrorMessage(this);
        err->setWindowTitle(tr("错误信息"));
        err->showMessage(tr("查无此人!"));
    }
}

/***********main.cpp************************/
#include <QtGui/QApplication>
#include "address_book.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
    address_book w;
    w.show();
    
    return a.exec();
}

以上代码若是编译有问题的话,可以到我的网盘下载:http://pan.baidu.com/s/1eRVWrxo

还有上次qt小神童的第三个教程的源代码,可以分享一下:http://pan.baidu.com/s/1cmZ4CY

转载于:https://my.oschina.net/u/3433829/blog/881564

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值