关于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();
}
相关推荐
wjs202425 分钟前
Lua 数据类型
开发语言
源代码•宸36 分钟前
C++高频知识点(十四)
开发语言·c++·经验分享·raii
2zcode1 小时前
基于Matlab的聚类彩色图像分割系统
开发语言·matlab·聚类
网小鱼的学习笔记1 小时前
python基础:数据解析BeatuifulSoup,不需要考虑前端形式的一种获取元素的方法
开发语言·前端·python
终是蝶衣梦晓楼2 小时前
HiC-Pro Manual
java·开发语言·算法
前端_yu小白2 小时前
Vue2实现docx,xlsx,pptx预览
开发语言·javascript·ecmascript
Pocker_Spades_A3 小时前
从 0 到 1 开发图书管理系统:飞算 JavaAI 让技术落地更简单
java·开发语言·java开发·飞算javaai炫技赛
郝学胜-神的一滴3 小时前
对于类似std::shared_ptr但有可能空悬的指针使用std::weak_ptr: Effective Modern C++ 条款20
开发语言·c++·程序人生·系统架构
linux修理工3 小时前
使用 SecureCRT 连接华为 eNSP 模拟器的方法
服务器·开发语言·php
若水晴空初如梦3 小时前
QT聊天项目DAY17
开发语言·qt