//QCheckBoxEx.h
#include <QtGui/QtGui>
class QCheckBoxEx : public QAbstractButton
{
public:
QCheckBoxEx(QWidget* parent);
bool init(QString yes,QString no);
private:
QPixmap m_pixmap[2];
protected:
void paintEvent(QPaintEvent *event);
};
//QCheckBoxEx.cpp
#include "QCheckBoxEx.h"
QCheckBoxEx::QCheckBoxEx(QWidget* parent)
{
setParent(parent);
setCheckable(true);
}
bool QCheckBoxEx::init(QString yes,QString no)
{
m_pixmap[0].load(no);
m_pixmap[1].load(yes);
int w = m_pixmap[0].width();
int h = m_pixmap[0].height();
if (w && h && w== m_pixmap[1].width() && h==m_pixmap[1].height())
{
resize(w,h);
return true;
}
return false;
}
void QCheckBoxEx::paintEvent(QPaintEvent *event)
{
QPainter p(this);
if (isChecked())
{
p.drawPixmap(0,0,m_pixmap[1]);
}
else{
p.drawPixmap(0,0,m_pixmap[0]);
}
}
//使用
m_check = new QCheckBoxEx(this);
m_check->init("d:\\1.jpg","d:\\0.jpg");
//m_check->setChecked(true);