Results 1 to 7 of 7

Thread: subclass qpushbutton

  1. #1
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default subclass qpushbutton

    I want to subclass QPushButton

    Qt Code:
    1. somefile.h
    2. //--------------------------------------
    3. class Button : public QPushButton
    4. {
    5. Q_OBJECT
    6. public:
    7. Button(QWidget *parent);
    8. Button(const QString &text, QWidget *parent);
    9. };
    10.  
    11. somefile.cpp
    12. /*------------------------------------------------------------------------------------*/
    13. Button::Button(const QString & text, QWidget * parent = 0 )
    14. {
    15. this->setText(text);
    16. }
    17. /*------------------------------------------------------------------------------------*/
    18. Button::Button(QWidget *parent) : QPushButton(parent)
    19. {
    20. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by Cremers; 22nd June 2013 at 18:52.

  2. #2
    Join Date
    Jun 2013
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: subclass qpushbutton

    Not sure if it is a typo, but the
    Qt Code:
    1. Button(const QString &text, QWidget *parent);
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. Button::Button(const QString & text, QWidget * parent = 0 )
    To copy to clipboard, switch view to plain text mode 
    constructor, too:
    Qt Code:
    1. Button::Button(const QString & text, QWidget * parent = 0 ) : QPushButton(text, parent)
    To copy to clipboard, switch view to plain text mode 
    .

  3. The following user says thank you to Blubbz0r for this useful post:

    Cremers (23rd June 2013)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: subclass qpushbutton

    Quote Originally Posted by Cremers View Post
    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:
    Qt Code:
    1. Button b(QLatin1String("coffee"));
    2. //or
    3. Button b(QString::fromUtf8("кофе"));
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to ChrisW67 for this useful post:

    Cremers (23rd June 2013)

  6. #4
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: subclass qpushbutton

    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:

    Qt Code:
    1. header:
    2. //--------------------------------------
    3. class Button : public QPushButton
    4. {
    5. Q_OBJECT
    6. public:
    7. Button::Button(const QString & text, QWidget * parent = 0 );
    8. };
    9.  
    10. cpp:
    11. /*------------------------------------------------------------------------------------*/
    12. Button::Button(const QString & text, QWidget * parent) : QPushButton(text, parent)
    13. {
    14. this->setText(text);
    15. }
    To copy to clipboard, switch view to plain text mode 

    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.

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: subclass qpushbutton

    Quote Originally Posted by Cremers View Post
    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
    Qt Code:
    1. class Button : public QPushButton
    2. {
    3. Q_OBJECT
    4. public:
    5. Button(const QString & text, QWidget * parent = 0 );
    6. };
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    Cremers (23rd June 2013)

  9. #6
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: subclass qpushbutton

    Thanks, I got my subclass. For other qt newbies:

    Qt Code:
    1. header:
    2. //--------------------------------------
    3. class Button : public QPushButton
    4. {
    5. Q_OBJECT
    6. public:
    7. Button(const QString & text, QWidget * parent = 0 );
    8. };
    9.  
    10. cpp:
    11. /*------------------------------------------------------------------------------------*/
    12. Button::Button(const QString & text, QWidget * parent) : QPushButton(text, parent)
    13. {
    14. this->setText(text);
    15. }
    To copy to clipboard, switch view to plain text mode 

    Now even new Button("Ok") works without errors, no complaints about char[] conversion.
    Last edited by Cremers; 23rd June 2013 at 16:48.

  10. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: subclass qpushbutton

    For other qt newbies
    This is a C++ newbies issue, nothing to do with Qt.

Similar Threads

  1. Subclass QList<T>
    By Seishin in forum Qt Programming
    Replies: 4
    Last Post: 23rd April 2013, 23:22
  2. Replies: 2
    Last Post: 15th April 2013, 06:33
  3. Replies: 8
    Last Post: 12th February 2010, 02:41
  4. Subclass
    By merry in forum General Programming
    Replies: 2
    Last Post: 1st March 2007, 10:34
  5. Reg - Subclass of QListBoxItem
    By lawrence in forum Newbie
    Replies: 1
    Last Post: 6th January 2007, 14:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.