信号槽操作案例

场景:在应用主界面,点击"按钮",弹出用户登陆对话框,用户输入用户名点击登陆,在主界面显示用户输入的用户名.

1:新建Qt Widgets Application,名称:signal-t02

2:在主应用界面(mainwindow.ui)增加一个Label与PushButton,objectName分别为:unameLabel与showLoginDialogBtn.

3:  增加一个Qt设计师界面类->Dialog without Buttons (类名ShowLoginDialog)

在对话框中增加:LineEdit(unameEdit)与PushButton(loginBtn)

4:mainwindow.ui

右键"Show Login Dialog Button",转到槽选择clicked,
mainwindow.cpp代码如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "showlogindialog.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

//Show Login Dialog
void MainWindow::on_showLoginDialogBtn_clicked()
{
    ShowLoginDialog * dlg = new ShowLoginDialog(this);
    
    dlg->show();
}

//Show username
void MainWindow::showUsername(QVariant str)
{
    ui->unameLabel->setText(str.toString());
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QVariant>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_showLoginDialogBtn_clicked();
    void showUsername(QVariant str);    //show username
    
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
此时运行程序,点击"Show Login Dialog Button"将弹出登陆信息输入对话框.

5:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QVariant>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_showLoginDialogBtn_clicked();
    void showUsername(QVariant str);    //show username

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "showlogindialog.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

//Show Login Dialog
void MainWindow::on_showLoginDialogBtn_clicked()
{
    ShowLoginDialog * dlg = new ShowLoginDialog(this);

    connect(dlg,&ShowLoginDialog::unameEditChanged,this,&MainWindow::showUsername);

    dlg->show();
}

//Show username
void MainWindow::showUsername(QVariant str)
{
    ui->unameLabel->setText(str.toString());
}

 

showlogindialog.h

#ifndef SHOWLOGINDIALOG_H
#define SHOWLOGINDIALOG_H

#include <QDialog>

namespace Ui {
class ShowLoginDialog;
}

class ShowLoginDialog : public QDialog
{
    Q_OBJECT

public:
    explicit ShowLoginDialog(QWidget *parent = 0);
    ~ShowLoginDialog();

private slots:
    void on_loginBtn_clicked();

    //定义信号
    signals:
        void unameEditChanged(QString str);

private:
    Ui::ShowLoginDialog *ui;
};

#endif // SHOWLOGINDIALOG_H

 

showlogindialog.cpp
#include "showlogindialog.h"
#include "ui_showlogindialog.h"

ShowLoginDialog::ShowLoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ShowLoginDialog)
{
    ui->setupUi(this);
}

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

//show login dialog
void ShowLoginDialog::on_loginBtn_clicked()
{
    QString str = ui->unameEdit->text();

    emit unameEditChanged(str);

    accept();
}

 

posted @ 2015-08-22 16:27  yshy  阅读(309)  评论(0编辑  收藏  举报