马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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();
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |