Results 1 to 7 of 7

Thread: subclass qpushbutton

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

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

    Cremers (23rd June 2013)

  3. #2
    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.

  4. #3
    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,
    _

  5. The following user says thank you to anda_skoa 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, 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.

  7. #5
    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
  •  
Qt is a trademark of The Qt Company.