PDA

View Full Version : subclass qpushbutton



Cremers
22nd June 2013, 18:39
I want to subclass QPushButton



somefile.h
//--------------------------------------
class Button : public QPushButton
{
Q_OBJECT
public:
Button(QWidget *parent);
Button(const QString &text, QWidget *parent);
};

somefile.cpp
/*------------------------------------------------------------------------------------*/
Button::Button(const QString & text, QWidget * parent = 0 )
{
this->setText(text);
}
/*------------------------------------------------------------------------------------*/
Button::Button(QWidget *parent) : QPushButton(parent)
{
}


However, I get 'no matching function for call to char[]' errors on code that calls them like this: okButton = new Button("Ok");
How is this done, it should convert the char[] somehow to const QString?
If I call them with okButton = new Button(QString("ok")); I also get the error because of the missing parent.
I'm obviously new at this.
Thanks.

Blubbz0r
22nd June 2013, 23:51
Not sure if it is a typo, but the
Button(const QString &text, QWidget *parent); function declared in the header file will need to have assigned the default value (0), not the implementation in your .cpp file.

Other than that, you have to invoke the base class constructor from the
Button::Button(const QString & text, QWidget * parent = 0 ) constructor, too:
Button::Button(const QString & text, QWidget * parent = 0 ) : QPushButton(text, parent).

ChrisW67
23rd June 2013, 01:47
However, I get 'no matching function for call to char[]' errors on code that calls them like this: okButton = new Button("Ok");
How is this done, it should convert the char[] somehow to const QString?Your class expects a QString and is being given a const char*. The compiler will try to find a QString constructor accepting a const char*, a conversion constructor, and construct a QString on your behalf. If there is no such constructor then your code generates an error like the one you describe. If your program is being built with QT_NO_CAST_FROM_ASCII defined then QString interfaces accepting C string literals are disabled and you need to explicitly do something like this:


Button b(QLatin1String("coffee"));
//or
Button b(QString::fromUtf8("кофе"));

Cremers
23rd June 2013, 08:08
Thanks for the help people.
I'm still not there.

In it's simplest form I try to make a subclass following Blubbz0r's advice:



header:
//--------------------------------------
class Button : public QPushButton
{
Q_OBJECT
public:
Button::Button(const QString & text, QWidget * parent = 0 );
};

cpp:
/*------------------------------------------------------------------------------------*/
Button::Button(const QString & text, QWidget * parent) : QPushButton(text, parent)
{
this->setText(text);
}


But I get an error, extra qualification on member Button.

I didn't even bother about the part about accepting char[] yet.

Could someone please just write out this little piece of code correctly for me, I learn best by by staring at correct syntax.
Thanks.

anda_skoa
23rd June 2013, 09:55
But I get an error, extra qualification on member Button.


You have Button::Button in your header inside class Button, but the constructor name should only be the class name


class Button : public QPushButton
{
Q_OBJECT
public:
Button(const QString & text, QWidget * parent = 0 );
};


Cheers,
_

Cremers
23rd June 2013, 12:07
Thanks, I got my subclass. For other qt newbies:



header:
//--------------------------------------
class Button : public QPushButton
{
Q_OBJECT
public:
Button(const QString & text, QWidget * parent = 0 );
};

cpp:
/*------------------------------------------------------------------------------------*/
Button::Button(const QString & text, QWidget * parent) : QPushButton(text, parent)
{
this->setText(text);
}


Now even new Button("Ok") works without errors, no complaints about char[] conversion.

ChrisW67
23rd June 2013, 22:17
For other qt newbies
This is a C++ newbies issue, nothing to do with Qt.