自己在看KDevelop生成的代码,写了点笔记,希望高手给看看那里有错误,

thisisll 2005-12-13 09:31:01
请大家纠正拍砖,要是能把自己的学习经验或其他所由有用的提出来就更好了
先使劲谢谢

KDevelop学习笔记一(测试,希望大家闲了,帮我看下,我翻译英文部分哪里不对)

在网上找了好久
都没有看到KDevelop的资料
大家的建议都是直接看他自带的文档,恩,一个类似MSDN的东西
内容太多,算了,我直接看他生成的代码吧

KDevelop跟VC十分的像,www.Linuxc.net的老大说这个就是照VC开发的,还好,VC对我不是问题
找到叫"工程"的菜单新建个工程先
wizard出来了,在目录c++里的KDE里找到头一个application framework,多年经验告诉我,这是最基础的之一.
我将"Generates a simple KDE application with one toplevel window, menus and toolbars. A DCOP interface is also provided, so that your application can provide a scripting interface"
我起的工程名是KDE
然后完成其他的下一步下一步,工程终于搞出来了
build出来后~~恩,确实跟VC里的SDI很像~
点开左边的类view有6个类,KDEApp,KDEAppIface,KDEAppPreferences,KDEAppPrefPageOne,KDEAppPrefPageTwo,KDEAppView
还有两个main
奇怪,两个~~~
还有3个全局变量
表面上看KDEApp是App类,还有一个View类KDEAppView,没有Doc类,估计跟MFC中的"文档/视图"(这种概念简单的讲就是视图负责显示,文档负责数据)的概念一样
再看下文件夹,东西不少,其中有个src,
里面装着我要看的(去掉了一些我现在认为没必要的文件包括makefile)
kdeapp.h kdeapp.cpp kdeappview.h kdeappview.cpp kdeapp_client.cpp kdeappiface.h pref.cpp main.cpp pref.h

侦查的差不多了,看看两个main到底是怎么回事吧,
一个在kdeapp_client.cpp里一个在main.cpp里
main.cpp中的main一开始就是KAboutData about...
估计他是菜单里的about,
重点放在kdeapp_client.cpp里
要想知道他是如何工作的就要跟他
先下上断点,对了这里叫切换断点
奇怪怎么在kdeapp_client.cpp里的main中的断点没有执行到?
看来前面的判断有问题,索性看看堆栈是怎么样的
在两个main开头都设了断点,开始调试
停在了main.cpp中的main里
再看堆栈,就他一个函数,看来这里是入口
KAboutData about...
该看文档了,在右边的文档里一查
KAboutData Class Reference
This class is used to store information about a program.Holds information needed by the "About" box and other classes
是用来放程序信息的,看来不能光望文生义了
下面是main.cpp中的主要代码
int main(int argc, char **argv)
{
KAboutData about("kdeapp", I18N_NOOP("KDEApp"), version, description,
KAboutData::License_GPL, "(C) 2005 snail", 0, 0, "snail@localhost.localdomain");

about.addAuthor( "snail", 0, "snail@localhost.localdomain" );
KCmdLineArgs::init(argc, argv, &about);
KCmdLineArgs::addCmdLineOptions(options);
KApplication app;

// register ourselves as a dcop client
app.dcopClient()->registerAs(app.name(), false);

// see if we are starting with session management
if (app.isRestored())
{
RESTORE(KDEApp);
}
else
{
// no session.. just start up normally
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
if (args->count() == 0)
{
KDEApp *widget = new KDEApp;
widget->show();
}
else
{
int i = 0;
for (; i < args->count(); i++)
{
KDEApp *widget = new KDEApp;
widget->show();
widget->load(args->url(i));
}
}
args->clear();
}

return app.exec();
}

接着将一段段的看
————————————————————————————————————————————
KAboutData about("kdeapp", I18N_NOOP("KDEApp"), version, description,
KAboutData::License_GPL, "(C) 2005 snail", 0, 0, "snail@localhost.localdomain");

about.addAuthor( "snail", 0, "snail@localhost.localdomain" );
KCmdLineArgs::init(argc, argv, &about);
KCmdLineArgs::addCmdLineOptions(options);
————————————————————————————————————————————
about.addAuthor( "snail", 0, "snail@localhost.localdomain" );
很显然,添加作者,安全期间我看下文档好了“Defines an author.”看来没猜错

KCmdLineArgs
A class for command-line argument handling.
KCmdLineArgs provides simple access to the command-line arguments for an application. It takes into account Qt-specific options, KDE-specific options and application specific options.
This class is used in main() via the static method init().
这个类是管理命令行选项的

void KCmdLineArgs::init ( int _argc,
char ** _argv,
const KAboutData * about,
bool noKApp = false
)
Initialize class.
This function should be called as the very first thing in your application. It uses KAboutData to replace some of the arguments that would otherwise be required.
通过把KAboutData对象的信息传进去init

void KCmdLineArgs::addCmdLineOptions ( const KCmdLineOptions * options,
const char * name = 0,
const char * id = 0,
const char * afterId = 0
)
Add options to your application.
要加的选项是一个全局变量option定义是如下
static KCmdLineOptions options[] =
{
{ "+[URL]", I18N_NOOP( "Document to open" ), 0 },
KCmdLineLastOption
};

KCmdLineOptions
Structure that holds command line options.
This class is intended to be used with the KCmdLineArgs class, which provides convenient and powerful command line argument parsing and handling functionality.
是个存放命令行选项的类,属性有
const char * name
const char * description
const char * def
分别是
The name of the argument as it should be called on the command line and appear in myapp --help.
选项的名字
The text description of the option as should appear in myapp --help.
选项的描述
The default value for the option, if it is not specified on the command line.
选项的缺省值

这里还有一个宏
#define I18N_NOOP(x) x
I18N_NOOP marks a string to be translated without translating it. Do not use this unless you know you need it.
标记一个要被翻译的字符串,还有一个宏
#define I18N_NOOP2(comment, x) x
If the string is too ambiguous to be translated well to a non-english language, use this instead of I18N_NOOP to separate lookup string and english.
Warning:
You need to call i18n( comment, stringVar ) later on, not just i18n( stringVar ).
看解释是有多种意思的时候用这个,怎么用,用到哪我还不清楚

————————————————————————————————————————————————
KApplication app;

// register ourselves as a dcop client
app.dcopClient()->registerAs(app.name(), false);
————————————————————————————————————————————————
KApplication
Controls and provides information to all KDE applications.
Only one object of this class can be instantiated in a single app. This instance is always accessible via the 'kapp' global variable.
控制整个程序的类,只能实例化一次在一个程序中,看来我们不用太关心他,起码刚开始学的时候
后面又注释了,将自己注册成dcop客户端,dcop??
DCOPClient * KApplication::dcopClient()
Returns a pointer to a DCOPClient for the application.
If a client does not exist yet, it is created when this function is called.
DCOPClient是什么?查查看
Inter-process communication and remote procedure calls for KDE applications.
为KDE程序提供内部进程通信和远程过程调用
registerAs(app.name(), false)
DCOPClient::registerAs ( const QCString & appId, bool addPID = true )
Registers at the DCOP server.
在dcop server上注册自己,addPID为什么是false呢?
appId is a unique application/program id that the server will use to associate requests with. If there is already an application registered with the same name, the server will add a number to the id to unify it. If addPID is true, the PID of the current process will be added to id.
appId是用来区分每个进程的ID,false就是他不注册这个ID到dcop server上了~~

...全文
287 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
thisisll 2005-12-14
  • 打赏
  • 举报
回复
恩,谢谢
thisisll 2005-12-13
  • 打赏
  • 举报
回复
http://www.qiliang.net/qt/index.html
这里汉化了
我现从这里看起吧
x86 2005-12-13
  • 打赏
  • 举报
回复
官方网站就有, www.trolltech.com
我记得还有人把文档汉化了,虽然不是最新的,也可以看看.
thisisll 2005-12-13
  • 打赏
  • 举报
回复
恩,我先搜搜QT的文档
x86 2005-12-13
  • 打赏
  • 举报
回复
你可以再试一下用KDevelop编一个QT程序,应该比KDE程序更清晰一些, 更容易搞懂KDE程序的本质.
QT的话,例子非常多, 文档也是很丰富的. 等熟练掌握QT之后回头再来研究KDE程序就可以比较容易抓住KDE相关的内容了.
loserking 2005-12-13
  • 打赏
  • 举报
回复
我觉得学东西还是要学其根本。KDE只是在QT之上封装了很薄的一层。

还是应该从QT文档和QT应用开始,从最基本的地方入手。QT文档里列举了QT的几大基本特性,特别是slot-signal。完全明白这些,才能写出好的KDE/QT应用。
thisisll 2005-12-13
  • 打赏
  • 举报
回复
———————————————————————————————————————————
// see if we are starting with session management
if (app.isRestored())
{
RESTORE(KDEApp);
}
———————————————————————————————————————————
bool KApplication::isRestored ( ) const [inline]
Is the application restored from the session manager?
bool KApplication::isRestored() const { return QApplication::isSessionRestored(); }
bool QApplication::isSessionRestored () const
Returns TRUE if the application has been restored from an earlier session; otherwise returns FALSE.
怎么又冒出来个session?
#define RESTORE(type) { int n = 1;\
while (KMainWindow::canBeRestored(n)){\
(new type)->restore(n);\
n++;}}
又出来一个restore??
CSDN上问了下  loserking(刻舟求剑)和x86(大雪) 回答了我的问题,在这里使劲谢谢
“这个session跟用户会话应该无关。而是应该指的是QSessionManager。QSessionManager提供了一个interface来存储应用的运行状态。比如在windows下存储注册表,linux下存储某个文件中。
KDE应用在QT之上建立了一个自己的目录结构。isRestored()应该是检查具体app所在的目录下的状态配置文件,来判断是否有过以前存储的运行状态。如果以前串行化了运行状态,就根据这个串行化来构造应用,否则构造个新的空应用。
比如,那些toolbar或者停靠窗口,用户改变了它们的位置。当下次启动程序的时候,可能还希望是原来的用户习惯的位置。
...”
"session是指当前回话。退出KDE再进入时可以恢复上一次session,如果你的应用程序支持session,那么你可以在保存和恢复session的时候做相应的事情,比如保存用户操作状态等等"
再看看代码,恩,如果支持session那把上次记录在session manager里的状态读出来并恢复(restore).
———————————————————————————————————————————
// no session.. just start up normally
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
if (args->count() == 0)
{
KDEApp *widget = new KDEApp;
widget->show();
}
else
{
int i = 0;
for (; i < args->count(); i++)
{
KDEApp *widget = new KDEApp;
widget->show();
widget->load(args->url(i));
}
}
args->clear();
———————————————————————————————————————————
如果不支持session就到这里了
程序运行到了这里了,又搞出一个KCmdLineArgs
KCmdLineArgs * KCmdLineArgs::parsedArgs ( const char * id = 0 )
Access parsed arguments.
This function returns all command line arguments that your code handles. If unknown command-line arguments are encountered the program is aborted and usage information is shown.
Parameters:
id The name of the options you are interested in, can be 0.
这个函数是分析命令行的,把所有命令行的信息放到KCmdLineArgs这个类里
int KCmdLineArgs::count ( ) const
Read the number of arguments that aren't options (but, for example, filenames).
Returns:
The number of arguments that aren't options
这个函数返回的为命令行中不是选项的数量(不包括文件名)
如果命令行输入的都是选项,那直接就实例化我们的app然后show出来
如果有不是的那么就由app来显示一些其他的信息(具体的在后面分析KDEApp的时候再看).

终于把main.cpp看完了
linux_DD 2005-12-13
  • 打赏
  • 举报
回复
mark 下, 再学习
loserking 2005-12-13
  • 打赏
  • 举报
回复
哦,对了。你在用QT开发的时候,建议你多多注意其构造函数。以及你自己派生类的构造函数的初始化列表。往往一个顺序问题,就能产生巨大的差别。
loserking 2005-12-13
  • 打赏
  • 举报
回复
坦率的说,呵呵,没仔细看细节。只是感觉学习方向有点问题。

随着你学习QT的深入,你会慢慢把注意力集中在QT本身上。熟悉基础类库,控制自己应用的slot和signal。由于QT的平台无关性,可能对具体平台的实现,反倒不太关心。

另外一个值得投入精力的是QT的基础代码实现上。不得不称道的是这是个巨大的代码库。由于其class tree非常清晰明确,你可以轻松找到某个具体实现的QT源代码。我来这个坛子也就2周,这里大多数Unix下的功能性问题,都可以在QT代码库里找到答案。

我比较依赖的代码库有QT和ACE,看看代码,查查man,是我Unix程序生活的重要一部分。

KDE部分跟上面2者比,显得微不足道。而且KDE的文档实在太烂了。很是让人郁闷。
bekars 2005-12-13
  • 打赏
  • 举报
回复
我个人看法不要用IDE开发,界面程序除外,但也不要太依靠。
thisisll 2005-12-13
  • 打赏
  • 举报
回复
那谁看到我写的有什么错误没?

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧