QT練手小專案一
最近在做MFC方面的工作,但是覺得QT不能夠扔下,所以空閒的時候做一些QT方面的練手小專案,就當複習了。程式碼量很小,易於理解。我會把整個程式碼放在部落格最後面。
我們先來看一看整個頁面,整個頁面是比較簡單的,有五個按鈕和兩個文字框。這只是一個空的介面,並沒有做任何功能。
我們先來看看MainWIndow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextEdit>
#include <QPushButton>
#include <QDialog>
class MainWindow : public QDialog
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QTextEdit *m_topPage;
QTextEdit *m_belowPage;
QTextEdit *m_rightPage;
QPushButton *m_fontButton;
QPushButton *m_sizeButton;
QPushButton *m_logButton;
QPushButton *m_closeButton;
QPushButton *m_sendButton;
};
#endif // MAINWINDOW_H
整個頁面主要是用對話方塊的形式做出來的,所以繼承的是QDialog.
首先在類裡面我們定義了對話方塊的基本部件,包括三個文字框,五個按鈕,非常簡單。
下面我們就來看看MainWindow.cpp
我首先對基本框非常簡單的設定,就是設定大小和新增標題。
resize(800, 600);
setWindowTitle(tr("Dialog Exercise"));
然後是初始化文字框和按鈕。因為想看看所有的字型長什麼樣,所以每個按鈕我都使用了不同的字型。
m_topPage = new QTextEdit();
m_belowPage = new QTextEdit();
m_rightPage = new QTextEdit();
//下面五個是五個按鈕初始化
m_fontButton = new QPushButton(tr("Font"));
m_fontButton->setFont(QFont("Times", 18,
QFont::Black, true));
m_sizeButton = new QPushButton(tr("Size"));
m_sizeButton->setFont(QFont("Times", 18,
QFont::Bold, true));
m_logButton = new QPushButton(tr("Log"));
m_logButton->setFont(QFont("Times", 18,
QFont::Light, true));
m_closeButton = new QPushButton(tr("Close"));
m_closeButton->setFont(QFont("Times", 18,
QFont::Normal, true));
m_sendButton = new QPushButton(tr("Send"));
m_sendButton->setFont(QFont("Times", 18,
QFont::DemiBold, true));
在這裡我做了非常簡單的一個connect,就是close按鈕做了一個點選退出介面功能。其他的按鈕就沒有做SLOT
connect(m_closeButton, SIGNAL(clicked()),
qApp, SLOT(quit()));
接下來是排列整個介面,我先排列的是左邊的框架。因為我沒排列好一部分框架就做了一個顯示去檢視效果,所以大家可以看到我註釋了好幾個setLayout()函式。
//橫向排列好三個按鈕
QHBoxLayout *topButtonLayout = new QHBoxLayout();
topButtonLayout->addWidget(m_fontButton);
topButtonLayout->addWidget(m_sizeButton);
topButtonLayout->addWidget(m_logButton);
//setLayout(topButtonLayout);
//縱向排列好按鈕和字元介面
QVBoxLayout *topTextLayout = new QVBoxLayout();
topTextLayout->addWidget(m_topPage);
topTextLayout->addLayout(topButtonLayout);
//setLayout(topTextLayout);
//橫向排列下面的兩個按鈕
QHBoxLayout *belowButtonLayout = new QHBoxLayout();
belowButtonLayout->addWidget(m_closeButton);
belowButtonLayout->addWidget(m_sendButton);
//縱向排列好下面的字元介面
QVBoxLayout *belowTextLayout = new QVBoxLayout();
belowTextLayout->addWidget(m_belowPage);
belowTextLayout->addLayout(belowButtonLayout);
//縱向排列好左邊的介面
QVBoxLayout *leftLayout = new QVBoxLayout();
leftLayout->addLayout(topTextLayout);
leftLayout->addLayout(belowTextLayout);
//setLayout(leftLayout);
左邊的框架排列好以後,因為右邊只有一個文字框,所以我們直接排列整個框架。直接排列的話,是對半分整個介面,不好看。所在排列之前我對右邊的文字框進行了寬度設定。
//右邊的字元介面好像有點大,調整一下寬度
m_rightPage->setMaximumWidth(300);
//橫向排列好整個介面
QHBoxLayout *mainLayout = new QHBoxLayout();
mainLayout->addLayout(leftLayout);
mainLayout->addWidget(m_rightPage);
setLayout(mainLayout);
主要程式碼到這裡就已經完成了。也就是說整個介面我們已經排列好了。如果想要新增其他的功能可以在類裡面宣告幾個SLOT,做connect。
下面我把我的整個原始碼放在最下面,供初學者使用。
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextEdit>
#include <QPushButton>
#include <QDialog>
class MainWindow : public QDialog
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QTextEdit *m_topPage;
QTextEdit *m_belowPage;
QTextEdit *m_rightPage;
QPushButton *m_fontButton;
QPushButton *m_sizeButton;
QPushButton *m_logButton;
QPushButton *m_closeButton;
QPushButton *m_sendButton;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QFont>
#include <QHBoxLayout>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent) :
QDialog(parent)
{
//設定介面大小以及擡頭
resize(800, 600);
setWindowTitle(tr("Dialog Exercise"));
//這三個是字元介面初始化
m_topPage = new QTextEdit();
m_belowPage = new QTextEdit();
m_rightPage = new QTextEdit();
//下面五個是五個按鈕初始化
m_fontButton = new QPushButton(tr("Font"));
m_fontButton->setFont(QFont("Times", 18,
QFont::Black, true));
m_sizeButton = new QPushButton(tr("Size"));
m_sizeButton->setFont(QFont("Times", 18,
QFont::Bold, true));
m_logButton = new QPushButton(tr("Log"));
m_logButton->setFont(QFont("Times", 18,
QFont::Light, true));
m_closeButton = new QPushButton(tr("Close"));
m_closeButton->setFont(QFont("Times", 18,
QFont::Normal, true));
//做一個關閉按鈕
connect(m_closeButton, SIGNAL(clicked()),
qApp, SLOT(quit()));
m_sendButton = new QPushButton(tr("Send"));
m_sendButton->setFont(QFont("Times", 18,
QFont::DemiBold, true));
//橫向排列好三個按鈕
QHBoxLayout *topButtonLayout = new QHBoxLayout();
topButtonLayout->addWidget(m_fontButton);
topButtonLayout->addWidget(m_sizeButton);
topButtonLayout->addWidget(m_logButton);
//setLayout(topButtonLayout);
//縱向排列好按鈕和字元介面
QVBoxLayout *topTextLayout = new QVBoxLayout();
topTextLayout->addWidget(m_topPage);
topTextLayout->addLayout(topButtonLayout);
//setLayout(topTextLayout);
//橫向排列下面的兩個按鈕
QHBoxLayout *belowButtonLayout = new QHBoxLayout();
belowButtonLayout->addWidget(m_closeButton);
belowButtonLayout->addWidget(m_sendButton);
//縱向排列好下面的字元介面
QVBoxLayout *belowTextLayout = new QVBoxLayout();
belowTextLayout->addWidget(m_belowPage);
belowTextLayout->addLayout(belowButtonLayout);
//縱向排列好左邊的介面
QVBoxLayout *leftLayout = new QVBoxLayout();
leftLayout->addLayout(topTextLayout);
leftLayout->addLayout(belowTextLayout);
//setLayout(leftLayout);
//右邊的字元介面好像有點大,調整一下寬度
m_rightPage->setMaximumWidth(300);
//橫向排列好整個介面
QHBoxLayout *mainLayout = new QHBoxLayout();
mainLayout->addLayout(leftLayout);
mainLayout->addWidget(m_rightPage);
setLayout(mainLayout);
}
MainWindow::~MainWindow()
{
}