Jump to Navigation

我的第一个kde4程序

在使用bespin中的xbar kde4 plasma applet的时候,遇到一个自定义菜单的功能,希望在菜单中有一功能,能最小化所有的窗口,能恢复所有窗口功能。
没有找到现成的工具或者方法,所以写了一个简单的使用kde4基础库的小工具来实现这一功能。另外这个小程序还有一个功能,可根据程序名字把其窗口提到最上层,展示给用户,这功能用于使用dolphin显示某些固定文件夹,如在已经开启的dolphin中显示文档目录,并把dolphin拉到最上层。

刚开始写kde4的程序,只用了kde4基础库的三个类,代码:

  1. #include <QtCore>
  2.  
  3. #include <kaboutdata.h>
  4. #include <kapplication.h>
  5. #include <kcmdlineargs.h>
  6. #include <kwindowsystem.h>
  7. #include <kwindowinfo.h>
  8. #include <KDirWatch>
  9. #include <KIcon>
  10. #include <KStandardDirs>
  11. #include <KUriFilterData>
  12. #include <KRun>
  13.  
  14. #include <KApplication>
  15. #include <KAboutData>
  16. #include <KCmdLineArgs>
  17. #include <KMessageBox>
  18. #include <KLocale>
  19.  
  20. /*
  21.   minimizeall
  22.   unminimizeall
  23.   appname
  24.  */
  25. int main (int argc, char *argv[])
  26. {
  27. if (argc != 2) {
  28. qDebug()<<__FILE__<<__LINE__<<"arguments count error.";
  29. return 1;
  30. }
  31. QString appname = argc > 1 ? QString(argv[1]) : QString();
  32.  
  33. KAboutData aboutData(
  34. // The program name used internally.
  35. "tutorial1",
  36. // The message catalog name
  37. // If null, program name is used instead.
  38. 0,
  39. // A displayable program name string.
  40. ki18n("Tutorial 1"),
  41. // The program version string.
  42. "1.0",
  43. // Short description of what the app does.
  44. ki18n("Displays a KMessageBox popup"),
  45. // The license this code is released under
  46. KAboutData::License_GPL,
  47. // Copyright Statement
  48. ki18n("(c) 2007"),
  49. // Optional text shown in the About box.
  50. // Can contain any information desired.
  51. ki18n("Some text..."),
  52. // The program homepage string.
  53. "<a href="http://example.com/",<br />
  54. ">http://example.com/",<br /></a> // The bug report email address
  55. "<a href="mailto:submit@bugs.kde.org">submit@bugs.kde.org</a>");
  56. argc = 1;
  57. KCmdLineArgs::init(argc, argv, &aboutData);
  58. KApplication app;
  59.  
  60.  
  61. QWidget *w;
  62. KWindowInfo info;
  63. QString title;
  64.  
  65. if (appname == "minimizeall") {
  66. foreach ( WId id, KWindowSystem::windows() ) {
  67. KWindowSystem::minimizeWindow(id);
  68. // KWindowSystem::unminimizeWindow(id);
  69. }
  70. } else if (appname == "unminimizeall") {
  71. foreach ( WId id, KWindowSystem::windows() ) {
  72. // KWindowSystem::minimizeWindow(id);
  73. KWindowSystem::unminimizeWindow(id);
  74. }
  75. } else {
  76. foreach ( WId id, KWindowSystem::windows() ) {
  77. // w = QWidget::find(id);
  78. info = KWindowInfo(id, NET::WMName | NET::WMVisibleName , 0);
  79. qDebug()<<"wid: "<<id<<","<<info.name()<<info.visibleName()<<info.win();
  80. title = info.name().toLower();
  81. // KWindowSystem::minimizeWindow(id);
  82. KWindowSystem::unminimizeWindow(id);
  83. if (title.endsWith(appname)) {
  84. // KWindowSystem::activateWindow(id);
  85. // KWindowSystem::raiseWindow(id);
  86. KWindowSystem::forceActiveWindow(id);
  87. break;
  88. }
  89. // printf("wid: %d, %s\n", id, );
  90. }
  91. }
  92.  
  93. // KGuiItem yesButton( i18n( "Hello" ), QString(),
  94. // i18n( "This is a tooltip" ),
  95. // i18n( "This is a WhatsThis help text." ) );
  96. // KMessageBox::questionYesNo( 0, i18n( "Hello World" ),
  97. // i18n( "Hello" ), yesButton );
  98. return 0;
  99. }

编译命令,
g++ -g kfocusapp.cpp -I/usr/include/qt4/ -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtCore/ -I/usr/include/qt4/qt -I/usr/include/KDE/ -L/usr/lib64/kde4 -lkdecore -lkdeui

Category:

添加新评论

Plain text

  • 不允许HTML标记。
  • 自动将网址与电子邮件地址转变为链接。
  • 自动断行和分段。
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.


Main menu 2

Story | by Dr. Radut