我最近尝试为KDE4编写动画壁纸插件。当我在它们之间切换时,动画会混乱。也就是说,先前的动画不会停止,并且先前的帧和当前帧混合在一起。我将帧嵌入到每个程序中作为图像资源。然后我为每个壁纸编写如下代码。任何人都可以告诉我我错过了什么(或者它只是一个KDE错误)?
dancingcoffee.h
#ifndef DANCINGCOFFEE_H
#define DANCINGCOFFEE_H
#include <Plasma/Wallpaper>
#include <QPainter>
#include <QTimer>
class DancingCoffee : public Plasma::Wallpaper
{
Q_OBJECT
public:
DancingCoffee(QObject *parent, const QVariantList& args);
void paint(QPainter *painter, const QRectF &exposedRect);
private slots:
void updateBackground();
private:
const int m_frames;
const int m_interval;
int m_frame_number;
QTimer m_timer;
};
#endif // DANCINGCOFFEE_H
dancingcoffee.cpp
#include "dancingcoffee.h"
#include <QImage>
K_EXPORT_PLASMA_WALLPAPER(coffee, DancingCoffee)
DancingCoffee::DancingCoffee(QObject *parent, const QVariantList &args) :
Plasma::Wallpaper(parent, args),
m_frames(2),
m_interval(1000 / m_frames)
{
m_frame_number = 0;
m_timer.setSingleShot(true);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateBackground()));
m_timer.start(m_interval);
}
void DancingCoffee::paint(QPainter *painter, const QRectF &exposedRect)
{
QString name = QString(":/images/frame-%1.png").arg(m_frame_number);
QImage image(name);
painter->drawImage(exposedRect, image);
}
void DancingCoffee::updateBackground()
{
++m_frame_number;
m_frame_number %= m_frames;
m_timer.start(m_interval);
emit update(boundingRect());
}
的CMakeLists.txt
set(CMAKE_INSTALL_PREFIX /usr)
project(plasma-wallpaper-coffee)
cmake_minimum_required(VERSION 2.8)
find_package(KDE4 REQUIRED)
find_package(KDE4Workspace REQUIRED)
include(KDE4Defaults)
add_definitions(${KDE4_DEFINITIONS})
include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${KDE4_INCLUDES})
set(TARGET plasma_wallpaper_coffee)
set(SRC_LIST dancingcoffee.cpp)
qt4_add_resources(RCC_LIST dancingcoffee.qrc)
kde4_add_plugin(${TARGET} ${SRC_LIST} ${RCC_LIST})
target_link_libraries(${TARGET} ${KDE4_PLASMA_LIBS})
install(TARGETS ${TARGET} DESTINATION ${PLUGIN_INSTALL_DIR})
install(FILES plasma-wallpaper-coffee.desktop DESTINATION ${SERVICES_INSTALL_DIR})
等离子体壁纸-coffee.desktop
[Desktop Entry]
Name=Coffee
Type=Service
Icon=image-jpeg
ServiceTypes=Plasma/Wallpaper
X-KDE-Library=plasma_wallpaper_coffee
X-KDE-PluginInfo-Author=Chris French
X-KDE-PluginInfo-Email=unclechromedome@gmail.com
X-KDE-PluginInfo-Name=coffee
X-KDE-PluginInfo-Version=0.1
X-KDE-PluginInfo-Website=
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true
瞧!我桌面上的动画壁纸。但壁纸不能很好地融合在一起。当我切换壁纸时,我必须杀死桌面并重新启动它,有时甚至不起作用。有时我必须重新启动。这对我来说很痛苦,如果我分发壁纸,这是不可接受的。