1.浠涔堟槸QApplication?
鏂囨。璇存槑:The QApplication class manages the GUI application's control flow and main settings.
Application綾葷鐞咷UI紼嬪簭鎺у埗嫻佸拰涓昏鍙傛暟璁劇疆
QApplication緇ф壙浜嶲CoreApplication銆傚悗鑰呮彁渚涗簡鎺у埗鍙扮▼搴忕殑浜嬩歡嫻?br>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鏄疩T涓殑鏍囩鎺т歡瀹冨叿鏈夋帶浠剁殑涓鑸睘鎬ф瘮濡傝緗ぇ灝弒etBaseSite,璁劇疆瀵歸綈鏍煎紡錛屽綋鐒朵篃鍙互璁劇疆鑳屾櫙鑹叉垨鑰呭浘鐗?榪欓兘鏄氳繃QPalette璋冭壊鏉挎潵瀹炵幇鐨?br>闇瑕佽鏄庣殑鏄疩T涓殑鎺т歡鏂囨湰鍙互浣跨敤Html璇硶鐨勬枃鏈潵鎿嶄綔鍏蜂綋濡備笂銆?br>閭h榪欎釜鍔熻兘姣旇緝緇欏姏錛?br>3.閭d箞浠涔堟槸QPalette?
QPalette璐熻矗鎺у埗鎺т歡鐘舵佺殑棰滆壊緇?娉ㄦ剰鏄帶浠剁姸鎬併?br>閭d箞瀵逛竴涓帶浠舵瘡涓姸鎬佺殑棰滆壊閮藉彲浠ユ槸涓嶄竴鏍風(fēng)殑鍜?br>鑷充簬QPalette鐨勮緇嗗姛鑳藉拰浣跨敤鏂規(guī)硶浠ュ悗闇瑕佺殑鏃跺欏啀鐪嬪惂
4.鍩烘湰鐨勪俊鍙烽摼鎺ヤ嬌鐢ㄤ緥瀛?br>
#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.涓涓鏉傜偣鐨勪緥瀛?br>
#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();
}
緙栬瘧榪愯鍙互鐪嬪嚭QWidget涓粯璁ょ殑甯冨眬綆$悊鍣ㄦ槸绔栫洿鍚戜笅鎺掑垪鐨?br>鍦≦T涓彲浠ラ氳繃setWindowIcon鏉ヨ緗獥浣撳浘鏍?br>閫氳繃setWindowTitle璁劇疆紿椾綋鏍囬
6.鍔犱笂甯冨眬綆$悊鍣ㄥ拰淇″彿榪炴帴鐨勮瘽浠g爜澶ц嚧搴旇鏄繖涓牱瀛?br>
#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();
}
闇瑕佽鏄庣殑鏄湪榪欓噷QSlider,QPinBox鎺т歡鏄簰鍔?br>緙栬瘧紼嬪簭騫惰繍琛岀晫闈㈠涓?

榪欐槸鍏充簬QT鐨勭鍏瘒絎旇
鎬葷粨涓嬪惂
QT鍔熻兘榪樻槸寰堝己澶ц創(chuàng)蹇冪殑
姣旇緝瀹規(guī)槗涓婃墜
涓嶈繃鏈?鐐規(guī)垜鎰熻涓嶅ぇ鑸掓湇鐨勫湴鏂規(guī)槸瀵硅繖涓彉閲忓懡鍚嶆牸寮忔湁鐐逛笉澶у枩嬈?br>姣斿setValue鎴戝枩嬈㈠啓鎴怱etValue.
浠呮鑰屽凡

]]>