Probleme mit Custom Style Plugins
Verfasst: 14. April 2008 19:00
Hallo,
ich habe versucht ein Cumstom Style Plugin zu erzeugen.
Zuerst habe ich einen Custom Style geschrieben und getestet.
Dann habe ich daraus eine DLL erzeugt und in den Stylewindow Ordner von Styles kopiert, wie es im Handbuch beschrieben wird.
Dann versuchte ich ihn in einem Programm dynamisch zu laden.
Leider änderte sich nichts.
Was könnte ich falschgemacht haben?
Ich habe es erstmal mit ganz einfachen Beispielen aus dem Handbuch versucht:
customstyle.h
//////////////////////////////////////
#include <QtCore>
#include <QtGui>
#include "customstyleplugin.h"
#include "customstyle.h"
QStringList CustomStylePlugin::keys() const
{
return QStringList() << "CustomStyle";
}
QStyle *CustomStylePlugin::create(const QString &key)
{
if (key.toLower() == "customstyle")
return new CustomStyle;
return 0;
}
Q_EXPORT_PLUGIN2(customstyleplugin, CustomStylePlugin)
////////////////////////////////////////////////////////////////////////////
customstyle.cpp
///////////////////////////////////////////////////////////////////////
#include "customstyle.h"
void CustomStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option,
QPainter *painter, const QWidget *widget) const
{
if (element == PE_IndicatorSpinUp || element == PE_IndicatorSpinDown) {
QPolygon points(3);
int x = option->rect.x();
int y = option->rect.y();
int w = option->rect.width() / 2;
int h = option->rect.height() / 2;
x += (option->rect.width() - w) / 2;
y += (option->rect.height() - h) / 2;
if (element == PE_IndicatorSpinUp) {
points[0] = QPoint(x, y + h);
points[1] = QPoint(x + w, y + h);
points[2] = QPoint(x + w / 2, y);
} else { // PE_SpinBoxDown
points[0] = QPoint(x, y);
points[1] = QPoint(x + w, y);
points[2] = QPoint(x + w / 2, y + h);
}
if (option->state & State_Enabled) {
painter->setPen(option->palette.mid().color());
painter->setBrush(option->palette.buttonText());
} else {
painter->setPen(option->palette.buttonText().color());
painter->setBrush(option->palette.mid());
}
painter->drawPolygon(points);
} else {
QWindowsStyle::drawPrimitive(element, option, painter, widget);
}
}
////////////////////////////////////////////////////////////////////////////////
customstyleplugin.h
///////////////////////////////////////////////////////////////////////////////
#ifndef CUSTOMSTYLEPLUGIN_H
#define CUSTOMSTYLEPLUGIN_H
#include <QStylePlugin>
class QStringList;
class QStyle;
class CustomStylePlugin : public QStylePlugin
{
Q_OBJECT
public:
CustomStylePlugin() {};
QStringList keys() const;
QStyle *create(const QString &key);
};
#endif // CUSTOMSTYLEPLUGIN_H
////////////////////////////////////////////////////////////////////////////////
customstyleplugin.cpp
/////////////////////////////////////////////////////////////////////////////////
#include <QtCore>
#include <QtGui>
#include "customstyleplugin.h"
#include "customstyle.h"
QStringList CustomStylePlugin::keys() const
{
return QStringList() << "CustomStyle";
}
QStyle *CustomStylePlugin::create(const QString &key)
{
if (key.toLower() == "customstyle")
return new CustomStyle;
return 0;
}
Q_EXPORT_PLUGIN2(customstyleplugin, CustomStylePlugin)
////////////////////////////////////////////////////////////////////////
Die dll habe ich in den Ordner styles->Stylewindow und später testweise in den Ordner plugins->designer kopiert.
Dann habe ich noch ein Widget mit einer Spinbox geschrieben,
und ein Standard-Main-Program um die folgende Code-Zeile ergänzt:
QApplication::setStyle(QStyleFactory::create("customstyle"));
Hat jemand eine idee?
ich habe versucht ein Cumstom Style Plugin zu erzeugen.
Zuerst habe ich einen Custom Style geschrieben und getestet.
Dann habe ich daraus eine DLL erzeugt und in den Stylewindow Ordner von Styles kopiert, wie es im Handbuch beschrieben wird.
Dann versuchte ich ihn in einem Programm dynamisch zu laden.
Leider änderte sich nichts.
Was könnte ich falschgemacht haben?
Ich habe es erstmal mit ganz einfachen Beispielen aus dem Handbuch versucht:
customstyle.h
//////////////////////////////////////
#include <QtCore>
#include <QtGui>
#include "customstyleplugin.h"
#include "customstyle.h"
QStringList CustomStylePlugin::keys() const
{
return QStringList() << "CustomStyle";
}
QStyle *CustomStylePlugin::create(const QString &key)
{
if (key.toLower() == "customstyle")
return new CustomStyle;
return 0;
}
Q_EXPORT_PLUGIN2(customstyleplugin, CustomStylePlugin)
////////////////////////////////////////////////////////////////////////////
customstyle.cpp
///////////////////////////////////////////////////////////////////////
#include "customstyle.h"
void CustomStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option,
QPainter *painter, const QWidget *widget) const
{
if (element == PE_IndicatorSpinUp || element == PE_IndicatorSpinDown) {
QPolygon points(3);
int x = option->rect.x();
int y = option->rect.y();
int w = option->rect.width() / 2;
int h = option->rect.height() / 2;
x += (option->rect.width() - w) / 2;
y += (option->rect.height() - h) / 2;
if (element == PE_IndicatorSpinUp) {
points[0] = QPoint(x, y + h);
points[1] = QPoint(x + w, y + h);
points[2] = QPoint(x + w / 2, y);
} else { // PE_SpinBoxDown
points[0] = QPoint(x, y);
points[1] = QPoint(x + w, y);
points[2] = QPoint(x + w / 2, y + h);
}
if (option->state & State_Enabled) {
painter->setPen(option->palette.mid().color());
painter->setBrush(option->palette.buttonText());
} else {
painter->setPen(option->palette.buttonText().color());
painter->setBrush(option->palette.mid());
}
painter->drawPolygon(points);
} else {
QWindowsStyle::drawPrimitive(element, option, painter, widget);
}
}
////////////////////////////////////////////////////////////////////////////////
customstyleplugin.h
///////////////////////////////////////////////////////////////////////////////
#ifndef CUSTOMSTYLEPLUGIN_H
#define CUSTOMSTYLEPLUGIN_H
#include <QStylePlugin>
class QStringList;
class QStyle;
class CustomStylePlugin : public QStylePlugin
{
Q_OBJECT
public:
CustomStylePlugin() {};
QStringList keys() const;
QStyle *create(const QString &key);
};
#endif // CUSTOMSTYLEPLUGIN_H
////////////////////////////////////////////////////////////////////////////////
customstyleplugin.cpp
/////////////////////////////////////////////////////////////////////////////////
#include <QtCore>
#include <QtGui>
#include "customstyleplugin.h"
#include "customstyle.h"
QStringList CustomStylePlugin::keys() const
{
return QStringList() << "CustomStyle";
}
QStyle *CustomStylePlugin::create(const QString &key)
{
if (key.toLower() == "customstyle")
return new CustomStyle;
return 0;
}
Q_EXPORT_PLUGIN2(customstyleplugin, CustomStylePlugin)
////////////////////////////////////////////////////////////////////////
Die dll habe ich in den Ordner styles->Stylewindow und später testweise in den Ordner plugins->designer kopiert.
Dann habe ich noch ein Widget mit einer Spinbox geschrieben,
und ein Standard-Main-Program um die folgende Code-Zeile ergänzt:
QApplication::setStyle(QStyleFactory::create("customstyle"));
Hat jemand eine idee?