关于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();
}
相关推荐
froginwe112 小时前
PHP 魔术常量
开发语言
古城小栈2 小时前
Rust 的 validator 库
开发语言·后端·rust
froginwe112 小时前
《Foundation 选项卡:设计与实现的深入探讨》
开发语言
lsx2024062 小时前
XSLT `<sort>` 标签详解
开发语言
古城小栈2 小时前
Rust 的 redis-rs 库
开发语言·redis·rust
楚Y6同学2 小时前
基于 Haversine 公式实现【经纬度坐标点】球面距离计算(C++/Qt 实现)
开发语言·c++·qt·经纬度距离计算
你怎么知道我是队长2 小时前
C语言---缓冲区
c语言·开发语言
一只专注api接口开发的技术猿3 小时前
如何处理淘宝 API 的请求限流与数据缓存策略
java·大数据·开发语言·数据库·spring
superman超哥3 小时前
Rust 异步递归的解决方案
开发语言·后端·rust·编程语言·rust异步递归