摘要

该plasmoid将包含一个文本框和按钮。

代码

.desktop文件

每个plasmoid都需要一个.desktop文件告诉plasma怎样启动它以及它的名字,文件内容如下:

plasma-applet-myplasmaapplet.desktop

[Desktop Entry]

Name=MyPlasmaApplet

Name[zh_CN]=我的Plasma小应用程序

Comment=PlasmaMyPlasmaApplet

Comment[zh_CN]=PlasmaMyPlasmaApplet

Type=Service

X-KDE-ServiceTypes=Plasma/Applet

X-KDE-Library=plasma_applet_myplasmaapplet

X-KDE-PluginInfo-Author=fuyajun

X-KDE-PluginInfo-Email=fuyajun1983cn@gmail.coms

X-KDE-PluginInfo-Name=myplasmaapplet

X-KDE-PluginInfo-Version=0.1

X-KDE-PluginInfo-Website=http://plasma.kde.org/

X-KDE-PluginInfo-Category=

X-KDE-PluginInfo-Depends=

X-KDE-PluginInfo-License=GPL

X-KDE-PluginInfo-EnabledByDefault=true

其中,最重要的是X-KDE_Library和X-KDE-PluginInfo-Name,它们是自己开发为类与plasma之间的“粘合剂”,没有它,plasamoid将不能启动。

头文件

myplasmaapplet.h

// Here we avoid loading the header multiple times
#ifndef MYPLASMAAPPLET_HEADER
#define MYPLASMAAPPLET_HEADER// We need the Plasma Applet headers
#include <Plasma/Applet>class QSizeF;namespace Plasma {class LineEdit;class PushButton;
}// Define our plasma Applet
class MyPlasmaApplet : public Plasma::Applet
{Q_OBJECTpublic:// Basic Create/DestroyMyPlasmaApplet(QObject *parent, const QVariantList &args);~MyPlasmaApplet();void init();private:Plasma::LineEdit *m_lineEdit;Plasma::PushButton *m_pushButton;
};// This is the command that links your applet to the .desktop file
K_EXPORT_PLASMA_APPLET(myplasmaapplet, MyPlasmaApplet)
#endif

voidpaintInterface(QRectF contentsRect)

这可认为是将plasmoid画到屏幕上的主函数。在该函数中,你可以定义plasmoid的界面。你应该使用在contentsRect定义的矩形区域内进行绘制,避免使用geometry()。当一个plasmoid没有标准的背景时,或当它调用setBackgroundHints()禁用背景时,或当它在一个面板中时,geometry()和boundingRect()行为一致;然而,当plasmoid使用标准背景时,小应用程序(applet)将与它本应画的位置之间有一个空隙。

实际工作文件

下面是一些实现函数体:

myplasmaapplet.cpp

#include "myplasmaapplet.h"
#include <QPainter>
#include <QSizeF>
#include <QGraphicsLinearLayout>#include <plasma/theme.h>
#include <plasma/widgets/lineedit.h>
#include <plasma/widgets/pushbutton.h>MyPlasmaApplet::MyPlasmaApplet(QObject *parent, const QVariantList &args): Plasma::Applet(parent, args),m_lineEdit(0),m_pushButton(0)
{// this will get us the standard applet background, for free!setBackgroundHints(DefaultBackground);//tell if we need a user interface for configuring the appletsetHasConfigurationInterface(true);resize(200, 200);
}MyPlasmaApplet::~MyPlasmaApplet()
{if (hasFailedToLaunch()) {// Do some cleanup here} else {// Save settings}
}void MyPlasmaApplet::init()
{QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(this);layout->setOrientation(Qt::Vertical); //so widgets will be stacked up/downm_lineEdit = new Plasma::LineEdit(this);m_lineEdit->setText("Hey! This is a Plasma line edit.");m_pushButton = new Plasma::PushButton(this);m_pushButton->setText("Whoa! This is a Plasma pushbutton.");layout->addItem(m_lineEdit);layout->addItem(m_pushButton);
}#include "myplasmaapplet.moc"

K_EXPORT_PLASMA_APPLET( <name>, <class> )

这是一个小而非常重要的部分,它将类名与.desktop文件中定义的小应用程序的名字联系在一起。

注:K_EXPORT_PLASMA_APPLET自动添加“plasma_applet_”前缀到名字上,所以在编辑.desktop文件时需要小心。

setBackgroundHints(DefaultBackground)

这是一个快速且更容易的绘制背景的函数接口,默认的Plasma背景将绘制在plasmoid的后面。这不但节省时间和代码,而且为用户创建了更一致的用户呈现。

Theinit() method

在构造函数中,你仅仅告诉plasma关于背景以及配置文件的一些信息(如果有的话)。也可以在构造函数中设置启动时的初始大小。之后,plasma将会处理任意的窗口缩放,你无需关注其大小。在init()方法中,你可以初始化任何需要初始化的数据,如读取配置数据。

hasFailedToLaunch()

如果由于某些原因,小应用程序未能成功启动(库未找到或是必要的硬件支持不存在等等),该方法会返回true。使用该方法可以为应用程序在退出之前提供清理的一个机会。

setFailedToLaunch(bool,QString)

当应用程序不能成功启动时,该函数会允许你通知plasma并给出一个可能的原因。plasma会绘制一个标准化的错误界面通知用户程序启动失败的原因,之后,你的应用程序也不会被调用去绘制自己。如果你的plasmoid变得更复杂,并且依赖更多因素,那么这是这好的清理办法。

dataUpdated(constQString &source, const Plasma::DataEngine::Data &data)

如果你想连接到任何一个plasma的DataEngine上,可以实现dataUpdated方法。当一个DataEngine直接连接到你的Applet子类时,当该DateEngine向你发送更新的数据时,dataUpdated方法会被调用。

决定applet的大小和几何属性:geometry() and contentsRect()

如果在你的代码中需要知道小应用程序的大小以及一些几何属性,调用contentsRect()和contentsRect().size()方法。避免调用geometry()和size()方法,因为它们没有将默认背景设置的空隙考虑进来。同样,避免使用绝对数字来设置位置如QPoint(0,0)以表示小应用程序的左上方,而是应当使用contentsRect().topLeft()。

通过CMakeLists.txt构建

CMakeLists.txt文件内容如下:

# Project Needs a name ofcourse
project(plasma-myplasmaapplet)# Find the required Libaries
find_package(KDE4 REQUIRED)
include(KDE4Defaults)add_definitions (${QT_DEFINITIONS} ${KDE4_DEFINITIONS})
include_directories(${CMAKE_SOURCE_DIR}${CMAKE_BINARY_DIR}${KDE4_INCLUDES})# We add our source code here
set(myplasmaapplet_SRCS myplasmaapplet.cpp)# Now make sure all files get to the right place
kde4_add_plugin(plasma_applet_myplasmaapplet ${myplasmaapplet_SRCS})
target_link_libraries(plasma_applet_myplasmaapplet ${KDE4_PLASMA_LIBS} ${KDE4_KDEUI_LIBS})install(TARGETS plasma_applet_myplasmaappletDESTINATION ${PLUGIN_INSTALL_DIR})install(FILES plasma-applet-myplasmaapplet.desktopDESTINATION ${SERVICES_INSTALL_DIR})

测试applet

如果你当前的开发环境不同于测试安装,运行cmake,并添加选项-DCMAKE_INSTALL_PREFIX=`kde4-config–prefix`。然后运行make以及sudo makeinstall,或者通过如下方式手动安装:

1.复制plasma_applet_myplasmaapplet.so到$KDEDIR/lib/kde4

2.复制plasma_applet_myplasmaapplet.desktop到$KDEDIR/share/kde4/services。

然后,运行kbuildsycoca4(这样,KDEapps就会知道新的desktop文件)。为了测试代码,可以使用plasmoidviewer程序:

kbuildsycoca4 #Needed once to let KDE know there is a new plugin
plasmoidviewer applet_name

你也可以在一个小桌面上观察你的Applet:

plasmoidviewer -c desktop applet_name

其中, applet_name就是.desktop文件中X-KDE-PluginInfo-Name对应的键值。

也可以重启plasma,这样新建的Applet可以在Applet浏览器中出现。

kbuildsycoca4
kquitapp plasma-desktop
plasma-desktop

转载于:https://my.oschina.net/fuyajun1983cn/blog/263829

kde Plasmoid Applet开发相关推荐

  1. 看一名 KDE 开发者如何使用 C++17 为项目提升巨大速度

    开发四年只会写业务代码,分布式高并发都不会还做程序员?   参与 KDE 和 openSUSE 开发的开源项目开发者 Antonio Larrosa 一直在独立开发着一个名为 Bard 的命令行音乐管 ...

  2. java applet 官网_java applet

    java applet[编辑] 概述 JavaApplet就是用Java语言编写的小应用程序,可以直接嵌入到网页中,并能够产生特殊的效果. 介绍 Java Applet就是用Java语言编写的一些小应 ...

  3. 安卓Android Studio开发IDE的安装

    安卓开发IDE的安装 1.进入官网 https://developer.android.google.cn/studio 可以在以下的操作系统开始 Android 应用程序开发: Microsoft® ...

  4. applet实现大文件ftp上传(一)

    由于要用APPLET实现大文件FTP上传下载,从网上搜索了几下,找到很多资料,最后决定采用基于 org.apache.commons.net.ftp包实现FTP上传下载,Net包中的类既提供对协议的底 ...

  5. Krita开发文档翻译——Introduction to Hacking Krita

    目录 介绍如何为开源项目Krita做贡献 从KDE软件开始 开始 构建Krita工程 在Krita代码库(CodeBase)的基础上工作 结构(Architecture) 集成开发环境(IDE) 资源 ...

  6. java智能卡开发_《Java智能卡原理与应用开发》PDF 下载

    图书目录: 封面 扉页 版权页 内容简介 出版说明 前言 序 目录 第一部分 Java智能卡编程基础 第1章 绪言 1.1 智能卡简介 1.2 Java智能卡简介 1.3 发展前景 1.3.1 智能卡 ...

  7. 不准再说linux丑,Ubuntu20.04+kde美化,动态桌面,软件安装

    ubuntu20.04美化教程,附加动态壁纸教程 安装ubuntu kde ubuntu官网下载好最新发行版ubuntu20.04,制作启动盘以最小安装,安装系统,然后替换/etc/apt/sourc ...

  8. ubuntu开发图形用户界面

    GUI 库有哪些 Windows 下的 GUI 库 Windows 下的 GUI 解决方案比较多: 基于 C++ 的有 Qt.MFC.WTL.wxWidgets.DirectUI.Htmlayout: ...

  9. java 用程序代码解释继承_关于初级java程序员笔试题

    关于初级java程序员笔试题 Sun 认证Java程序员考试内容涉及Java所有相关知识.编程概念及applet开发技巧.下面是小编整理的关于初级java程序员笔试题,欢迎大家参考! 第一题:判断题 ...

最新文章

  1. CS架构和BS架构的发展趋势即在图像处理软件中的应用
  2. P1903 [国家集训队]数颜色 / 维护队列
  3. python中csv模块读写文件
  4. php gd库 图片水印,php使用GD库实现文字图片水印及缩略图教程
  5. 雪花怎么画_平安夜怎么过?画个圣诞妆,你是最迷人嘎,你知道吗
  6. svn 创建 分支 branches
  7. linux终端中出现 cd: OLDPWD 未设定 的提示
  8. 2014 Super Training #2 F The Bridges of Kolsberg --DP
  9. springboot 通过@Value读取自定义属性文件变量获取结果为null
  10. CentOS7 编译安装LNMP
  11. Unity UGUI 屏幕适配
  12. Legend of Mir(传奇)官方源码学习1、运行游戏
  13. 关于机器人方面的sci论文_近十年机器人学科中国学者SCI十大发文期刊 - 论文投稿 - 小木虫 - 学术 科研 互动社区...
  14. 仿百度文库,office转pdf核心转换功能
  15. 20170918-20170924C#工作学习周总结
  16. php采集今日头条,用php蓝天采集器抓取今日头条ajax的文章内容
  17. Opencv创建纯色图
  18. PostMan发送请求参数带有路径特殊字符会返回400错误(与URL字符及URL编码值有关)
  19. 详细解读【虚拟内存】
  20. E. The Humanoid #834 div3

热门文章

  1. Emacs 24.3 配置JDEE(http://blog.csdn.net/csfreebird/article/details/19033939)
  2. ASP.NET常用的26个优化性能方法
  3. 如何正确的在项目中接入微信JS-SDK
  4. openlayers之style符号化
  5. .NET 判断进程是否运行 是否未响应
  6. 文本主题模型之非负矩阵分解(NMF)
  7. 日期类型存储成字符串类型的格式问题
  8. 数据库复习1——数据库体系结构和关系系统
  9. 用thinkphp进行微信开发的整体设计思考
  10. Javascript 评估用户输入密码的强度