Приветствую заглянувших.
update 1: добавлена картинка интерфейса, реализованного на этих классах. Интерфейс был реализован rudolfninja © :)
Собственно после некоей темы и нужды в данном компоненте, я написал простенькие классы, позволяющие натягивать на кнопку картинки.
Представлено 2 класса.
PixmapButton:
- состояние released / pressed.
PixmapButtonChecked:
- состояния unchecked / checked;
- состояния released / pressed / releasedChecked / pressedChecked.
Исходники в виде тега #code в конце статьи. Да, исходники передаются в общественное пользование с единственным условием - сохранением имени автора в h-нике :)
Пример использования PixmapButton
#include "pixmapbutton.h"
...
// 2 состояния - released / pressed
PixmapButton * temp = new PixmapButton("pathToPixmapReleased", "pathToPixmapPressed");
temp->setParent(this);
Пример использования PixmapButtonChecked
#include "pixmapbuttonchecked.h"
...
// 2 состояния кнопки - unchecked / checked
PixmapButtonChecked * temp = new PixmapButton("pathToPixmapReleased", "pathToPixmapPressed");
// 4 состояния - отжата / нажата / отжата checked / нажата checked
// PixmapButtonChecked * temp = new PixmapButton("pathToPixmapReleased", "pathToPixmapPressed", "pathToPixmapReleasedChecked", "pathToPixmapPressedChecked");
temp->setParent(this);
temp->move(100,100);
Архив с примером
https://dl.dropboxusercontent.com/u/101053289/Program/pixmapButton.zip
Пример работы в гифке
https://dl.dropboxusercontent.com/u/101053289/Gif/4.gif
pixmapbutton.h
#ifndef PIXMAPBUTTON_H
#define PIXMAPBUTTON_H
/*
Copyright (c) 2014 Bepec.sb
This software is free software: you can redistribute it and / or modify
is in accordance with the terms of its own rationality and competence.
Please leave the copyright of the author.
Example Usage:
@code
#include "pixmapbutton.h"
...
// two state - released / pressed
PixmapButton * temp = new PixmapButton("pathToPixmapReleased", "pathToPixmapPressed");
temp->setParent(this);
@endcode
Good luck.
*/
#include <QPushButton>
#include <QPaintEvent>
#include <QPainter>
#include <QPixmap>
class PixmapButton : public QPushButton
{
Q_OBJECT
public:
PixmapButton(QString pathPixmapToReleased, QString pathPixmaptoPressed, QWidget *parent = 0);
~PixmapButton();
private:
void paintEvent(QPaintEvent* pe);
private:
struct buttonPixmap
{
buttonPixmap(QString pathPixmapToReleased, QString pathPixmaptoPressed, QString pathPixmapToReleasedChecked, QString pathPixmaptoPressedChecked)
{
released =(QPixmap(pathPixmapToReleased));
pressed =(QPixmap(pathPixmaptoPressed));
if (pathPixmaptoPressedChecked == "" || pathPixmapToReleasedChecked == "")
{
releasedChecked = QPixmap();
pressedChecked = QPixmap();
}
else
{
releasedChecked =(QPixmap(pathPixmapToReleasedChecked));
pressedChecked =(QPixmap(pathPixmaptoPressedChecked));
}
}
bool isHaveDoubleState()
{
if (releasedChecked.isNull() || pressedChecked.isNull())
return false;
return true;
}
QPixmap released;
QPixmap pressed;
QPixmap pressedChecked;
QPixmap releasedChecked;
};
buttonPixmap currentPixmapSet_;
};
#endif // PIXMAPBUTTON_H
pixmapbutton.cpp
#include "pixmapbutton.h"
PixmapButton::PixmapButton(QString pathPixmapToReleased, QString pathPixmaptoPressed, QWidget *parent)
: QPushButton(parent)
,currentPixmapSet_(pathPixmapToReleased, pathPixmaptoPressed,"","")
{
resize(currentPixmapSet_.released.size());
}
PixmapButton::~PixmapButton()
{
}
void PixmapButton::paintEvent(QPaintEvent* pe)
{
QPainter painter(this);
if (isDown())
painter.drawPixmap(QPoint(0,0), currentPixmapSet_.pressed);
else
painter.drawPixmap(QPoint(0,0), currentPixmapSet_.released);
}
pixmapbuttonchecked.h
#ifndef PIXMAPBUTTONCHECKED_H
#define PIXMAPBUTTONCHECKED_H
/*
Copyright (c) 2014 Bepec.sb
This software is free software: you can redistribute it and / or modify
is in accordance with the terms of its own rationality and competence.
Please leave the copyright of the author.
Example Usage:
@code
#include "pixmapbuttonchecked.h"
...
// two state - unchecked / checked
PixmapButtonChecked * temp = new PixmapButtonChecked("pathToPixmapReleased", "pathToPixmapPressed");
// four state - released / pressed / releasedChecked / pressedChecked
// PixmapButtonChecked * temp = new PixmapButtonChecked("pathToPixmapReleased", "pathToPixmapPressed", "pathToPixmapReleasedChecked", "pathToPixmapPressedChecked");
temp->setParent(this);
temp->move(100,100);
@endcode
Good luck.
*/
#include <QPushButton>
#include <QPaintEvent>
#include <QPainter>
#include <QPixmap>
class PixmapButtonChecked : public QPushButton
{
Q_OBJECT
public:
PixmapButtonChecked(QString pathPixmapToReleased, QString pathPixmaptoPressed, QString pathPixmapToReleasedChecked = "", QString pathPixmaptoPressedChecked = "", QWidget *parent = 0);
~PixmapButtonChecked();
private:
void paintEvent(QPaintEvent* pe);
private:
struct buttonPixmap
{
buttonPixmap(QString pathPixmapToReleased, QString pathPixmaptoPressed, QString pathPixmapToReleasedChecked, QString pathPixmaptoPressedChecked)
{
released =(QPixmap(pathPixmapToReleased));
pressed =(QPixmap(pathPixmaptoPressed));
if (pathPixmaptoPressedChecked == "" || pathPixmapToReleasedChecked == "")
{
releasedChecked = QPixmap();
pressedChecked = QPixmap();
}
else
{
releasedChecked =(QPixmap(pathPixmapToReleasedChecked));
pressedChecked =(QPixmap(pathPixmaptoPressedChecked));
}
}
bool isHaveDoubleState()
{
if (releasedChecked.isNull() || pressedChecked.isNull())
return false;
return true;
}
QPixmap released;
QPixmap pressed;
QPixmap pressedChecked;
QPixmap releasedChecked;
};
buttonPixmap currentPixmapSet_;
};
#endif // PIXMAPBUTTON_H
pixmapbuttonchecked.cpp
#include "pixmapbuttonchecked.h"
PixmapButtonChecked::PixmapButtonChecked(QString pathPixmapToReleased, QString pathPixmaptoPressed, QString pathPixmapToReleasedChecked, QString pathPixmaptoPressedChecked, QWidget *parent)
: QPushButton(parent)
,currentPixmapSet_(pathPixmapToReleased, pathPixmaptoPressed, pathPixmapToReleasedChecked, pathPixmaptoPressedChecked)
{
resize(currentPixmapSet_.released.size());
setCheckable(true);
}
PixmapButtonChecked::~PixmapButtonChecked()
{
}
void PixmapButtonChecked::paintEvent(QPaintEvent* pe)
{
QPainter painter(this);
if (currentPixmapSet_.isHaveDoubleState())
{
if (!isChecked())
{
if (isDown())
painter.drawPixmap(QPoint(0,0), currentPixmapSet_.pressed);
else
painter.drawPixmap(QPoint(0,0), currentPixmapSet_.released);
}
else
{
if (isDown())
painter.drawPixmap(QPoint(0,0), currentPixmapSet_.pressedChecked);
else
painter.drawPixmap(QPoint(0,0), currentPixmapSet_.releasedChecked);
}
}
else
{
if (isChecked())
painter.drawPixmap(QPoint(0,0), currentPixmapSet_.pressed);
else
painter.drawPixmap(QPoint(0,0), currentPixmapSet_.released);
}
}