C++備忘-QT(4),備忘-qt
使用者登入-creator編程
畫好以下介面:
編譯時間,會自動產生如下內容的標頭檔ui_mainwindow.h
/********************************************************************************
** Form generated from reading UI file'mainwindow.ui'
**
** Created by: Qt User Interface Compilerversion 5.4.0
**
** WARNING! All changes made in this filewill be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QWidget *centralWidget;
QVBoxLayout *verticalLayout;
QHBoxLayout *horizontalLayout_2;
QLabel *userLb;
QLineEdit *userNameLe;
QHBoxLayout *horizontalLayout_3;
...
編寫cpp檔案
mainwindow.cpp
#include"mainwindow.h"
#include"ui_mainwindow.h"
#include<QMessageBox>
MainWindow::MainWindow(QWidget*parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
voidMainWindow::on_okBt_clicked()
{
if(ui->userNameLe->text().compare("user")==0 &&ui->passWordLe->text().compare("123")==0)
{
QMessageBox::information(this, "登入系統","密碼正確",
QMessageBox::Ok);
close();
}
else
{
QMessageBox::information(this, "登入系統","密碼錯誤,請重新輸入!",
QMessageBox::Ok);
}
}
void MainWindow::on_quitBt_clicked()
{
if (QMessageBox::information(this, "登入系統", "退出系統否?",
QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes){
close();
}
}
本部落格所有內容是原創,如果轉載請註明來源http://blog.csdn.net/myhaspl/
setupUi將自動產生一些connect關係,其中有以下語句
QMetaObject::connectSlotsByName(MainWindow);
將按訊號名稱自動產生槽
比如
connect(quitBt,SIGNAL(clicked()),this,SLOT(on_quitBt_clicked));