在qdialog中实现增加toolbar

MyDialog::MyDialog() :
    QDialog()
{
    QMainWindow * mainWindow = new QMainWindow(); // or your own class
                                                  // inheriting QMainWindow

    QToolBar * myToolBar = new QToolBar();
    mainWindow->addToolBar(myToolBar);

    QHBoxLayout * layout = new QHBoxLayout();
    layout->addWidget(mainWindow);
    setLayout(layout);

}

QMainWindow实例化,当成Widget添加到布局 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用QDialog实现登录,可以按照以下步骤进行: 1. 创建一个QDialog类,并在它的构造函数设置登录界面的组件,比如用户名和密码的输入框、登录按钮等。 2. 在登录按钮的槽函数,获取用户名和密码的输入内容,并进行相应的验证操作。 3. 如果验证成功,则在槽函数设置QDialog的返回值为Accepted,表示登录成功,然后关闭对话框。 4. 如果验证失败,则在槽函数设置QDialog的返回值为Rejected,表示登录失败,然后清空输入框的内容,让用户重新输入。 5. 在主窗口,调用QDialog的exec()方法来显示登录对话框,并根据返回值来判断用户是否登录成功。 以下是一个简单的示例代码: ```cpp class LoginDialog : public QDialog { Q_OBJECT public: LoginDialog(QWidget *parent = nullptr); private: QLineEdit *m_usernameEdit; QLineEdit *m_passwordEdit; QPushButton *m_loginButton; QPushButton *m_cancelButton; private slots: void onLoginButtonClicked(); }; LoginDialog::LoginDialog(QWidget *parent) : QDialog(parent) { setWindowTitle(tr("Login")); m_usernameEdit = new QLineEdit(this); m_passwordEdit = new QLineEdit(this); m_passwordEdit->setEchoMode(QLineEdit::Password); m_loginButton = new QPushButton(tr("Login"), this); m_cancelButton = new QPushButton(tr("Cancel"), this); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addWidget(m_loginButton); buttonLayout->addWidget(m_cancelButton); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(m_usernameEdit); mainLayout->addWidget(m_passwordEdit); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); connect(m_loginButton, SIGNAL(clicked()), this, SLOT(onLoginButtonClicked())); connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(reject())); } void LoginDialog::onLoginButtonClicked() { QString username = m_usernameEdit->text(); QString password = m_passwordEdit->text(); // 进行验证操作,如果验证成功,则设置返回值为Accepted if (username == "admin" && password == "123456") accept(); else { QMessageBox::warning(this, tr("Warning"), tr("Incorrect username or password!")); m_usernameEdit->clear(); m_passwordEdit->clear(); m_usernameEdit->setFocus(); } } ``` 在主窗口,可以这样调用登录对话框: ```cpp LoginDialog loginDialog; if (loginDialog.exec() == QDialog::Accepted) { // 登录成功 } else { // 登录失败 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值