LINUX爱好者! 该文章的项目是基于LINUX下的QT。
转载请标明出处!
QT实现简单的页面跳转
以登录功能为例,本项目将默认类作为主界面(main_widgt),创建的新类(login_widget)作登录界面。
#ifndef LOGIN_WIDGET_H
#define LOGIN_WIDGET_H
#include <QObject>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QMessageBox>
class login_widget : public QWidget
{
Q_OBJECT
public:
login_widget();
QLabel *userLabel;
QLabel *pswLabel;
QLineEdit *luserEdit;
QLineEdit *pswEdit;
QPushButton *loginBtn;
QVBoxLayout *layout;
QWidget *mainwidget;
private slots:
void on_lloginBtn_Clicked();
};
#endif // LOGIN_WIDGET_H
#include "login_widget.h"
#include "main_widget.h"
login_widget::login_widget()
{
userLabel = new QLabel;
userLabel->setText(tr("username"));
luserEdit = new QLineEdit;
pswLabel = new QLabel;
pswLabel->setText(tr("password"));
pswEdit = new QLineEdit;
//pswEdit->setEchoMode(QLineEdit::Password);//输入密码时显示*
loginBtn = new QPushButton(tr("Login"));
connect(loginBtn,SIGNAL(clicked(bool)),this,SLOT(on_lloginBtn_Clicked()));
......
}
void login_widget::on_lloginBtn_Clicked()
{
if((this->luserEdit->text().trimmed()==tr("a"))&&(this->pswEdit->text().trimmed()==tr("a")))
{
this->hide();//密码正确登陆成功跳到主页面
mainwidget = new main_widget;
mainwidget->show();
}
else if (this->luserEdit->text().trimmed()==NULL)
{
QMessageBox::warning(this,tr("ERROR"),tr("please input username!"),QMessageBox::Yes);
this->luserEdit->setFocus();//鼠标回到用户名栏
}
else
{
QMessageBox::warning(this,tr("WARNING"),tr("username or password wrong!"),QMessageBox::Yes);
this->pswEdit->clear();//清除密码
this->pswEdit->setFocus();//鼠标回到密码栏
}
}
void login_widget::on_lregisterBtn_Clicked()
{
this->hide();
registerwidget = new register_widget;
registerwidget->show();
}
#ifndef MAIN_WIDGET_H
#define MAIN_WIDGET_H
#include <QWidget>
class main_widget : public QWidget
{
Q_OBJECT
public:
main_widget(QWidget *parent = 0);
~main_widget();
QLabel *welcomeLabel;
QVBoxLayout *layout;
};
#endif // MAIN_WIDGET_H
#include "main_widget.h"
#include "login_widget.h"
#include <QtSql>//数据库
main_widget::main_widget(QWidget *parent)
: QWidget(parent)
{
welcomeLabel = new QLabel;
welcomeLabel->setText(tr("welcome to back!"));
......
}
main_widget::~main_widget()
{
}
#include "login_widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
login_widget w;//设置启动的默认窗口
w.show();
return a.exec();
}
成功!
如有疑问请提出一起探讨,谢谢!