接上文,這篇學(xué)學(xué)QT中基本控件的使用和QApplication對(duì)象
1.什么是QApplication?
文檔說(shuō)明:
The QApplication class manages the GUI application's control flow and main settings.
Application類(lèi)管理GUI程序控制流和主要參數(shù)設(shè)置
QApplication繼承于QCoreApplication。后者提供了控制臺(tái)程序的事件流
2.基本控件的使用例子:
#include <QApplication>
#include <QLabel>
#include <QPalette>
#define QT_HTML
QLabel* label = NULL;
void initlabel()
{
#ifndef QT_HTML
label = new QLabel("Hello Qt!");
#else
label = new QLabel("<h2><i>Hello</i><font color=red>Qt!</font></h2>");
#endif
//! set size
label->setBaseSize(64,48);
//! set alignment
label->setAlignment(Qt::AlignHCenter);
//! sht background color
QColor bk(100,100,125);
QPalette palette(bk);
label->setPalette(palette);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setApplicationName("QT Test");
initlabel();
label->show();
return app.exec();
}
QLabel是QT中的標(biāo)簽控件它具有控件的一般屬性比如設(shè)置大小setBaseSite,設(shè)置對(duì)齊格式,當(dāng)然也可以設(shè)置背景色或者圖片-這都是通過(guò)QPalette調(diào)色板來(lái)實(shí)現(xiàn)的
需要說(shuō)明的是QT中的控件文本可以使用Html語(yǔ)法的文本來(lái)操作具體如上。
那覺(jué)這個(gè)功能比較給力!
3.那么什么是QPalette?
QPalette負(fù)責(zé)控制控件狀態(tài)的顏色組-注意是控件狀態(tài)。
那么對(duì)一個(gè)控件每個(gè)狀態(tài)的顏色都可以是不一樣的咯
至于QPalette的詳細(xì)功能和使用方法以后需要的時(shí)候再看吧
4.基本的信號(hào)鏈接使用例子
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton *button = new QPushButton("Quit");
//! when click button, app exit.
QObject::connect(button, SIGNAL(clicked()),&app, SLOT(quit()));
button->show();
return app.exec();
}
5.一個(gè)復(fù)雜點(diǎn)的例子
#include <QApplication>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
#include <QIcon>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget* widget = new QWidget;
QIcon icon("config.png");
widget->setWindowIcon(icon);
widget->setWindowTitle("Using QT");
QSlider* slider = new QSlider(widget);
slider->setRange(0,99);
QSpinBox* spinbox = new QSpinBox(widget);
spinbox->setRange(0,99);
widget->show();
return app.exec();
}
編譯運(yùn)行可以看出QWidget中默認(rèn)的布局管理器是豎直向下排列的
在QT中可以通過(guò)setWindowIcon來(lái)設(shè)置窗體圖標(biāo)
通過(guò)setWindowTitle設(shè)置窗體標(biāo)題
6.加上布局管理器和信號(hào)連接的話(huà)代碼大致應(yīng)該是這個(gè)樣子
#include <QApplication>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
#include <QIcon>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget* widget = new QWidget;
QIcon icon("config.png");
widget->setWindowIcon(icon);
widget->setWindowTitle("Using QT");
QSlider* slider = new QSlider(widget);
slider->setRange(0,99);
QSpinBox* spinbox = new QSpinBox(widget);
spinbox->setRange(0,99);
QHBoxLayout* layout = new QHBoxLayout;
layout->addWidget(spinbox);
//! adjust slider's direction
slider->setOrientation(Qt::Horizontal);
layout->addWidget(slider);
spinbox->setValue(28);
//! connect signals and slots
QObject::connect(spinbox, SIGNAL(valueChanged(int)),slider,SLOT(setValue(int)));
QObject::connect(slider,SIGNAL(valueChanged(int)),spinbox,SLOT(setValue(int)));
widget->setLayout(layout);
widget->show();
return app.exec();
}
需要說(shuō)明的是在這里QSlider,QPinBox控件是互動(dòng)
編譯程序并運(yùn)行界面如下:

這是關(guān)于QT的第六篇筆記
總結(jié)下吧
QT功能還是很強(qiáng)大貼心的
比較容易上手
不過(guò)有2點(diǎn)我感覺(jué)不大舒服的地方是對(duì)這個(gè)變量命名格式有點(diǎn)不大喜歡
比如setValue我喜歡寫(xiě)成SetValue.
僅此而已