程式碼示例:
//////////////////////dialog.cpp
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->lineEditPassword->setEchoMode(QLineEdit::Password);
}
Dialog::~Dialog()
delete ui;
void Dialog::on_pushButtonLogin_clicked()
//獲取資料
m_strUser = ui->lineEditUser->text();
m_strPassword = ui->lineEditPassword->text();
//校驗資料
if(m_strUser.isEmpty() || m_strPassword.isEmpty())
QMessageBox::information(this, QString(tr("Tips"))
,QString(tr("User name or Password is empty!")));
else
//正確的使用者名稱和密碼
this->accept();
void Dialog::on_pushButtonQuit_clicked()
this->reject();
////////////////////////////////mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
//
Dialog* pDlgLogin = new Dialog(NULL);
//以模式對話方塊顯示
int nResult = pDlgLogin->exec();
if(nResult != QDialog::Accepted)
//登入錯誤,退出
exit(1);
//正常登入
//讀取使用者名稱和密碼
m_strUser = pDlgLogin->m_strUser;
m_Password = pDlgLogin->m_strPassword;
//設定標籤為使用者名稱
ui->labelUserName->setText(m_strUser);
//繫結退出訊號到關閉槽函式
connect(ui->action_Quit, SIGNAL(triggered()), this, SLOT(close()));
//將主視窗移到指定位置
this->move(QPoint(0,0));
MainWindow::~MainWindow()
程式碼示例:
//////////////////////dialog.cpp
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->lineEditPassword->setEchoMode(QLineEdit::Password);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButtonLogin_clicked()
{
//獲取資料
m_strUser = ui->lineEditUser->text();
m_strPassword = ui->lineEditPassword->text();
//校驗資料
if(m_strUser.isEmpty() || m_strPassword.isEmpty())
{
QMessageBox::information(this, QString(tr("Tips"))
,QString(tr("User name or Password is empty!")));
}
else
{
//正確的使用者名稱和密碼
this->accept();
}
}
void Dialog::on_pushButtonQuit_clicked()
{
this->reject();
}
////////////////////////////////mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//
ui->setupUi(this);
//
Dialog* pDlgLogin = new Dialog(NULL);
//以模式對話方塊顯示
int nResult = pDlgLogin->exec();
if(nResult != QDialog::Accepted)
{
//登入錯誤,退出
exit(1);
}
//正常登入
//讀取使用者名稱和密碼
m_strUser = pDlgLogin->m_strUser;
m_Password = pDlgLogin->m_strPassword;
//設定標籤為使用者名稱
ui->labelUserName->setText(m_strUser);
//繫結退出訊號到關閉槽函式
connect(ui->action_Quit, SIGNAL(triggered()), this, SLOT(close()));
//將主視窗移到指定位置
this->move(QPoint(0,0));
//
}
MainWindow::~MainWindow()
{
delete ui;
}