Hello,
I am trying to create a custom Button of my own in Qt4. What I want it to do is to display an open switch and a closed switch respectively, depending on whether it has been pressed by the user. Like a QCheckBox, but with different graphics. Two possible solutions come to mind:
1.) Create a subclass of QAbstractButton.
I have tried to do this and have achieved some success, however I still have problems. For instance, I can display my own Button within another QWidget, but I cannot add it to any kind of layout (QGridLayout and the like). It is not visible then. I thought this could be a problem with the class I've written not being a subclass of QWidget but of QAbstractButton. But since QAbstractButton is a subclass of QWidget itself, I don't know what I am doing wrong here.
Here is the source code of my own Button class:
File: MyButton.h
#ifndef _MYBUTTON
#define _MYBUTTON
#include <QAbstractButton>
{
Q_OBJECT
public:
signals:
void valueChanged(int newValue);
};
#endif
#ifndef _MYBUTTON
#define _MYBUTTON
#include <QAbstractButton>
class MyButton : public QAbstractButton
{
Q_OBJECT
public:
MyButton(QWidget *parent = 0);
void paintEvent(QPaintEvent*);
signals:
void valueChanged(int newValue);
};
#endif
To copy to clipboard, switch view to plain text mode
File: MyButton.cpp
#include "MyButton.h"
#include <QtGui>
MyButton
::MyButton(QWidget *parent
){
setCheckable(true);
setChecked(false);
}
{
myPen.setWidth(2);
if(isChecked())
{
myPen.setColor(Qt::black);
painter.setPen(myPen);
painter.drawLine(20,29,50,20);
int a = 1;
emit valueChanged(a);
}
else
{
myPen.setColor(Qt::darkGray);
painter.setPen(myPen);
painter.drawLine(20,29,30,0);
int b = 0;
emit valueChanged(b);
}
painter.drawLine(0,30,20,30);
painter.drawLine(50,30,70,30);
painter.drawLine(50,30,50,20);
}
#include "MyButton.h"
#include <QtGui>
MyButton::MyButton(QWidget *parent)
: QAbstractButton(parent)
{
setCheckable(true);
setChecked(false);
}
void MyButton::paintEvent(QPaintEvent*)
{
QPainter painter(this);
QPen myPen;
myPen.setWidth(2);
if(isChecked())
{
myPen.setColor(Qt::black);
painter.setPen(myPen);
painter.drawLine(20,29,50,20);
int a = 1;
emit valueChanged(a);
}
else
{
myPen.setColor(Qt::darkGray);
painter.setPen(myPen);
painter.drawLine(20,29,30,0);
int b = 0;
emit valueChanged(b);
}
painter.drawLine(0,30,20,30);
painter.drawLine(50,30,70,30);
painter.drawLine(50,30,50,20);
}
To copy to clipboard, switch view to plain text mode
2.) Another possibility would be to use the QCheckBox class and just change its graphical appearance by using QIcons. I tried that just a few minutes ago but somehow I can't seem to get it to display another pixmap. Here is a small example that does compile and run but obviously is not enough to change the QCheckBox's graphical appearance:
#include <QtGui>
int main(int argc, char *argv[])
{
button
->setIcon
(QIcon("CustomIcon.png"));
button->show();
return app.exec();
}
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QCheckBox *button = new QCheckBox;
button->setIcon(QIcon("CustomIcon.png"));
button->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Any help on this matter is highly welcome. :-)
Bookmarks