关于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();
}
相关推荐
Hello-Mr.Wang几秒前
vue3中开发引导页的方法
开发语言·前端·javascript
救救孩子把3 分钟前
Java基础之IO流
java·开发语言
WG_174 分钟前
C++多态
开发语言·c++·面试
宇卿.11 分钟前
Java键盘输入语句
java·开发语言
Amo Xiang21 分钟前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
friklogff33 分钟前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链
重生之我在20年代敲代码2 小时前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
爱上语文2 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
编程零零七4 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
2401_858286115 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言