11.8ui界面——信号与槽

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include 

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

public slots:
    void my_slot();
    void log_slot();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

main.cpp

#include "widget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

widget.cpp
 

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //窗口图标设置
    QMovie *mv = new QMovie(":/pictrue/qq2.gif");
    ui->backgroundlabel->setMovie(mv);
    mv->start();
    ui->backgroundlabel->setScaledContents(true);

    ui->accountlabel->setPixmap(QPixmap(":/pictrue/userName.jpg"));
    ui->accountlabel->setScaledContents(true);

    ui->passwordlabel->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
    ui->passwordlabel->setScaledContents(true);

    ui->passwordlineEdit->setEchoMode(QLineEdit::Password);

    //取消连接
    connect(ui->cancelButton,SIGNAL(clicked()),this,SLOT(my_slot()));
    //登录连接
    connect(ui->loginButton,&QPushButton::clicked,this,&Widget::log_slot);


}
void Widget::my_slot() //取消连接的槽函数
{
    close();
}

void Widget::log_slot() //登录连接的槽函数
{
    if(ui->accountlineEdit->text() == "admin" && ui->passwordlineEdit->text() == "123456")
    {
        qDebug() << "登录成功" << endl;
        close();
    }
    else
    {
        ui->passwordlineEdit->clear();
    }
}

Widget::~Widget()
{
    delete ui;
}

11.8ui界面——信号与槽_第1张图片

你可能感兴趣的:(ui)