用户登录-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();
}
}
setupUi将自动生成一些connect关系,其中有以下语句
QMetaObject::connectSlotsByName(MainWindow);
将按信号名称自动生成槽
比如
connect(quitBt,SIGNAL(clicked()),this,SLOT(on_quitBt_clicked));
原文地址:http://blog.csdn.net/myhaspl/article/details/44004051