Drawer::Drawer(QWidget *parent, Qt::WindowFlags f) :QToolBox(parent, f) { setWindowTitle(tr("Myself QQ 2013")); //设置主窗口的标题 setWindowIcon(QPixmap(":/image/qq.png")); //设置主窗体标题栏图标 toolBtn1 = new QToolButton; toolBtn1->setText(tr("道枝小尧")); toolBtn1->setIcon(QPixmap(":/image/spqy.png")); toolBtn1->setIconSize(QPixmap(":/image/spqy.png").size()); toolBtn1->setAutoRaise(true); toolBtn1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); connect(toolBtn1, SIGNAL(clicked()), this, SLOT(showChatWidget1())); toolBtn2 = new QToolButton; toolBtn2->setText(tr("忆梦如澜")); toolBtn2->setIcon(QPixmap(":/image/ymrl.png")); toolBtn2->setIconSize(QPixmap(":/image/ymrl.png").size()); toolBtn2->setAutoRaise(true); toolBtn2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); connect(toolBtn2, SIGNAL(clicked()), this, SLOT(showChatWidget2()));在此基础上添加登录界面
时间: 2024-03-13 20:42:51 浏览: 108
qtquick-navigationdrawer:适用于QtQuick的android风格的导航抽屉
在这段代码中,Drawer 类的构造函数已经实现了 QQ 聊天界面的主体部分,现在需要添加登录界面。可以在构造函数中添加一个 QDialog 对象,该对象包含用户名和密码的输入框以及登录和取消按钮。当用户点击登录按钮时,检查输入的用户名和密码是否正确,如果正确则关闭登录界面并显示 QQ 聊天窗口,否则弹出错误提示框。
以下是可能的实现方式:
```c++
Drawer::Drawer(QWidget *parent, Qt::WindowFlags f)
: QToolBox(parent, f)
{
setWindowTitle(tr("Myself QQ 2013"));
setWindowIcon(QPixmap(":/image/qq.png"));
// 添加登录界面
QDialog loginDialog(this);
loginDialog.setWindowTitle(tr("登录"));
QLabel* nameLabel = new QLabel(tr("用户名:"));
QLineEdit* nameEdit = new QLineEdit;
QLabel* pwdLabel = new QLabel(tr("密码:"));
QLineEdit* pwdEdit = new QLineEdit;
pwdEdit->setEchoMode(QLineEdit::Password);
QPushButton* loginButton = new QPushButton(tr("登录"));
QPushButton* cancelButton = new QPushButton(tr("取消"));
QHBoxLayout* nameLayout = new QHBoxLayout;
nameLayout->addWidget(nameLabel);
nameLayout->addWidget(nameEdit);
QHBoxLayout* pwdLayout = new QHBoxLayout;
pwdLayout->addWidget(pwdLabel);
pwdLayout->addWidget(pwdEdit);
QVBoxLayout* buttonLayout = new QVBoxLayout;
buttonLayout->addWidget(loginButton);
buttonLayout->addWidget(cancelButton);
QVBoxLayout* mainLayout = new QVBoxLayout(&loginDialog);
mainLayout->addLayout(nameLayout);
mainLayout->addLayout(pwdLayout);
mainLayout->addLayout(buttonLayout);
connect(loginButton, &QPushButton::clicked, [&](){
QString username = nameEdit->text();
QString password = pwdEdit->text();
if (username == "admin" && password == "123456") {
loginDialog.close();
showChatWidget1();
} else {
QMessageBox::warning(this, tr("错误"), tr("用户名或密码不正确!"));
}
});
connect(cancelButton, &QPushButton::clicked, [&](){
loginDialog.close();
close();
});
// 添加聊天窗口按钮
toolBtn1 = new QToolButton;
toolBtn1->setText(tr("道枝小尧"));
toolBtn1->setIcon(QPixmap(":/image/spqy.png"));
toolBtn1->setIconSize(QPixmap(":/image/spqy.png").size());
toolBtn1->setAutoRaise(true);
toolBtn1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
connect(toolBtn1, &QToolButton::clicked, this, &Drawer::showChatWidget1);
toolBtn2 = new QToolButton;
toolBtn2->setText(tr("忆梦如澜"));
toolBtn2->setIcon(QPixmap(":/image/ymrl.png"));
toolBtn2->setIconSize(QPixmap(":/image/ymrl.png").size());
toolBtn2->setAutoRaise(true);
toolBtn2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
connect(toolBtn2, &QToolButton::clicked, this, &Drawer::showChatWidget2);
}
```
注意,这里使用了 C++11 中的 lambda 表达式来简化信号槽的连接,同时也使用了新的信号槽语法。
阅读全文