PDA

View Full Version : Deriving From QPushButton



Sonic7145
4th March 2009, 00:56
Hello,

I'm learning about inheritance, so I tried to derive a class from QPushButton called myButton.

Here is the header file:
#ifndef _MY_BUTTON_H_
#define _MY_BUTTON_H_
#include <QPushButton>
class myButton : public QPushButton
{
};
#endif


In my main.cpp, when I do myButton *Button1 = new myButton("Hello, World!");
it simply doesn't work. The error is:
main.cpp:11: error: no matching function for call to 'myButton::myButton(const char [14])'
mybutton.h:5: note: candidates are: myButton::myButton()
mybutton.h:5: note: myButton::myButton(const myButton&)

I included my .h file.

Any input would be appreciated!

JimDaniel
4th March 2009, 05:47
You have provided no constructor for the class, so the compiler has generated one for you: myButton::myButton(); And it takes no string, which is why it throws an error.

For what you want to happen, you need to provide a constructor which takes a const reference to a QString and in the constructor's implementation pass it along to the base QPushButton class's constructor, like this:


//class declaration (MyButton.h)
class MyButton : public QPushButton
{
Q_OBJECT //don't forget this macro, or your signals/slots won't work

public:
MyButton(const QString & text, QWidget * parent = 0);
virtual ~MyButton();
};

//class implementation (MyButton.cpp)
MyButton::MyButton(const QString & text, QWidget * parent) : QPushButton(text, parent)
{
}

MyButton::~MyButton(){}

Sonic7145
4th March 2009, 05:56
Thank you, that worked!