急需一个QT登陆界面并带有注册功能,用sqllite实现

123无色全文 2014-12-11 10:34:52
急需一个QT登陆界面并带有注册功能,用sqllite实现,因为我的PC机QT与mysql链接不上只能用sqllite了。
请大神帮帮忙,谢谢了。
要代码,注释有没有无所谓,就是想看看是如何实现的。
...全文
1349 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
大卫无限 2014-12-12
  • 打赏
  • 举报
回复
百密一疏.本来应该是m_pQLineEditPwd的地方写成m_pQLineEditUsr了.
大卫无限 2014-12-12
  • 打赏
  • 举报
回复
为了更容易让人看懂,我已经最大程度的节省篇幅,为你书写此代码.不要问我叫什么,我叫雷锋. .pro文件

QT       += core gui sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled2
TEMPLATE = app

SOURCES += main.cpp
HEADERS  += widget.h

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui>
#include <QtSql>
#include <QtCore>

class DataCtrl : public QObject
{
public:
    DataCtrl() : m_iQSqlDatabase(QSqlDatabase::addDatabase("QSQLITE")) {
        m_iQSqlDatabase.setDatabaseName("sqlite.dat");
        m_iQSqlDatabase.open();
        QSqlQuery("create table if not exists sqlite (usr TEXT primary key, pwd TEXT)",m_iQSqlDatabase).exec();
    }

    ~DataCtrl() {
        m_iQSqlDatabase.commit();
        m_iQSqlDatabase.close();
    }

    bool verify(const QString &strName, const QString &strPwd) {
        QSqlQuery query(QString("select * from sqlite where usr = '%1' and pwd ='%2'").arg(strName, strPwd), m_iQSqlDatabase);
        query.exec();
        return query.next();
    }

    bool usrReg(const QString &strName, const QString &strPwd) {
        QSqlQuery query(QString("replace into sqlite (usr, pwd) values('%1', '%2')").arg(strName, strPwd), m_iQSqlDatabase);
        return query.exec();
    }

private:
    QSqlDatabase m_iQSqlDatabase;
};

class Window : public QWidget
{
    Q_OBJECT
public:
    Window() : m_pQLineEditUsr(new QLineEdit(this)),
        m_pQLineEditPwd(new QLineEdit(this)),
        m_pQPushButtonLogin(new QPushButton("login", this)),
        m_pQPushButtonReg(new QPushButton("register", this)) {

        m_pQPushButtonLogin->setObjectName("QPushButtonLogin");
        m_pQPushButtonReg->setObjectName("QPushButtonReg");
        QMetaObject::connectSlotsByName(this);

        m_pQLineEditUsr->setGeometry(0, 0, 150, 20);
        m_pQLineEditPwd->setGeometry(0, 20, 150, 20);
        m_pQPushButtonLogin->setGeometry(0, 40, 75, 20);
        m_pQPushButtonReg->setGeometry(75, 40, 75, 20);
    }

public slots:
    void on_QPushButtonLogin_clicked() {
        bool b = m_iDataCtrl.verify(m_pQLineEditUsr->text(), QCryptographicHash::hash(m_pQLineEditUsr->text().toLocal8Bit(), QCryptographicHash::Md5).toHex());
        QMessageBox::information(this, "tips", QString("login %1.").arg(b ? "successed" : "faild"));
    }

    void on_QPushButtonReg_clicked() {
        bool b = m_iDataCtrl.usrReg(m_pQLineEditUsr->text(), QCryptographicHash::hash(m_pQLineEditUsr->text().toLocal8Bit(), QCryptographicHash::Md5).toHex());
        QMessageBox::information(this, "tips", QString("register %1.").arg(b ? "successed" : "faild"));
    }

private:
    QLineEdit * const m_pQLineEditUsr;
    QLineEdit * const m_pQLineEditPwd;
    QPushButton * const m_pQPushButtonLogin;
    QPushButton * const m_pQPushButtonReg;
    DataCtrl m_iDataCtrl;
};
#endif // WIDGET_H

main.cpp

#include "widget.h"
#include <QApplication>

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

    return a.exec();
}

yangchuankai 2014-12-12
  • 打赏
  • 举报
回复
http://blog.csdn.net/u010002704/article/details/39994507 mysql和sqlite操作基本是一样的, 保存sqlite数据库信息用QSettings就行

16,216

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧