在分享是之前,先看截图,用事实说话,有图有真相!
通过点击底部栏的新浪、人人按钮来进行3个界面的相互切换,进行用户删除时,自定义的QDialog用于和用户交互,通过exec()可进入阻塞状态!详情请查看(Qt模态/非模态),translateLanguage()主要用于多语化的切换。。。
在用Qt实现界面的时候,界面切换必不可少,如何实现呢?今天在这里分享一下自己的一些小心得!
实现界面切换主要用到:QStackedLayout、QWidget通过QStackedLayout的setCurrentWidget()来显示当前界面!
当然还有其它的办法,好了,代码如下:
#include "login_dialog.h"
#include "util.h"
#include "account_item.h"
LoginDialog::LoginDialog(QWidget *parent)
: QDialog(parent)
{
this->resize(370, 275);
this->setWindowFlags(Qt::FramelessWindowHint |
Qt::Dialog);
mouse_press = false;
login = SAFE;
sina_login = SINA;
msg_box = new MsgBox(this);
this->initRenRenWidget();
this->initSafeWidget();
this->initAuthorizeWidget();
this->initSinaWidget();
main_stacked_layout = new
QStackedLayout();
stacked_layout = new QStackedLayout();
stacked_layout->addWidget(safe_widget);
stacked_layout->addWidget(renren_widget);
stacked_layout->setCurrentWidget(safe_widget);
safe_renren_widget = new QWidget(this);
title_label = new QLabel();
account_label = new QLabel();
password_label = new QLabel();
other_account_label = new QLabel();
close_button = new PushButton();
sina_button = new QPushButton();
renren_button = new QPushButton();
register_button = new QPushButton();
forgot_button = new QPushButton();
login_button = new QPushButton();
password_line_edit = new QLineEdit();
remember_check_box = new QCheckBox();
auto_login_check_box = new QCheckBox();
title_label->setAlignment(Qt::AlignBottom);
register_button->setCursor(Qt::PointingHandCursor);
forgot_button->setCursor(Qt::PointingHandCursor);
QPixmap
sina_pixmap(":/loginDialog/sina");
sina_button->setIcon(sina_pixmap);
sina_button->setIconSize(QSize(20, 20));
sina_button->setFixedSize(25, 22);
QPixmap
renren_pixmap(":/loginDialog/renren");
renren_button->setIcon(renren_pixmap);
renren_button->setIconSize(QSize(20,
20));
renren_button->setFixedSize(25, 22);
sina_button->setStyleSheet("border-radius:2px;
border:1px solid rgb(180, 180, 180);");
renren_button->setStyleSheet("border-radius:2px;
border:1px solid rgb(180, 180, 180);");
account_combo_box = new QComboBox();
list_widget = new QListWidget();
account_combo_box->setModel(list_widget->model());
account_combo_box->setView(list_widget);
account_combo_box->setEditable(true);
for(int i=0; i<3; i++)
{
AccountItem *account_item = new
AccountItem();
account_item->setAccountNumber(QString("safe_")
+ QString::number(i, 10) + QString("@sina.com"));
connect(account_item,
SIGNAL(showAccount(QString)), this,
SLOT(showAccount(QString)));
connect(account_item,
SIGNAL(removeAccount(QString)), this,
SLOT(removeAccount(QString)));
QListWidgetItem *list_item =
new QListWidgetItem(list_widget);
list_widget->setItemWidget(list_item,
account_item);
}
account_label->setScaledContents(true);
password_label->setScaledContents(true);
account_label->setAlignment(Qt::AlignRight);
password_label->setAlignment(Qt::AlignRight);
password_line_edit->setEchoMode(QLineEdit::Password);
account_combo_box->setFixedHeight(25);
password_line_edit->setFixedHeight(25);
close_button->loadPixmap(":/sysButton/close");
title_label->setStyleSheet("color:white");
register_button->setFixedSize(70, 25);
forgot_button->setFixedSize(70, 25);
login_button->setFixedSize(60, 25);
register_button->setStyleSheet("color:green;
background:transparent;");
forgot_button->setStyleSheet("color:green;
background:transparent;");
login_button->setStyleSheet("QPushButton{border-radius:2px;
border: 1px solid rgb(110, 180, 20); background:rgb(110, 190, 10);
color:white;}"
"QPushButton:hover{background:rgb(140,
220, 35);}");
account_combo_box->setStyleSheet("QComboBox{border:1px
solid rgb(180, 180, 180);}"
"QComboBox
QAbstractItemView::item{height:30px;}"
"QComboBox::down-arrow{image:url(:/loginDialog/arrowDown);}"