新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Qt圖形編程基礎(chǔ)之:實(shí)驗(yàn)內(nèi)容——使用Qt編寫“Hello,World”程序

Qt圖形編程基礎(chǔ)之:實(shí)驗(yàn)內(nèi)容——使用Qt編寫“Hello,World”程序

作者: 時(shí)間:2013-09-13 來源:網(wǎng)絡(luò) 收藏

本文引用地址:http://butianyuan.cn/article/257102.htm

/****************************************************************************

**以下是hello.h的代碼

****************************************************************************/

#ifndefHELLO_H

#defineHELLO_H

#includeqvariant.h>

#includeqwidget.h>

classQVBoxLayout;

classQHBoxLayout;

classQGridLayout;

classHello:publicQWidget

{

Q_OBJECT

public:

Hello(QWidget*parent=0,constchar*name=0,WFlagsfl=0);

~Hello();

/*以下是手動(dòng)添加的代碼*/

signals:

voidclicked();

protected:

voidmouseReleaseEvent(QMouseEvent*);

voidpaintEvent(QPaintEvent*);

privateslots:

voidanimate();

private:

QStringt;

intb;

};

#endif//HELLO_H

/****************************************************************************

**以下是hello.cpp源代碼

****************************************************************************/

#includehello.h

#includeqlayout.h>

#includeqvariant.h>

#includeqtooltip.h>

#includeqwhatsthis.h>

#includeqpushbutton.h>

#includeqtimer.h>

#includeqpainter.h>

#includeqpixmap.h>

/*

*ConstructsaHellowhichisachildof'parent',withthe

*name'name'andwidgetflagssetto'f'

*/

Hello::Hello(QWidget*parent,constchar*name,WFlagsfl)

:QWidget(parent,name,fl)

{

if(!name)

setName(Hello);

resize(240,320);

setMinimumSize(QSize(240,320));

setMaximumSize(QSize(240,320));

setSizeIncrement(QSize(240,320));

setBaseSize(QSize(240,320));

QPalettepal;

QColorGroupcg;

cg.setColor(QColorGroup::Foreground,black);

cg.setColor(QColorGroup::Button,QColor(192,192,192));

cg.setColor(QColorGroup::Light,white);

cg.setColor(QColorGroup::Midlight,QColor(223,223,223));

cg.setColor(QColorGroup::Dark,QColor(96,96,96));

cg.setColor(QColorGroup::Mid,QColor(128,128,128));

cg.setColor(QColorGroup::Text,black);

cg.setColor(QColorGroup::BrightText,white);

cg.setColor(QColorGroup::ButtonText,black);

cg.setColor(QColorGroup::Base,white);

cg.setColor(QColorGroup::Background,white);

cg.setColor(QColorGroup::Shadow,black);

cg.setColor(QColorGroup::Highlight,black);

cg.setColor(QColorGroup::HighlightedText,white);

pal.setActive(cg);

cg.setColor(QColorGroup::Foreground,black);

cg.setColor(QColorGroup::Button,QColor(192,192,192));

cg.setColor(QColorGroup::Light,white);

cg.setColor(QColorGroup::Midlight,QColor(220,220,220));

cg.setColor(QColorGroup::Dark,QColor(96,96,96));

cg.setColor(QColorGroup::Mid,QColor(128,128,128));

cg.setColor(QColorGroup::Text,black);

cg.setColor(QColorGroup::BrightText,white);

cg.setColor(QColorGroup::ButtonText,black);

cg.setColor(QColorGroup::Base,white);

cg.setColor(QColorGroup::Background,white);

cg.setColor(QColorGroup::Shadow,black);

cg.setColor(QColorGroup::Highlight,black);

cg.setColor(QColorGroup::HighlightedText,white);

pal.setInactive(cg);

cg.setColor(QColorGroup::Foreground,QColor(128,128,128));

cg.setColor(QColorGroup::Button,QColor(192,192,192));

cg.setColor(QColorGroup::Light,white);

cg.setColor(QColorGroup::Midlight,QColor(220,220,220));

cg.setColor(QColorGroup::Dark,QColor(96,96,96));

cg.setColor(QColorGroup::Mid,QColor(128,128,128));

cg.setColor(QColorGroup::Text,black);

cg.setColor(QColorGroup::BrightText,white);

cg.setColor(QColorGroup::ButtonText,QColor(128,128,128));

cg.setColor(QColorGroup::Base,white);

cg.setColor(QColorGroup::Background,white);

cg.setColor(QColorGroup::Shadow,black);

cg.setColor(QColorGroup::Highlight,black);

cg.setColor(QColorGroup::HighlightedText,white);

pal.setDisabled(cg);

setPalette(pal);

QFontf(font());

f.setFamily(adobe-helvetica);

f.setPointSize(29);

f.setBold(TRUE);

setFont(f);

setCaption(tr());

/*以下是手動(dòng)添加的代碼*/

t=Hello,World;

b=0;

QTimer*timer=newQTimer(this);

connect(timer,SIGNAL(timeout()),SLOT(animate()));

timer->start(40);

}

/*

*Destroystheobjectandfreesanyallocatedresources

*/

Hello::~Hello()

{

}

/*以下至結(jié)尾是手動(dòng)添加的代碼*/

voidHello::animate()

{

b=(b+1)15;

repaint(FALSE);

}

/*

HandlesmousebuttonreleaseeventsfortheHellowidget.

Weemittheclicked()signalwhenthemouseisreleasedinside

thewidget.

*/

voidHello::mouseReleaseEvent(QMouseEvent*e)

{

if(rect().contains(e->pos()))

emitclicked();

}

/*HandlespainteventsfortheHellowidget.

Flicker-freeupdate.Thetextisfirstdrawninthepixmapandthe

pixmapisthenbltedtothescreen.

*/

voidHello::paintEvent(QPaintEvent*)

{

staticintsin_tbl[16]={0,38,71,92,100,92,

71,38,0,-38,-71,-92,-100,-92,-71,-38};

if(t.isEmpty())

eturn;

/*1:Computesomesizes,positionsetc.*/

QFontMetricsfm=fontMetrics();

intw=fm.width(t)+20;

inth=fm.height()*2;

intpmx=width()/2-w/2;

intpmy=height()/2-h/2;

/*2:Createthepixmapandfillitwiththewidgetsbackground*/

QPixmappm(w,h);

pm.fill(this,pmx,pmy);

/*3:Paintthepixmap.Coolwaveeffect*/

QPainterp;

intx=10;

inty=h/2+fm.descent();

inti=0;

p.begin(pm);

p.setFont(font());

while(!t[i].isNull())

{

nti16=(b+i)15;

.setPen(QColor((15-i16)*16,255,255,QColor::Hsv));

wText(x,y-sin_tbl[i16]*h/800,t.mid(i,1),1);

+=fm.width(t[i]);

+;

}

p.end();

/*4:CopythepixmaptotheHellowidget*/

bitBlt(this,pmx,pmy,pm);

}

linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)

linux相關(guān)文章:linux教程




評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉