魔方网表 让信息化更简单

 找回密码
 注册
查看: 3945|回复: 0

在Qt 中如何记录上次程序退出时的位置与大小

[复制链接]
admin 发表于 2008-11-18 20:10:00 | 显示全部楼层 |阅读模式
要保存位置,只要在组件退出时记录下组件的几何属性,在组件创建时读入就可以了.
我借用kdevelop自己生成的Qt单文档程序实现了这个功能,具体操作如下:

1.重载RecordPosApp类的closeEvent虚函数.
protected:
virtual void closeEvent(QCloseEvent* e);

2.实现closeEvent函数.
void RecordPosApp::closeEvent(QCloseEvent* e)
{
//创建文件
QFile f("Pos.txt");
if ( !f.open( IO_WriteOnly) )
return;

QTextStream t( &f );
QString strPos[4];

//记录组件的几何坐标
strPos[0].setNum(this->x());
strPos[1].setNum(this->y());
strPos[2].setNum(this->width());
strPos[3].setNum(this->height());

//将组件的几何坐标写入文件
t <<strPos[0]<< "\n";
t <<strPos[1]<< "\n";
t <<strPos[2]<< "\n";
t <<strPos[3]<< "\n";
f.close();

//退出整个程序
qApp->quit();
}
到这里,记录部分就写完了.下面是读入部分.

3.读入组件的几何位置.
这段代码书写具体函数不限,只要被RecordPosApp的构造函数调用的就可以了,我写在了
RecordPosApp::initView()中.
具体如下:
void RecordPosApp::initView()
{
////////////////////////////////////////////////////////////////////
// set the main widget here
view=new RecordPosView(this, doc);
setCentralWidget(view);//这里以上是原有的代码

//打开文件
QFile f("Pos.txt");
if ( !f.open( IO_ReadOnly ) )//打不开的话不进行操作
return;

QTextStream t( &f );
QString strPos[4];

//从文件中读入位置的值放入字符数组
strPos[0]=t.readLine();
strPos[1]=t.readLine();
strPos[2]=t.readLine();
strPos[3]=t.readLine();

//设置本组件的几何位置
this->setGeometry(
strPos[0].toInt(),//x
strPos[1].toInt(),//y
strPos[2].toInt(),//width
strPos[3].toInt());//height


f.close();

}
这样,在程序开启时就被重新设置了一次几何位置了.

junglesong 原创
2004-3-17AM
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|魔方软件 ( 京ICP备08008787号 )

京公网安备 11010702001722号

GMT+8, 2024-4-24 08:32 , Processed in 0.064666 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表