I decided to go ahead and add that additional functionality, as well as add a bunch of methods with the same name as the public QCheckBox methods for getting and setting. I also set up some signal forwarding, so you should be able to drop the RichTextCheckBox class in anywhere that QCheckBox is used, and still have everything work even though RichTextCheckBox does not inherit QCheckBox.
Here's my updated code. Hopefully the next time someone needs to display rich text in the label of a checkbox they will find this thread instead of all the other threads which, IMO, offer inadequate solutions.
clickablelabel.h
#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H
#include <QLabel>
class ClickableLabel
: public QLabel{
Q_OBJECT
public:
explicit ClickableLabel
(QWidget *parent
= 0);
signals:
void released();
void pressed();
void left();
public slots:
};
#endif // CLICKABLELABEL_H
#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H
#include <QLabel>
class ClickableLabel : public QLabel
{
Q_OBJECT
public:
explicit ClickableLabel(QWidget *parent = 0);
void mouseReleaseEvent(QMouseEvent*);
void mousePressEvent(QMouseEvent*);
void mouseMoveEvent(QMouseEvent*);
signals:
void released();
void pressed();
void left();
public slots:
};
#endif // CLICKABLELABEL_H
To copy to clipboard, switch view to plain text mode
clickablelabel.cpp
#include "clickablelabel.h"
#include <QMouseEvent>
ClickableLabel
::ClickableLabel(QWidget *parent
) :{
}
void ClickableLabel
::mouseReleaseEvent(QMouseEvent* event
){ if(contentsRect().contains(event->pos())){
if(event->buttons() == Qt::LeftButton){
emit released();
}else if(event->button() == Qt::LeftButton){
emit released();
}
}
}
void ClickableLabel
::mousePressEvent(QMouseEvent* event
){ if(event->buttons() == Qt::LeftButton){
emit pressed();
}else if(event->button() == Qt::LeftButton){
emit pressed();
}
}
void ClickableLabel
::mouseMoveEvent(QMouseEvent* event
){ if(contentsRect().contains(event->pos())&&
(event->buttons() == Qt::LeftButton
|| event->button() == Qt::LeftButton)){
emit left();
}
}
#include "clickablelabel.h"
#include <QMouseEvent>
ClickableLabel::ClickableLabel(QWidget *parent) :
QLabel(parent)
{
}
void ClickableLabel::mouseReleaseEvent(QMouseEvent* event){
if(contentsRect().contains(event->pos())){
if(event->buttons() == Qt::LeftButton){
emit released();
}else if(event->button() == Qt::LeftButton){
emit released();
}
}
}
void ClickableLabel::mousePressEvent(QMouseEvent* event){
if(event->buttons() == Qt::LeftButton){
emit pressed();
}else if(event->button() == Qt::LeftButton){
emit pressed();
}
}
void ClickableLabel::mouseMoveEvent(QMouseEvent* event){
if(contentsRect().contains(event->pos())&&
(event->buttons() == Qt::LeftButton
|| event->button() == Qt::LeftButton)){
emit left();
}
}
To copy to clipboard, switch view to plain text mode
hovercheckbox.h
#ifndef HOVERCHECKBOX_H
#define HOVERCHECKBOX_H
#include <QCheckBox>
{
Q_OBJECT
public:
explicit HoverCheckBox
(QWidget *parent
= 0);
private:
bool isHover;
bool isPressed;
public slots:
void setHover();
void setPressed();
void clearPressed();
void clearStates();
};
#endif // HOVERCHECKBOX_H
#ifndef HOVERCHECKBOX_H
#define HOVERCHECKBOX_H
#include <QCheckBox>
class HoverCheckBox : public QCheckBox
{
Q_OBJECT
public:
explicit HoverCheckBox(QWidget *parent = 0);
private:
virtual void paintEvent(QPaintEvent *);
bool isHover;
bool isPressed;
public slots:
void setHover();
void setPressed();
void clearPressed();
void clearStates();
};
#endif // HOVERCHECKBOX_H
To copy to clipboard, switch view to plain text mode
hovercheckbox.cpp
#include "hovercheckbox.h"
#include <QStyleOptionButton>
#include <QStylePainter>
HoverCheckBox
::HoverCheckBox(QWidget *parent
) :{
isHover = false;
}
initStyleOption(&opt);
if(isHover){
opt.
state |
= QStyle::State_MouseOver;
}
if(isPressed){
opt.
state |
= QStyle::State_Sunken;
}
p.
drawControl(QStyle::CE_CheckBox, opt
);
}
void HoverCheckBox::setHover(){
isHover = true;
update();
}
void HoverCheckBox::setPressed(){
isPressed = true;
update();
}
void HoverCheckBox::clearPressed(){
isPressed = false;
update();
}
void HoverCheckBox::clearStates(){
isHover = false;
isPressed = false;
update();
}
#include "hovercheckbox.h"
#include <QStyleOptionButton>
#include <QStylePainter>
HoverCheckBox::HoverCheckBox(QWidget *parent) :
QCheckBox(parent)
{
isHover = false;
}
void HoverCheckBox::paintEvent(QPaintEvent *){
QStylePainter p(this);
QStyleOptionButton opt;
initStyleOption(&opt);
if(isHover){
opt.state |= QStyle::State_MouseOver;
}
if(isPressed){
opt.state |= QStyle::State_Sunken;
}
p.drawControl(QStyle::CE_CheckBox, opt);
}
void HoverCheckBox::setHover(){
isHover = true;
update();
}
void HoverCheckBox::setPressed(){
isPressed = true;
update();
}
void HoverCheckBox::clearPressed(){
isPressed = false;
update();
}
void HoverCheckBox::clearStates(){
isHover = false;
isPressed = false;
update();
}
To copy to clipboard, switch view to plain text mode
richtextcheckbox.h
#ifndef RICHTEXTCHECKBOX_H
#define RICHTEXTCHECKBOX_H
#include <QWidget>
class HoverCheckBox;
class ClickableLabel;
class RichTextCheckBox
: public QWidget{
Q_OBJECT
public:
HoverCheckBox* getCheckBox(){return checkBox;}
Qt::CheckState checkState() const;
bool isChecked() const;
void clearStates();
private:
HoverCheckBox* checkBox;
ClickableLabel* label;
virtual void enterEvent
(QEvent*);
virtual void leaveEvent
(QEvent*);
signals:
void toggled(bool);
void clicked();
public slots:
void setChecked(bool);
void setCheckState(Qt::CheckState state);
void emitToggled(bool);
void emitClicked();
};
#endif // RICHTEXTCHECKBOX_H
#ifndef RICHTEXTCHECKBOX_H
#define RICHTEXTCHECKBOX_H
#include <QWidget>
class HoverCheckBox;
class QCheckBox;
class ClickableLabel;
class RichTextCheckBox : public QWidget
{
Q_OBJECT
public:
explicit RichTextCheckBox(QString text, QWidget *parent = 0);
HoverCheckBox* getCheckBox(){return checkBox;}
QString text() const;
QString plainText() const;
Qt::CheckState checkState() const;
bool isChecked() const;
void clearStates();
private:
HoverCheckBox* checkBox;
ClickableLabel* label;
virtual void enterEvent(QEvent*);
virtual void leaveEvent(QEvent*);
signals:
void toggled(bool);
void clicked();
public slots:
void setChecked(bool);
void setCheckState(Qt::CheckState state);
void emitToggled(bool);
void emitClicked();
};
#endif // RICHTEXTCHECKBOX_H
To copy to clipboard, switch view to plain text mode
richtextcheckbox.cpp
#include "richtextcheckbox.h"
#include "hovercheckbox.h"
#include "clickablelabel.h"
#include <QMouseEvent>
#include <QLabel>
#include <QLayout>
#include <QTextDocument>
{
setMouseTracking(true);
checkBox = new HoverCheckBox;
label = new ClickableLabel;
label->setTextFormat(Qt::RichText);
label->setText(text);
layout->setMargin(0);
layout->addWidget(checkBox);
layout->addWidget(label);
layout->setAlignment(Qt::AlignLeft);
setLayout(layout);
connect(label, SIGNAL(released()), checkBox, SLOT(toggle()));
connect(label, SIGNAL(released()), checkBox, SLOT(clearPressed()));
connect(label, SIGNAL(pressed()), checkBox, SLOT(setPressed()));
connect(label, SIGNAL(left()), checkBox, SLOT(clearStates()));
connect(checkBox, SIGNAL(toggled(bool)),this, SLOT(emitToggled(bool)));
connect(checkBox, SIGNAL(clicked()), this, SLOT(emitClicked()));
connect(label, SIGNAL(released()), this, SLOT(emitClicked()));
}
void RichTextCheckBox::emitClicked(){
emit clicked();
}
void RichTextCheckBox::emitToggled(bool b){
emit toggled(b);
}
bool RichTextCheckBox::isChecked() const{
return checkBox->isChecked();
}
Qt::CheckState RichTextCheckBox::checkState() const{
return checkBox->checkState();
}
QString RichTextCheckBox
::text() const{ return label->text();
}
QString RichTextCheckBox
::plainText() const{ doc.setHtml(label->text());
return doc.toPlainText();
}
void RichTextCheckBox::clearStates(){
checkBox->clearStates();
}
void RichTextCheckBox::setCheckState(Qt::CheckState state){
checkBox->setCheckState(state);
}
void RichTextCheckBox::setChecked(bool value){
checkBox->setChecked(value);
}
void RichTextCheckBox
::enterEvent(QEvent* /*event*/){ checkBox->setHover();
}
void RichTextCheckBox
::leaveEvent(QEvent* /*event*/){ checkBox->clearStates();
}
#include "richtextcheckbox.h"
#include "hovercheckbox.h"
#include "clickablelabel.h"
#include <QMouseEvent>
#include <QLabel>
#include <QLayout>
#include <QTextDocument>
RichTextCheckBox::RichTextCheckBox(QString text, QWidget *parent) :
QWidget(parent)
{
setMouseTracking(true);
checkBox = new HoverCheckBox;
label = new ClickableLabel;
label->setTextFormat(Qt::RichText);
label->setText(text);
QHBoxLayout* layout = new QHBoxLayout;
layout->setMargin(0);
layout->addWidget(checkBox);
layout->addWidget(label);
layout->setAlignment(Qt::AlignLeft);
setLayout(layout);
connect(label, SIGNAL(released()), checkBox, SLOT(toggle()));
connect(label, SIGNAL(released()), checkBox, SLOT(clearPressed()));
connect(label, SIGNAL(pressed()), checkBox, SLOT(setPressed()));
connect(label, SIGNAL(left()), checkBox, SLOT(clearStates()));
connect(checkBox, SIGNAL(toggled(bool)),this, SLOT(emitToggled(bool)));
connect(checkBox, SIGNAL(clicked()), this, SLOT(emitClicked()));
connect(label, SIGNAL(released()), this, SLOT(emitClicked()));
}
void RichTextCheckBox::emitClicked(){
emit clicked();
}
void RichTextCheckBox::emitToggled(bool b){
emit toggled(b);
}
bool RichTextCheckBox::isChecked() const{
return checkBox->isChecked();
}
Qt::CheckState RichTextCheckBox::checkState() const{
return checkBox->checkState();
}
QString RichTextCheckBox::text() const{
return label->text();
}
QString RichTextCheckBox::plainText() const{
QTextDocument doc;
doc.setHtml(label->text());
return doc.toPlainText();
}
void RichTextCheckBox::clearStates(){
checkBox->clearStates();
}
void RichTextCheckBox::setCheckState(Qt::CheckState state){
checkBox->setCheckState(state);
}
void RichTextCheckBox::setChecked(bool value){
checkBox->setChecked(value);
}
void RichTextCheckBox::enterEvent(QEvent* /*event*/){
checkBox->setHover();
}
void RichTextCheckBox::leaveEvent(QEvent* /*event*/){
checkBox->clearStates();
}
To copy to clipboard, switch view to plain text mode
Bookmarks