欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > 关于qt中如何布局

关于qt中如何布局

2025/2/24 5:27:50 来源:https://blog.csdn.net/qq_73937769/article/details/140865761  浏览:    关键词:关于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();
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词