关于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();
}
相关推荐
侃侃_天下15 小时前
最终的信号类
开发语言·c++·算法
echoarts16 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix16 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题17 小时前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说17 小时前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔17 小时前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号18 小时前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_18 小时前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty18 小时前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再18 小时前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame