Results 1 to 9 of 9

Thread: Mess with inherited constructors

  1. #1
    Join Date
    Mar 2007
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Mess with inherited constructors

    I created a subclass of QAction, among others with this constructor:
    Qt Code:
    1. MyAction(QObject * parent, const QString & name);
    To copy to clipboard, switch view to plain text mode 

    Then I use it in other parts this way:

    Qt Code:
    1. MyAction *action = new MyAction(this, "name");
    To copy to clipboard, switch view to plain text mode 

    The problem is that instead of my constructor, it's called QAction ( QObject * parent, const char * name ).

    I fixed it changing my constructor to
    Qt Code:
    1. MyAction ( QObject * parent, const char * name);
    To copy to clipboard, switch view to plain text mode 

    But I also had the same problem with other constructors.

    Is it possible any way that only my constructors are taking into account and not the inherited ones?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Mess with inherited constructors

    Could we see the implementation of the problematic constructor?
    J-P Nurmi

  3. #3
    Join Date
    Mar 2007
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mess with inherited constructors

    Qt Code:
    1. MyAction::MyAction(QObject * parent, const QString & name) : QAction(parent)
    2. {
    3. setObjectName(name);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Anyway this code is not called.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Mess with inherited constructors

    It just... won't magically jump directly to QAction constructor if you create a MyAction.
    Qt Code:
    1. #include <QtGui>
    2. #include <QtDebug>
    3.  
    4. class MyAction : public QAction
    5. {
    6. public:
    7. MyAction(QObject* parent, const QString& name);
    8. };
    9.  
    10. MyAction::MyAction(QObject* parent, const QString& name)
    11. : QAction(parent)
    12. {
    13. qDebug() << "MyAction::MyAction()";
    14. setText(name);
    15. setObjectName(name);
    16. }
    17.  
    18. class MainWindow : public QMainWindow
    19. {
    20. public:
    21. MainWindow(QWidget* parent = 0);
    22. };
    23.  
    24. MainWindow::MainWindow(QWidget* parent)
    25. : QMainWindow(parent)
    26. {
    27. qDebug() << "MainWindow::MainWindow()";
    28. MyAction* action = new MyAction(this, "Action");
    29. menuBar()->addAction(action);
    30. }
    31.  
    32. int main(int argc, char* argv[])
    33. {
    34. QApplication a(argc, argv);
    35. MainWindow w;
    36. w.show();
    37. return a.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Mar 2007
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mess with inherited constructors

    I think I have found the problem. It wasn't calling QAction ( QObject * parent, const char * name ) but another constructor that I had: MyAction(QObject *, bool).

    But why a "myaction *action = new MyAction(this, "name")" could call that constructor with a bool argument? I don't get it.

    And the problem is that I really need that constructor.

    Your code modified:

    Qt Code:
    1. #include <QtGui>
    2. #include <QtDebug>
    3.  
    4.  
    5. class MyAction : public QAction
    6. {
    7. public:
    8. MyAction(QObject* parent, const QString& name);
    9. MyAction(QObject *parent, bool a);
    10. };
    11.  
    12. MyAction::MyAction(QObject* parent, const QString& name)
    13. : QAction(parent)
    14. {
    15. qDebug() << "MyAction::MyAction()";
    16. setText(name);
    17. setObjectName(name);
    18. }
    19.  
    20. MyAction::MyAction(QObject *parent, bool a) : QAction(parent)
    21. {
    22. qDebug() << "MyAction::MyAction(QObject, bool)";
    23. }
    24.  
    25. class MainWindow : public QMainWindow
    26. {
    27. public:
    28. MainWindow(QWidget* parent = 0);
    29. };
    30.  
    31. MainWindow::MainWindow(QWidget* parent)
    32. : QMainWindow(parent)
    33. {
    34. qDebug() << "MainWindow::MainWindow()";
    35. MyAction * action = new MyAction(this, "Action");
    36. menuBar()->addAction(action);
    37. }
    38.  
    39. int main(int argc, char* argv[])
    40. {
    41. QApplication a(argc, argv);
    42. MainWindow w;
    43. w.show();
    44. return a.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 

    After running it, console output:
    MainWindow::MainWindow()
    MyAction::MyAction(QObject, bool)

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Mess with inherited constructors

    The most sensible choice for a human is not always the most sensible choice for a machine.

    A literal "Action" is a const char*. A pointer can be directly used as a boolean value whereas for a QString to match, the compiler would have to implicitly convert const char* to a QString. This is why MyAction(QObject*, bool) gets chosen over MyAction(QObject*, const QString&). Now, if you add MyAction(QObject*, const char*) as mentioned in the first post, it gets naturally chosen as a first choice because it's the closest possible match.
    J-P Nurmi

  7. #7
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mess with inherited constructors

    Makes me wonder what would happen if you called MyAction(this, QString("name"));

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mess with inherited constructors

    Quote Originally Posted by jpn View Post
    It just... won't magically jump directly to QAction constructor if you create a MyAction.
    Actually in such situation it will call the default QAction constructor, won't it?

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Mess with inherited constructors

    Quote Originally Posted by wysota View Post
    Actually in such situation it will call the default QAction constructor, won't it?
    The question was about
    Qt Code:
    1. new MyAction(this, "name");
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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.