QT创建登录页面

<pre name="code" class="cpp">这是头文件
<pre name="code" class="cpp">#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSlider>
#include <QDebug>
#include <QSpinBox>
#include <QPushButton>
#include <QLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QAction>
#include <QLineEdit>
#include <QIcon>
#include "Login.h"
class Widget : public QWidget
{
    Q_OBJECT
    
public:
    Widget(QWidget *parent = 0);
    ~Widget();
private:
        Login *login;
        QLineEdit *name;
        QLineEdit  *password ;
private slots:
        void return_State();
        void login_false();
        void login_ok();
signals:
        void get_name(QString name,QString password);
};

#endif // WIDGET_H


这是第一个例子
#include "widget.h"
#include <QMessageBox>
#include <QBoxLayout>
#include <QFormLayout>
#include <QLabel>
#include <QCheckBox>


    QLabel *label_find = new QLabel(tr("Find &what: "));
    QLineEdit *lineEdit_find = new QLineEdit();
    label_find->setBuddy(lineEdit_find);


    QCheckBox* checkBox_match = new QCheckBox(tr("Match &case"));
    QCheckBox* checkbox_search =  new QCheckBox(tr("Search &backford"));

    QPushButton* pushbutton_find = new QPushButton(tr("&Find"));
    pushbutton_find->setDefault(true);
    pushbutton_find->setEnabled(false);
    QPushButton* pushbutton_close =new QPushButton(tr("Close"));

    QHBoxLayout *topLeftLayout = new QHBoxLayout;
    topLeftLayout->addWidget(label_find);
    topLeftLayout->addWidget(lineEdit_find);


     QVBoxLayout *leftLayout = new QVBoxLayout;
     leftLayout->addLayout(topLeftLayout);
     leftLayout->addWidget(checkBox_match);
     leftLayout->addWidget(checkbox_search);


     QVBoxLayout *rightLayout = new QVBoxLayout;
     rightLayout->addWidget(pushbutton_find);
     rightLayout->addWidget(pushbutton_close);
     rightLayout->addStretch();//添加弹簧将上面的布局顶上去
     QHBoxLayout *mainLayout = new QHBoxLayout;
     mainLayout->addLayout(leftLayout);
     mainLayout->addLayout(rightLayout);
     setLayout(mainLayout);
     setFixedHeight(sizeHint().height());

这是第二个

    QIcon *icon = new QIcon("/image/User.png");
    QFormLayout* formlayout_main = new QFormLayout(this);
//     QHBoxLayout *layout_h = new QHBoxLayout(this);

    QVBoxLayout* layout_v = new QVBoxLayout(this);
   // QBoxLayout *layoyt_box = new QBoxLayout(dir, parent = 0);
//    QGridLayout  *layout1 =new QGridLayout(this);
//    layout1->addWidget(button,5,5,5,1);
    QLabel *username = new QLabel();
    QLabel *pwd =  new QLabel("密码:");
    name = new  QLineEdit();
    password = new QLineEdit();
    formlayout_main->addRow(username);
    QPushButton *button_login = new QPushButton("login");
    QPushButton *button_quit = new QPushButton("quit");
//    layout_v->addWidget(username);
//    layout_v->addWidget(name);
    formlayout_main->addRow("用户名:",name);

    formlayout_main->addRow("密码:",password);
    formlayout_main->addRow(button_quit,button_login);
    QSpinBox *spinBox = new QSpinBox;
    QSlider *slider = new QSlider(Qt::Horizontal);
    spinBox->setRange(0, 200);
    slider->setRange(0, 200);


    formlayout_main->addRow(spinBox);
    formlayout_main->addRow(slider);

    this->resize( 200,600);
    login = new Login(this);

    connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int)));
    connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
    connect(button_login,SIGNAL(clicked()),this,SLOT(return_State()));
    connect(this,SIGNAL(get_name(QString,QString)),login,SLOT(verfiy_login(QString,QString)));
    slider->setValue(100);
    this->setFixedSize(sizeHint());

好的,以下是用 Qt 创建登录界面并与数据库交互的步骤: 1. 打开 Qt Creator,创建一个新的 Qt Widgets 应用程序项目。 2. 在项目中添加一个登录界面的 UI 界面,可以使用 Qt Designer 工具进行创建。 3. 在登录界面中添加用户名和密码的输入框以及登录按钮。 4. 在 Qt Creator 中添加数据库连接,选择数据库类型和数据库驱动,输入数据库的连接信息,如数据库名称、用户名和密码等等。 5. 在登录按钮的槽函数中,获取输入框中的用户名和密码信息,然后使用 SQL 语句查询数据库中是否有匹配的用户信息。如果有,弹出提示框登录成功;如果没有,弹出提示框登录失败。 以下是一个简单的示例代码: ```cpp #include <QApplication> #include <QtSql> #include <QSqlDatabase> #include <QSqlQuery> #include <QDebug> #include <QMessageBox> int main(int argc, char *argv[]) { QApplication a(argc, argv); // 连接数据库 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("test.db"); if (!db.open()) { qDebug() << "Failed to connect to database!"; return -1; } // 创建数据表 QSqlQuery query; query.exec("create table user (id int primary key, name varchar(20), password varchar(20))"); query.exec("insert into user values(1, 'admin', '123456')"); // 创建登录界面 QWidget w; w.setWindowTitle("Login"); w.resize(300, 200); QLabel *label1 = new QLabel("Username:", &w); label1->move(50, 50); QLineEdit *lineEdit1 = new QLineEdit(&w); lineEdit1->move(120, 50); QLabel *label2 = new QLabel("Password:", &w); label2->move(50, 100); QLineEdit *lineEdit2 = new QLineEdit(&w); lineEdit2->move(120, 100); lineEdit2->setEchoMode(QLineEdit::Password); QPushButton *button = new QPushButton("Login", &w); button->move(120, 150); // 登录按钮的槽函数 QObject::connect(button, &QPushButton::clicked, [&]() { QString username = lineEdit1->text(); QString password = lineEdit2->text(); QSqlQuery query; query.exec(QString("select * from user where name='%1' and password='%2'").arg(username).arg(password)); if (query.next()) { QMessageBox::information(&w, "Information", "Login success!"); } else { QMessageBox::warning(&w, "Warning", "Login failed!"); } }); w.show(); return a.exec(); } ``` 需要注意的是,这只是一个简单的示例,实际的应用程序中可能需要更复杂的数据库操作和数据验证逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值