Results 1 to 3 of 3

Thread: Deriving From QPushButton

  1. #1
    Join Date
    Mar 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question 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!

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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:
    Qt Code:
    1. //class declaration (MyButton.h)
    2. class MyButton : public QPushButton
    3. {
    4. Q_OBJECT //don't forget this macro, or your signals/slots won't work
    5.  
    6. public:
    7. MyButton(const QString & text, QWidget * parent = 0);
    8. virtual ~MyButton();
    9. };
    10.  
    11. //class implementation (MyButton.cpp)
    12. MyButton::MyButton(const QString & text, QWidget * parent) : QPushButton(text, parent)
    13. {
    14. }
    15.  
    16. MyButton::~MyButton(){}
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Deriving From QPushButton

    Thank you, that worked!

Similar Threads

  1. QPushButton not calling repaint when needed?
    By Enygma in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2010, 18:03
  2. Change QPushButton Background Color, Qt4.3
    By Rayven in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2009, 08:14
  3. Problem with slot
    By stefek in forum Newbie
    Replies: 19
    Last Post: 8th March 2009, 22:48
  4. QPushButton appears differently
    By gianhut in forum Qt Tools
    Replies: 4
    Last Post: 5th January 2009, 23:08
  5. Qpushbutton
    By iamhere in forum Qt Programming
    Replies: 5
    Last Post: 15th October 2008, 05:40

Tags for this Thread

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.