Deriving From QPushButton
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!
Re: Deriving From QPushButton
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:
Code:
//class declaration (MyButton.h)
{
Q_OBJECT //don't forget this macro, or your signals/slots won't work
public:
virtual ~MyButton();
};
//class implementation (MyButton.cpp)
{
}
MyButton::~MyButton(){}
Re: Deriving From QPushButton