- QT creator 4.11.0 + C++ + 代码 = IM客户端
- 手写代码做界面。
- 客户端。
- 代码地址:XiaIM
流程
-
新建一个Qt Widgets Application项目。
-
新建一个类,取名为login。
-
界面
-
代码如下:
login.h
#ifndef LOGIN_H
#define LOGIN_H
#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <string>
#include <QTextCodec>
class CLogin : public QMainWindow
{
Q_OBJECT
public:
explicit CLogin(QMainWindow *parent = nullptr);
~CLogin();
public slots:
bool Login();
private:
QPushButton *m_pButtonLogin;
QLabel *m_pLabelUserName;
QLabel *m_pLabelpassword;
QLineEdit *m_pLineEditName;
QLineEdit *m_pLineEditPwd;
};
#endif // LOGIN_H
login.cpp
#include "login.h"
CLogin::CLogin(QMainWindow *parent)
{
this->resize(QSize(400, 300));
QPalette palette;
palette.setBrush(QPalette::Background, QBrush(QPixmap("../IMClient/resource/pictures/login_bg.jpg")));
this->setPalette(palette);
m_pLabelUserName = new QLabel(this);
m_pLabelUserName->move(120, 80);
m_pLabelUserName->setText(QStringLiteral("用户名:"));
m_pLineEditName = new QLineEdit(this);
m_pLineEditName->move(190, 80);
m_pLineEditName->setPlaceholderText(QStringLiteral("请输入用户名"));
m_pLabelpassword = new QLabel(this);
m_pLabelpassword->move(120, 130);
m_pLabelpassword->setText(QStringLiteral("密码:"));
m_pLineEditPwd = new QLineEdit(this);
m_pLineEditPwd->move(190, 130);
m_pLineEditPwd->setPlaceholderText(QStringLiteral("请输入密码"));
m_pLineEditPwd->setEchoMode(QLineEdit::Password);
m_pButtonLogin = new QPushButton(this);
m_pButtonLogin->move(150, 200);
m_pButtonLogin->setText(QStringLiteral("登录"));
connect(m_pButtonLogin, &QPushButton::clicked, this, &CLogin::Login);
}
CLogin::~CLogin()
{
delete this;
}
bool CLogin::Login()
{
QString username = m_pLineEditName->text();
QString password = m_pLineEditPwd->text();
printf("%s login success\n", username.toStdString().data());
return true;
}