PDA

View Full Version : Subclassing QWidget and QPushButton



ber0y
24th July 2009, 09:36
Hello,
i have some problems by subclassing QWidget and QPushButton

the error is :


main.cpp: In member function `virtual void PushButton::paintEvent(QPaintEvent*)':
main.cpp:33: error: `QPaintDevice' is an ambiguous base of `PushButton'


The entire code is :


#include <QtCore/QDebug>

#include <QtGui/QApplication>
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>

class Widget : public QWidget
{
public:
Widget() : QWidget() {}
virtual void doTreatment() {qDebug() << "Treatment";}
};

class PushButton : public Widget, public QPushButton
{
public:
PushButton() : Widget(), QPushButton() {setText("Push Button");}
void paintEvent(QPaintEvent *event);
};

void PushButton::paintEvent(QPaintEvent *event)
{
QPushButton::paintEvent(event);

QPixmap pixmap("image.png");
QPoint point = (event->rect()).center();
point.setX(point.x() - pixmap.rect().center().x());
point.setY(point.y() - pixmap.rect().center().y());

QPainter painter(this);
painter.drawPixmap(point, pixmap);
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

Widget *widget = new PushButton();
widget->doTreatment();
delete widget;

return a.exec();
}


:confused:

If you have any ideas, thanks...

nish
24th July 2009, 09:37
you cannot inherit from two QObject clases...

EDIT
your widget class only has one virtual fucntion... so you can just skip the QWidget inheritance and the code will work.... but one more thing... inherit QPushButton first and then Widget otherwise u will get other errors..

EDIT2

do not delete the button in main()... your window will not show up..

ber0y
24th July 2009, 10:01
Thanks you !!!

My first problem was that i inherit Widget from QWidget to have access to QWidget's methods. I removed it and i just have to cast to have access to this.
Thanks again.

Here the new code :



#include <QtCore/QDebug>

#include <QtGui/QApplication>
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>

class Widget
{
public:
Widget() {}
virtual void doTreatment() {qDebug() << "Treatment";}
};

class PushButton : public QPushButton, public Widget
{
public:
PushButton() : QPushButton(), Widget() {setText("Push Button");}
void paintEvent(QPaintEvent *event);
};

void PushButton::paintEvent(QPaintEvent *event)
{
QPushButton::paintEvent(event);

QPixmap pixmap("image.png");
QPoint point = (event->rect()).center();
point.setX(point.x() - pixmap.rect().center().x());
point.setY(point.y() - pixmap.rect().center().y());

QPainter painter(this);
painter.drawPixmap(point, pixmap);
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

Widget *widget = new PushButton();
widget->doTreatment();
((PushButton*)widget)->show();

return a.exec();
}


:D

wagmare
24th July 2009, 10:02
void PushButton::paintEvent(QPaintEvent *event)

declare it as protected .. its a virtual function ..

nish
24th July 2009, 10:06
((PushButton*)widget)->show();

you dont have to cast it... widget->show() will do

ber0y
24th July 2009, 10:19
you dont have to cast it... widget->show() will do

No a Widget instance do not know the show method. The compiler refuses it

nish
24th July 2009, 10:23
No a Widget instance do not know the show method. The compiler refuses it

yeah... my fault... you have named your class as widget... that confused me...:):D

ber0y
24th July 2009, 10:25
yeah because when i created it i subclassed QWidget! ;-)
thanks wagmare too !