Qt学习笔记(Qt3库)下
9-1.cpp
#include <qapplication.h>
#include <qwidget.h>
#include <qpainter.h>
class MyMainWindow:public QWidget
{
public:
MyMainWindow();
private:
void paintEvent(QPaintEvent *);
QPainter *paint;
};
//此函数是由系统调用的,每当第一次生成或被挡住,它都会再次被调用
void MyMainWindow::paintEvent(QPaintEvent *)
{
//此句最好做成局部变量的定义,否则将导致重复申请动态内存
paint=new QPainter;
paint->begin(this);
//设置边框颜色、线形和线的宽度
paint->setPen(QPen(blue,10,QPen::SolidLine));
//设置边框内部颜色全部填充为红色
paint->setBrush(QBrush(red,SolidPattern));
//绘制矩形
paint->drawRect(20,20,160,160);
//绘图介绍
paint->end();
}
MyMainWindow::MyMainWindow()
{
//设置主窗口坐标位置、大小
setGeometry(100,100,200,200);
}
int main(int argc,char **argv)
{
QApplication a(argc,argv);
MyMainWindow w;
a.setMainWidget(&w);
w.show();
a.exec();
}
//**************************************************************
对上一个例子做一个扩展,画一个4*4棋盘
9-1-1.cpp
#include <qapplication.h>
#include <qwidget.h>
#include <qpainter.h>
class MyMainWindow:public QWidget
{
public:
MyMainWindow();
private:
//绘图函数声明
void paintEvent(QPaintEvent *);
QPainter *paint;
};
void MyMainWindow::paintEvent(QPaintEvent *)
{
int i;
int x=20,y=20;
paint=new QPainter;
paint->begin(this);
paint->setPen(QPen(blue,4,QPen::SolidLine));
paint->setBrush(QBrush(white,SolidPattern));
//画5条横线
for(i=0;i<5;i++)
{
paint->drawLine(20,y+80*i,340,y+80*i);
}
//画5条竖线
for(i=0;i<5;i++)
{
paint->drawLine(x+80*i,20,x+80*i,340);
}
在棋盘正中做两个红棋子
paint->setBrush(QBrush(red,SolidPattern));
paint->drawEllipse(100,100,80,80);
paint->drawEllipse(180,180,80,80);
在棋盘正中做两个蓝棋子
paint->setBrush(QBrush(green,SolidPattern));
paint->drawEllipse(180,100,80,80);
paint->drawEllipse(100,180,80,80);
paint->end();
}
MyMainWindow::MyMainWindow()
{
setGeometry(100,100,400,400);
}
int main(int argc,char **argv)
{
QApplication a(argc,argv);
MyMainWindow w;
a.setMainWidget(&w);
w.show();
a.exec();
}
//**************************************************************
在主窗口上加一个标签,在标签上做出一个动画。
15-1.h
#include <qapplication.h>
#include <qwidget.h>
#include <qlabel.h>
#include <qmovie.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <qlayout.h>
class MyMainWindow:public QWidget
{
//宏定义
Q_OBJECT;
public:
MyMainWindow(char *);
QMovie movie; //定义动画的对象
public slots:
void UnPause(); //继续播放
void Pause(); //暂停
void addSpeed(); //加速动画的播放速度
void decSpeed(); //减慢动画的播放速度
private:
unsigned long num;
QLabel *label;
QPushButton *b1;
QPushButton *b2;
QPushButton *b3;
QPushButton *b4;
QPushButton *b5;
};
15-1.cpp
#include "15-1.h"
void MyMainWindow::UnPause()
{
movie.unpause(); //继续播放
}
void MyMainWindow::Pause()
{
movie.pause(); //暂停播放
}
void MyMainWindow::addSpeed()
{
num+=20;
movie.setSpeed(num); //以20%增加速度
}
void MyMainWindow::decSpeed()
{
num-=20;
movie.setSpeed(num); //以20%减少
}
//str里存放动画的文件名
MyMainWindow::MyMainWindow(char *str):movie(str)
{
setGeometry(100,100,300,280);
num=100; //初始速度为100
b1=new QPushButton("Continue!",this);
b1->setGeometry(130,20,120,40);
b1->setMinimumSize(40,20);
b1->setFont(QFont("Times",18,QFont::Bold));
b2=new QPushButton("Pause!",this);
b2->setGeometry(130,70,120,40);
b2->setMinimumSize(40,20);
b2->setFont(QFont("Times",18,QFont::Bold));
b3=new QPushButton("Hurry!",this);
b3->setGeometry(130,120,120,40);
b3->setMinimumSize(40,20);
b3->setFont(QFont("Times",18,QFont::Bold));
b4=new QPushButton("Slow!",this);
b4->setGeometry(130,170,120,40);
b4->setMinimumSize(40,20);
b4->setFont(QFont("Times",18,QFont::Bold));
b5=new QPushButton("Quit!",this);
b5->setGeometry(130,220,120,40);
b5->setMinimumSize(40,20);
b5->setFont(QFont("Times",18,QFont::Bold));
label=new QLabel(this);
label->setGeometry(10,10,120,60);
label->setMovie(movie);
QVBoxLayout *vbox=new QVBoxLayout(this);
vbox->addWidget(label);
vbox->addWidget(b1);
vbox->addWidget(b2);
vbox->addWidget(b3);
vbox->addWidget(b4);
vbox->addWidget(b5);
connect(b1,SIGNAL(clicked()),this,SLOT(UnPause()));
connect(b2,SIGNAL(clicked()),this,SLOT(Pause()));
connect(b3,SIGNAL(clicked()),this,SLOT(addSpeed()));
connect(b4,SIGNAL(clicked()),this,SLOT(decSpeed()));
connect(b5,SIGNAL(clicked()),qApp,SLOT(quit()));
}
int main(int argc,char **argv)
{
QApplication a(argc,argv);
//给构造函数发送动画名,并且文件必须使.gif格式
MyMainWindow w("trolltech.gif");
a.setMainWidget(&w);
w.show();
a.exec();
}
转自: http://blog.sina.com.cn/s/blog_5eedcb760100cdp3.html