1. 程式人生 > >Qt -- 輸出hello world

Qt -- 輸出hello world

介面設計:
這裡寫圖片描述

.h中定義slots

#include <QtWidgets/QMainWindow>
#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()));