Windows编程实验最后一课

qt实现QQ登录界面

mychat.pro

QT       += core gui \
            sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MyChat
TEMPLATE = app


SOURCES += main.cpp\
    login.cpp \
    register.cpp \
    chatwindow.cpp

HEADERS  += \
    login.h \
    register.h \
    chatwindow.h

FORMS    += \
    login.ui \
    register.ui \
    chatwindow.ui

RESOURCES += \
    chat.qrc
chatwindow.h
#ifndef CHATWINDOW_H
#define CHATWINDOW_H

#include <QDialog>

namespace Ui {
class ChatWindow;
}

class ChatWindow : public QDialog
{
    Q_OBJECT

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

private:
    Ui::ChatWindow *ui;
};

#endif // CHATWINDOW_H
login.h
#ifndef LOGIN_H
#define LOGIN_H

#include <QWidget>
#include "register.h"
#include "chatwindow.h"
#include <QPainter>

namespace Ui {
class Login;
}

class Login : public QWidget
{
    Q_OBJECT

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

signals:
    void transmitdb(QSqlDatabase db);

private slots:
    void login_clicked();   //登录按键槽函数
    void register_clicked();    //注册按键槽函数
    void getUserInfo(QString name);

private:
    Ui::Login *ui;
    QSqlDatabase database;
    bool tableFlag;

    int usr_id;
    QString usr_passwd;
    QString usr_name;
    QString usr_email;
    int usr_history;
    bool matchFlag;

    QString select_table = "select tbl_name name from sqlite_master where type = 'table'";
    QString create_sql = "create table user (chatid int primary key, passwd varchar(30), name varchar(30), email varchar(30), history int)";
    QString select_max_sql = "select max(chatid) from user";
    QString insert_sql = "insert into user values (?, ?, ?, ? ?)";
    QString select_sql = "select name from user";
    QString select_nameInfo = "selcet * from user wher name=";


};

#endif // LOGIN_H

register.h
#ifndef REGISTER_H
#define REGISTER_H

#include <QDialog>
#include <QtSql>
#include "login.h"

namespace Ui {
class Register;
}

class Register : public QDialog
{
    Q_OBJECT

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

    void dboperation();

private:
    Ui::Register *ui;

    QSqlDatabase database;
    bool tableFlag;
    int max_id;

private slots:
    void okBtn_clicked();
    void closeBtn_clicked();
    void receivedb(QSqlDatabase db);
};

#endif // REGISTER_H

chatwindow.cpp
#include "chatwindow.h"
#include "ui_chatwindow.h"

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

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

login.cpp
#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    connect(ui->regBtn,SIGNAL(clicked(bool)),this,SLOT(register_clicked()));
    connect(ui->loginBtn,SIGNAL(clicked(bool)),this,SLOT(login_clicked()));
    connect(ui->nameCmBox,SIGNAL(editTextChanged(QString)),this,SLOT(getUserInfo(QString)));
    tableFlag=false;

    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("database.db");  
    if(!database.open())
    {
        qDebug()<<database.lastError();
        qFatal("failed to connect.") ;
    }
    else
    {
        qDebug()<<"open seccess";
        QSqlQuery sql_query;      
        sql_query.prepare(select_table);
        if(!sql_query.exec())
        {
            qDebug()<<sql_query.lastError();
        }
        else
        {
            QString tableName;
            while(sql_query.next())
            {
                tableName = sql_query.value(0).toString();
                qDebug()<<tableName;
                if(tableName.compare("user"))
                {
                    tableFlag=false;
                    qDebug()<<"table is not exist";
                }
                else
                {
                    tableFlag=true;
                    qDebug()<<"table is exist";
                }
            }
        }
        if(tableFlag==false)
        {
            sql_query.prepare(create_sql);
            if(!sql_query.exec())
            {
                qDebug()<<sql_query.lastError();
            }
            else
            {
                qDebug()<<"table created!";
            }
        }
    }
}
Login::~Login()
{
    delete ui;
}
void Login::register_clicked()
{
    //QApplication b();
    Register r(this);
    this->hide();
    r.show();
    transmitdb(database);
    r.exec();
    this->show();
}
void Login::login_clicked()
{
    if(matchFlag==false)
    {
        qDebug()<<"name invalid";
    }
    else
    {
        if(usr_passwd!=ui->passwdLineEdit->text())
        {
            qDebug()<<"passwd not match";
        }
        else
        {
            ChatWindow cw(this);
            this->hide();
            cw.show();
            cw.exec();
            this->close();
        }
    }
}
void Login::getUserInfo(QString name)
{

    QSqlQuery sql_query;        
    QString tempstring="select * from user where name='"+name+"'";
    qDebug()<<tempstring;
    if(!sql_query.exec(tempstring))
    {
        qDebug()<<sql_query.lastError();
        matchFlag=false;
    }
    else
    {
        while(sql_query.next())
        {
            usr_id = sql_query.value(0).toInt();
            usr_passwd = sql_query.value(1).toString();
            usr_name = sql_query.value(2).toString();
            usr_email = sql_query.value(3).toString();
            usr_history = sql_query.value(4).toInt();
            qDebug()<<QString("chatid=%1    passwd=%2     name=%3       email=%4    history=%5").arg(usr_id).arg(usr_passwd).arg(usr_name).arg(usr_email).arg(usr_history);
        }
        if(usr_name==name)  matchFlag=true;
        else                matchFlag=false;
    }    
    qDebug()<<matchFlag;
    if(matchFlag==true)
    {
        QString path=":/image/userx.png";
        QString diff="user"+QString::number(usr_id);
        path.replace("userx",diff);
        QImage img;
        img.load(path);
    }
    else
    {
        QPixmap pic;
    }
}

main.cpp
#include "login.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Login w;
    w.show();

    return a.exec();
}

register.cpp
#include "register.h"
#include "ui_register.h"

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

    connect(ui->okBtn,SIGNAL(clicked(bool)),this,SLOT(okBtn_clicked()));
    connect(ui->closeBtn,SIGNAL(clicked(bool)),this,SLOT(closeBtn_clicked()));
    connect((Login *)parent,SIGNAL(transmitdb(QSqlDatabase)),this,SLOT(receivedb(QSqlDatabase)));
    dboperation();
}

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

QString select_table = "select tbl_name name from sqlite_master where type = 'table'";
QString create_sql = "create table user (chatid int primary key, passwd varchar(30), name varchar(30), email varchar(30), history int)";
QString select_max_sql = "select max(chatid) from user";
QString insert_sql = "insert into user values (?, ?, ?, ?, ?)";
QString select_sql = "select name from user";
void Register::dboperation()
{
}

void Register::okBtn_clicked()
{
    bool nameFlag=false;    //用户名有效标志
    bool passwdFlag=false;  //密码有效标志
    int newchatid=max_id+1;
    QString newpasswd=NULL;
    QString newname=NULL;

    if(ui->passwd1LineEdit->text()==""||ui->passwd2LineEdit->text()=="")
    {
        passwdFlag=false;
    }
    else if(ui->passwd1LineEdit->text()==ui->passwd2LineEdit->text())    //两次密码相同
    {
        passwdFlag=true;
    }
    else
    {
        qDebug()<<"passwd err";
        passwdFlag=false;
    }

    QSqlQuery sql_query;

    max_id = 0;
    sql_query.prepare(select_max_sql);
    if(!sql_query.exec())
    {
        qDebug()<<sql_query.lastError();
    }
    else
    {
        while(sql_query.next())
        {
            max_id = sql_query.value(0).toInt();
            qDebug()<<QString("max chatid:%1").arg(max_id);
        }
    }

    if(!sql_query.exec(select_sql))
    {
        qDebug()<<sql_query.lastError();
    }
    else
    {
        while(1)
        {
            if(sql_query.next())    //name有数据
            {
                QString name = sql_query.value("name").toString();
                qDebug()<<QString("name=%1").arg(name);

                if(ui->nameLineEdit->text()==name)    //用户名已经存在
                {
                    qDebug()<<"name existed";
                    nameFlag=false;
                    break;
                }
                else
                {
                    //newname=ui->nameLineEdit->text();
                    nameFlag=true;
                }
            }
            else
            {       //name列为空
                nameFlag=true;
                break;
            }
        }
    }

    newchatid=max_id+1;
    if(nameFlag==true) newname=ui->nameLineEdit->text();
    else                return;
    if(passwdFlag==true)    newpasswd=ui->passwd1LineEdit->text();
    else                    return;

    //插入数据
    sql_query.prepare(insert_sql);
    sql_query.addBindValue(newchatid);              //chatid
    sql_query.addBindValue(newpasswd);              //passwd
    sql_query.addBindValue(newname);                //name
    sql_query.addBindValue(0);                      //history
    if(!sql_query.exec())
    {
        qDebug()<<sql_query.lastError();
    }
    else
    {
        qDebug()<<"inserted!";
    }

    this->close();
}

void Register::receivedb(QSqlDatabase db)
{
    qDebug()<<"received db";
    database=db;
    if(!database.isOpen())
    {
        if(!database.open())
        {
            qDebug()<<database.lastError();
            qFatal("failed to connect.") ;
            return;
        }
        else
        {
        }
    }
}

void Register::closeBtn_clicked()
{
    this->close();
}

chatwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ChatWindow</class>
 <widget class="QDialog" name="ChatWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>208</width>
    <height>432</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>0</y>
     <width>111</width>
     <height>241</height>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Login</class>
 <widget class="QWidget" name="Login">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>448</width>
    <height>247</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QComboBox" name="nameCmBox">
   <property name="geometry">
    <rect>
     <x>140</x>
     <y>100</y>
     <width>271</width>
     <height>31</height>
    </rect>
   </property>
   <property name="editable">
    <bool>true</bool>
   </property>
  </widget>
  <widget class="QLineEdit" name="passwdLineEdit">
   <property name="geometry">
    <rect>
     <x>140</x>
     <y>150</y>
     <width>271</width>
     <height>31</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="regBtn">
   <property name="geometry">
    <rect>
     <x>240</x>
     <y>200</y>
     <width>75</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text">
    <string>注册</string>
   </property>
  </widget>
  <widget class="QPushButton" name="loginBtn">
   <property name="geometry">
    <rect>
     <x>330</x>
     <y>200</y>
     <width>75</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text">
    <string>登录</string>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>96</y>
     <width>31</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>帐号</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>150</y>
     <width>31</width>
     <height>17</height>
    </rect>
   </property>
   <property name="text">
    <string>密码</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_3">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>0</y>
     <width>81</width>
     <height>61</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">font: 75 36pt "Waree";</string>
   </property>
   <property name="text">
    <string>QQ</string>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <tabstops>
  <tabstop>nameCmBox</tabstop>
  <tabstop>passwdLineEdit</tabstop>
  <tabstop>loginBtn</tabstop>
  <tabstop>regBtn</tabstop>
 </tabstops>
 <resources/>
 <connections/>
</ui>

</rect> </property> <property name="styleSheet"> <string notr="true">font: 13pt "Waree";</string> </property> <property name="text"> <string>汉大帮名单祁同伟高育良</string> </property> </widget> </widget> <resources/> <connections/></ui>

login.ui 
 
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Login</class>
 <widget class="QWidget" name="Login">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>448</width>
    <height>247</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QComboBox" name="nameCmBox">
   <property name="geometry">
    <rect>
     <x>140</x>
     <y>100</y>
     <width>271</width>
     <height>31</height>
    </rect>
   </property>
   <property name="editable">
    <bool>true</bool>
   </property>
  </widget>
  <widget class="QLineEdit" name="passwdLineEdit">
   <property name="geometry">
    <rect>
     <x>140</x>
     <y>150</y>
     <width>271</width>
     <height>31</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="regBtn">
   <property name="geometry">
    <rect>
     <x>240</x>
     <y>200</y>
     <width>75</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text">
    <string>注册</string>
   </property>
  </widget>
  <widget class="QPushButton" name="loginBtn">
   <property name="geometry">
    <rect>
     <x>330</x>
     <y>200</y>
     <width>75</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text">
    <string>登录</string>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>96</y>
     <width>31</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>帐号</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>150</y>
     <width>31</width>
     <height>17</height>
    </rect>
   </property>
   <property name="text">
    <string>密码</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_3">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>0</y>
     <width>81</width>
     <height>61</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">font: 75 36pt "Waree";</string>
   </property>
   <property name="text">
    <string>QQ</string>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <tabstops>
  <tabstop>nameCmBox</tabstop>
  <tabstop>passwdLineEdit</tabstop>
  <tabstop>loginBtn</tabstop>
  <tabstop>regBtn</tabstop>
 </tabstops>
 <resources/>
 <connections/>
</ui>

register.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Register</class>
 <widget class="QDialog" name="Register">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>310</width>
    <height>242</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QLineEdit" name="nameLineEdit">
   <property name="geometry">
    <rect>
     <x>112</x>
     <y>40</y>
     <width>171</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QLineEdit" name="passwd2LineEdit">
   <property name="geometry">
    <rect>
     <x>112</x>
     <y>139</y>
     <width>171</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QLineEdit" name="passwd1LineEdit">
   <property name="geometry">
    <rect>
     <x>112</x>
     <y>90</y>
     <width>171</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="closeBtn">
   <property name="geometry">
    <rect>
     <x>120</x>
     <y>200</y>
     <width>75</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text">
    <string>Close</string>
   </property>
  </widget>
  <widget class="QPushButton" name="okBtn">
   <property name="geometry">
    <rect>
     <x>210</x>
     <y>200</y>
     <width>75</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text">
    <string>OK</string>
   </property>
  </widget>
  <widget class="QLabel" name="nameLabel">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>40</y>
     <width>54</width>
     <height>12</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>11</pointsize>
    </font>
   </property>
   <property name="text">
    <string>Name</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
   </property>
  </widget>
  <widget class="QLabel" name="passw1dLabel">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>90</y>
     <width>54</width>
     <height>12</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>11</pointsize>
    </font>
   </property>
   <property name="text">
    <string>Passwd</string>
   </property>
  </widget>
  <widget class="QLabel" name="passw2dLabel">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>140</y>
     <width>101</width>
     <height>16</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>11</pointsize>
    </font>
   </property>
   <property name="text">
    <string>Passwd</string>
   </property>
  </widget>
 </widget>
 <tabstops>
  <tabstop>nameLineEdit</tabstop>
  <tabstop>passwd1LineEdit</tabstop>
  <tabstop>passwd2LineEdit</tabstop>
  <tabstop>okBtn</tabstop>
  <tabstop>closeBtn</tabstop>
 </tabstops>
 <resources/>
 <connections/>
</ui>



 
 
 
 


猜你喜欢

转载自blog.csdn.net/qq_36946026/article/details/70173719