实训四文档格式.docx
《实训四文档格式.docx》由会员分享,可在线阅读,更多相关《实训四文档格式.docx(26页珍藏版)》请在冰豆网上搜索。
curImage.setAlphaBuffer(true);
resizeContents();
//此函数用于管理初始窗口部件获取自己的大小
}
voidImageEditor:
resizeContents()
QSizesize=zoom*curImage.size();
if(zoom>
=3)
size+=QSize(1,1);
//若zoom大于等于3,size则放大一格
QScrollView:
resizeContents(size.width(),size.height());
//QScrollView是否显示滚动条由窗口的大小决定
setImage(constQImage&
newImage)//获取所要显示的图片
if(newImage!
=curImage)
{
curImage=newImage.convertDepth(32);
curImage.detach();
//重新获得大小
updateContents();
//重新绘制图片
}
contentsMousePressEvent(QMouseEvent*event)//鼠标点击事件
if(event->
button()==LeftButton)//左击事件
setImagePixel(event->
pos(),true);
else
button()==RightButton)//右击事件
pos(),false);
cotentMouseMoveEvent(QMouseEvent*event)//鼠标按住拖动事件
state()&
LeftButton)//左击事件
RightButton)//右击事件
intmain(intargc,char*argv[])
QApplicationapp(argc,argv);
ImageEditor*imageEditor=newImageEditor;
imageEditor->
setImage(QImage:
QImage("
mouse.png"
0));
//加载资源图片mouse.png
app.setMainWidget(imageEditor);
show();
//显示窗口
returnapp.exec();
多文档界面的Editor运行结果:
MainWindow:
MainWindow(QWidget*parent,constchar*name)
QMainWindow(parent,name)
workspace=newQWorkspace(this);
//创建一个窗口部件
setCentralWidget(workspace);
//使其成为中央窗口部件
connect(workspace,SIGNAL(windowActivated(QWidget*)),
this,SLOT(updateMenus()));
connect(workspace,SIGNAL(windowActivated(QWidget*)),//槽用于保持菜单栏和状态栏总是用于反应,当前激活子菜单的窗口状态。
this,SLOT(updateModIndicator()));
createActions();
createMenus();
createToolBars();
createStatusBar();
setCaption(tr("
Editor"
));
setIcon(QPixmap:
fromMimeSource("
icon.png"
voidMainWindow:
updateMenus()//只要一个窗口被激活或最后一个被关闭,updatemenu就会被调用
boolhasEditor=(activeEditor()!
=0);
saveAct->
setEnabled(hasEditor);
saveAsAct->
pasteAct->
deleteAct->
copyAvailable(activeEditor()
&
&
activeEditor()->
hasSelectedText());
closeAct->
closeAllAct->
tileAct->
cascadeAct->
nextAct->
previousAct->
windowsMenu->
clear();
//若不存在激活的窗口,清空windows菜单
createWindowsMenu();
2.编写程序ch_501,实现程序界面如下:
下方文本框可以输入内容(保存类型为AllFiles(*.*)),所有菜单及工具按钮均可实现功能。
File菜单如下:
Help菜单如下:
About菜单为:
AboutQt菜单为
实验过程:
1.运行结果:
2、完成书本第七部分“事件处理”中的例题Ticker;
#include<
qpainter.h>
#include"
ticker.h"
Ticker:
Ticker(QWidget*parent,constchar*name)
QWidget(parent,name)
offset=0;
//设置被绘制文本X坐标
myTimerId=0;
//定时器标示器
voidTicker:
setText(constQString&
newText)
myText=newText;
update();
//强制重新绘制
updateGeometry();
//提示ticker窗口布局的变化
QSizeTicker:
sizeHint()const
returnfontMetrics().size(0,text());
paintEvent(QPaintEvent*)
QPainterpainter(this);
inttextWidth=fontMetrics().width(text());
//确定文版需要多少水平空间
if(textWidth<
1)
return;
intx=-offset;
while(x<
width()){
painter.drawText(x,0,textWidth,height(),
AlignLeft|AlignVCenter,text());
x+=textWidth;
showEvent(QShowEvent*)
myTimerId=startTimer(30);
//设置一个30秒的定时器
timerEvent(QTimerEvent*event)//时间间隔调用
if(event->
timerId()==myTimerId){
++offset;
if(offset>
=fontMetrics().width(text()))
scroll(-1,0);
//左移一个像素
}else{
QWidget:
timerEvent(event);
hideEvent(QHideEvent*)
killTimer(myTimerId);
//当文本消失时,释放对应的定时器
3、请按下列程序源代码,编写ch_601,并在程序后进行注释:
ch_601.cpp的源程序为:
qtimer.h>
qapplication.h>
qtextcodec.h>
stdlib.h>
ch_601.h"
ch_601:
ch_601(QWidget*parent,constchar*name):
QWidget(parent,name)
for(inta=0;
a<
numColors;
a++)//声明变量a产生随机颜色
{
colors[a]=QColor(rand()&
255,rand()&
255);
//设置随机颜色
rectangles=0;
//矩形计数为0
startTimer(0);
//开始计数为0
QTimer*counter=newQTimer(this);
connect(counter,SIGNAL(timeout()),this,SLOT(updateCaption()));
//计数器的信号为timeout时,更新caption
counter->
start(1000);
voidch_601:
updateCaption()
QStrings;
s.sprintf(QObject:
tr("
QTimer实例"
),rectangles);
//设置caption为“QTimer实例”和举行数
setCaption(s);
QPainterpaint(this);
intw=width();
//宽w
inth=height();
//高h
if(w<
=0||h<
=0)//若w<
=0或h<
=0
return;
//返回
paint.setPen(NoPen);
//若没有画笔则设置画笔
paint.setBrush(colors[rand()%numColors]);
//画刷颜色随机
QPointp1(rand()%w,rand()%h);
//设置矩形的位置
QPointp2(rand()%w,rand()%h);
QRectr(p1,p2);
paint.drawRect(r);
//画矩形
timerEvent(QTimerEvent*)
for(inti=0;
i<
100;
i++)
repaint(false);
//在一个绘画窗口发送之前,之前的窗口不会被擦除
rectangles++;
//矩形累加
Main.cpp的源程序为:
intmain(intargc,char**argv)
QApplicationa(argc,argv);
ch_601always(0,0);
QTextCodec:
setCodecForTr(QTextCodec:
codecForName("
gb18030"
//设置标题字体
always.resize(400,250);
//初始化窗体
a.setMainWidget(&
always);
always.setCaption(QObject:
时钟的测试例题"
always.show();
returna.exec();
ch_601.h的源程序为:
#ifndefCH_601_H
#defineCH_601_H
qwidget.h>
constintnumColors=120;
classCh_601:
publicQWidget
Q_OBJECT
public:
Ch_601(QWidget*parent=0,constchar*name=0);
protected:
voidpaintEvent(QPaintEvent*);
voidtimerEvent(QTimerEvent*);
privateslots:
voidupdateCaption();
private:
intrectangles;
QColorcolors[numColors];
};
#endif
Ch_601运行结果:
4、请按下列程序源代码(其中主程序自编),编写ch_602,并在程序后进行注释:
progressbar.h"
qradiobutton.h>
qpushbutton.h>
qprogressbar.h>
qlayout.h>
qmotifstyle.h>
ProgressBar:
ProgressBar(QWidget*parent,constchar*name)
QButtonGroup(0,Horizontal,QObject:
进度条测试"
),parent,name),timer()
setMargin(10);
//设置边距
QGridLayout*toplayout=newQGridLayout(layout(),2,2,5);
//设置空间组
setRadioButtonExclusive(TRUE);
//按键单一选择
slow=newQRadioButton(QObject:
低速"
),this);
//设置低速,中速,高速三个选项
normal=newQRadioButton(QObject:
中速"
),this);
fast=newQRadioButton(QObject:
高速"
QVBoxLayout*vb1=newQVBoxLayout;
//新建一个box
toplayout->
addLayout(vb1,0,0);
//嵌套布局
vb1->
addWidget(slow);
//添加slow,normal,fast到布局
addWidget(normal);
addWidget(fast);
start=newQPushButton(QObject:
开始"
//新建开始按钮
reset=newQPushButton(QObject:
重启"
//新建重启按钮
QVBoxLayout*vb2=newQVBoxLayout;
addLayout(vb2,0,1);
vb2->
addWidget(start);
addWidget(reset);
progress=newQProgressBar(100,this);
//添加进度条
addMultiCellWidget(progress,1,1,0,1);
//1列插入progressbar占2行2列
connect(start,SIGNAL(clicked()),this,SLOT(slotStart()));
//点击开始触发开始槽
connect(reset,SIGNAL(timeout()),this,SLOT(slotTimeout()));
//点击重置触发timeout槽
normal->
setChecked(TRUE);
//默认选中设置为中速
start->
setFixedWidth(80);
//设置最大最小宽度为80
setMinimumWidth(300);
//设置最小宽度300
voidProgressBar:
slotStart()
if(progress->
progress()==-1)
if(slow->
isChecked())
progress->
setTotalSteps(10000);
//slow进度条满需10000小时
elseif(normal->
setTotalSteps(1000);
//normal进度条满需1000小时
setTotalSteps(50);
//fast进度条满需50小时
slow->
setEnabled(FALSE);
//slow不能选中
//normal不能选中
fast->
/fast不能选中
if(!
timer.isActive())//若时钟未被激活
timer.start
(1);
//时钟设置为1
setText(QObject:
暂停"
timer.stop();
//时间停止
继续"
slotReset()//进度条重置
setEnabled(TRUE);
//start可以被选择
//slow可以被选择
//normal可以被选择
//fast可以被选择
reset();
//进度条重置
slotTimeout()
intp=progress->
progress();
//声明p,用于保存进度条
#if1
if(p==progress->
totalSteps())//若进度条满
#endif
setProgress(++p);
processbar.h的源程序为:
#ifndefPROGRESSBAR_H
#definePROGRESSBAR_H
qbuttongroup.h>
classQRadioButton;
classQPushButton;
classQProgressBar;
classProgressBar:
publicQButtonGroup
ProgressBar(QWidget*parent=0,constchar*name=0);
QRadioButton*slow,*normal,*fast;
QPushButton*start,*pause,*reset;
QProgressBar*progress;
QTimertimer;
protectedslots:
voidslotStart();
voidslotReset();
voidslotTimeout();
Main.cpp源程序为:
ProgressBa