关于qt中如何布局

qt中有水平布局 垂直布局等等

1 可把控件放到空窗口中进行水平布局

要想有间隙 加弹簧即可

lineedit控件中若想让输入的数在屏幕上显示密码 别人不可见

usernameLineEdit->text(); 这个函数是获取用户输入 然后与if else 中的密码相匹配

复制代码
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>

class LoginWidget : public QWidget {
    Q_OBJECT
public:
    LoginWidget(QWidget *parent = nullptr) : QWidget(parent) {
        // 创建布局
        QVBoxLayout *layout = new QVBoxLayout(this);

        // 创建用户名和密码输入框
        usernameLineEdit = new QLineEdit;
        passwordLineEdit = new QLineEdit;
        passwordLineEdit->setEchoMode(QLineEdit::Password); // 设置密码模式

        // 创建登录按钮
        loginButton = new QPushButton("Login");

        // 创建标签用于显示错误消息
        errorLabel = new QLabel;

        // 添加到布局
        layout->addWidget(new QLabel("Username:"));
        layout->addWidget(usernameLineEdit);
        layout->addWidget(new QLabel("Password:"));
        layout->addWidget(passwordLineEdit);
        layout->addWidget(loginButton);
        layout->addWidget(errorLabel);

        // 连接信号和槽
        connect(loginButton, &QPushButton::clicked, this, &LoginWidget::onLoginClicked);
    }

private slots:
    void onLoginClicked() {
        // 获取用户名和密码
        QString username = usernameLineEdit->text();
        QString password = passwordLineEdit->text();

        // 检查用户名和密码
        if (username == "admin" && password == "password") {
            errorLabel->setText("Login successful.");
            // 在这里可以添加更多的逻辑,例如打开新的窗口等
        } else {
            errorLabel->setText("Incorrect username or password.");
        }
    }

private:
    QLineEdit *usernameLineEdit;
    QLineEdit *passwordLineEdit;
    QPushButton *loginButton;
    QLabel *errorLabel;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    LoginWidget widget;
    widget.show();

    return app.exec();
}
相关推荐
sunfove4 分钟前
实战篇:用 Python 徒手实现模拟退火算法解决 TSP 问题
开发语言·python·模拟退火算法
jiunian_cn5 分钟前
【C++】IO流
开发语言·c++
froginwe1143 分钟前
C 语言输入与输出详解
开发语言
_童年的回忆_1 小时前
【PHP】关于守护进程报错:SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
开发语言·oracle·php
我是菜鸟0713号1 小时前
Qt + Python 算法集成的一种低耦合实践:FastAPI 服务化方案
python·qt·fastapi
少林码僧1 小时前
2.30 传统行业预测神器:为什么GBDT系列算法在企业中最受欢迎
开发语言·人工智能·算法·机器学习·ai·数据分析
CoderCodingNo1 小时前
【GESP】C++六级考试大纲知识点梳理, (7) 栈与队列
开发语言·c++
edisao1 小时前
六、 读者高频疑问解答 & 架构价值延伸
大数据·开发语言·人工智能·科技·架构·php
范纹杉想快点毕业1 小时前
C语言实现埃拉托斯特尼筛法
c语言·开发语言