Qt -- 输出hello world

界面设计:

.h中定义slots

#include 
#include "ui_QtGuiApplication2.h"

class QtGuiApplication2 : public QMainWindow
{
    Q_OBJECT

public:
    QtGuiApplication2(QWidget *parent = Q_NULLPTR);
    void init();

private:
    Ui::QtGuiApplication2Class ui;

private slots:
    void onshow();
};

.cpp中实现

#include "QtGuiApplication2.h"
#include "QMessageBox"

QtGuiApplication2::QtGuiApplication2(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    init();
}

void QtGuiApplication2::init()
{
    connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(onshow()));
}

void QtGuiApplication2::onshow()
{
    QMessageBox msgBox;
    msgBox.setText("Hello world.");
    msgBox.exec();
}

实现结果:

总结:
Qt邮槽总体上封装感觉类似mfc的消息事件。如下

BEGIN_MESSAGE_MAP(CSetAdapter, CDialog)
ON_BN_CLICKED(IDOK, &CSetAdapter::OnBnClickedOk)
ON_BN_CLICKED(IDC_BUTTON1, &CSetAdapter::OnBnClickedButton1)
ON_NOTIFY(NM_CLICK, IDC_LIST_ADAPTER, &CSetAdapter::OnNMClickListAdapter)
ON_CBN_SELCHANGE(IDC_COMBOX_CREATEID, &CSetAdapter::OnSelComChange)
ON_CBN_DROPDOWN(IDC_COMBOX_CREATEID, &CSetAdapter::OnDropDown)
ON_CBN_CLOSEUP(IDC_COMBOX_CREATEID, &CSetAdapter::OnCloseUp)
ON_BN_CLICKED(IDC_BUTTON2, &CSetAdapter::OnBnClickedButton2)
ON_BN_CLICKED(IDCANCEL, &CSetAdapter::OnBnClickedCancel)
END_MESSAGE_MAP()

connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(onshow()));

你可能感兴趣的:(Qt学习之道)