Qt第一個應用,HelloWorld


    下面爲主要代碼片斷
git

HelloWorldMainWindow.h

#ifndef HELLOWORLDMAINWINDOW_H
#define HELLOWORLDMAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class HelloWorldMainWindow;
}

class HelloWorldMainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    //槽函數
    void wosaySlot();

private:
    Ui::HelloWorldMainWindow *ui;
};

#endif // HELLOWORLDMAINWINDOW_H

HelloWorldMainWindow.cpp

#include "HelloWorldMainWindow.h"
#include "ui_HelloWorldMainWindow.h"

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

    /*
     * 將文字居中
     */
    ui->textLabel->setAlignment(Qt::AlignCenter);
    ui->wosayLabel->setAlignment(Qt::AlignCenter);

    /*
     * 將按鈕的clicked信號與HelloWorldMainWindow類中的槽函數進行鏈接
     */
    connect(ui->helloPushButton, SIGNAL(clicked(bool)),
            this, SLOT(wosaySlot()));
}

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

void HelloWorldMainWindow::wosaySlot()
{
    /*
     * 促發按鈕事件,調用此槽函數
     */
    ui->wosayLabel->setText("world");
}

main.cpp

#include "HelloWorldMainWindow.h"
#include <QApplication>
#include <QTextCodec>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    /*
     * Qt5如下設置編碼
     */
#if QT_VERSION < 0x050000
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
    QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
#endif

    HelloWorldMainWindow w;
    w.show();

    return a.exec();
}


運行結果:函數

點擊按鈕後效果:ui

源代碼:this

http://git.oschina.net/gateslu1986/HelloWorld
編碼

相關文章
相關標籤/搜索