Results 1 to 8 of 8

Thread: Problem dynamically allocating QWidget child class

  1. #1
    Join Date
    Aug 2010
    Posts
    65
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Problem dynamically allocating QWidget child class

    This'll be easier if I start with the code:
    Qt Code:
    1. //FaultLine.h
    2.  
    3. class Dialog : public QDialog
    4. {
    5. //...
    6. private:
    7. Dialog();
    8. Dialog(QWidget* parent,Qt::WindowFlags f);
    9. Dialog(const QWidget& parent,Qt::WindowFlags f);
    10. };
    11.  
    12. class Canvas : public QWidget
    13. {
    14. //...
    15. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //Canvas.cpp
    2. #include "FaultLine.h"
    3.  
    4. //...
    5. void Canvas::HorizonDialog()
    6. {
    7. Dialog* dlg = new Dialog(this);
    8. //...
    9. }
    10. //...
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //Dialog.cpp
    2. //...
    3. #include "FaultLine.h"
    4.  
    5. Dialog::Dialog()
    6. {
    7. }
    8. Dialog::Dialog(QWidget* parent=0, Qt::WindowFlags f=0) : QDialog(parent,f)
    9. {
    10. this->setLayout(&Layout);
    11. }
    To copy to clipboard, switch view to plain text mode 
    The following code does not compile correctly. In ::HorizonDialog(), I get a "cannot convert parameter 1 from 'Canvas *const ' to 'const Dialog &'" when trying to allocate a new Dialog. I've tried using a copy-constructor (which obviously doesn't work), overloading the operator=, changing the argument to "*this", and a few other things. I was certain this was a C++ error (I still wouldn't be surprised if it is), but the people over at the cplusplus.com forums have so far been unable to find the error. So, in the off-chance that this is somehow related to qmake, I'm also asking here.

    Help, please?

    As well, substituting the current construction for the default ("new Dialog()") compiles just fine.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Problem dynamically allocating QWidget child class

    How does the definition of
    Qt Code:
    1. Dialog(const QWidget& parent,Qt::WindowFlags f);
    To copy to clipboard, switch view to plain text mode 
    look like?

  3. #3
    Join Date
    Aug 2010
    Posts
    65
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Problem dynamically allocating QWidget child class

    It is very, very basic (I got this error when I was still building up the class).

    Qt Code:
    1. class Dialog : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. QString Value;
    6. QHBoxLayout Layout;
    7. public:
    8. Dialog();
    9. Dialog(QWidget* parent,Qt::WindowFlags f);
    10. void AddToLayout(QWidget* widget);
    11. public slots:
    12. void ok_pressed();
    13. signals:
    14. void value(QString value);
    15. };
    16.  
    17. Dialog::Dialog(QWidget* parent=0, Qt::WindowFlags f=0) : QDialog(parent,f)
    18. {
    19. this->setLayout(&Layout);
    20. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Problem dynamically allocating QWidget child class

    I can't reproduce your problem here. Please make a compilable example reproducing the problem.

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 142 Times in 135 Posts

    Default Re: Problem dynamically allocating QWidget child class

    Put the default arguments in the definition in your header file:
    Qt Code:
    1. //FaultLine.h
    2.  
    3. class Dialog : public QDialog
    4. {
    5. //...
    6. Dialog(QWidget* parent=0 ,Qt::WindowFlags f=0);
    To copy to clipboard, switch view to plain text mode 

    and take them out of your cpp file:
    Qt Code:
    1. //Dialog.cpp
    2. //...
    3. Dialog::Dialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent,f)
    4. {
    5. this->setLayout(&Layout);
    6. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Problem dynamically allocating QWidget child class

    Quote Originally Posted by norobro View Post
    Put the default arguments in the definition in your header file:
    You can define your default parameters in the declaration or definition. It does not matter. You only cannot give default parameters in both, definition and declaration.

  7. The following user says thank you to Lykurg for this useful post:

    norobro (22nd September 2010)

  8. #7
    Join Date
    Aug 2010
    Posts
    65
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Problem dynamically allocating QWidget child class

    Norobro might be wrong, but he made me think of the solution. I don't know why the error was stated as it was, but the problem was that I had both arguments with default values. I took out the default value of the parent and it worked! Perhaps this made the compiler mix up the default constructor (no arguments) with a constructor with all (possibly) default values? Or something of the sort, I don't know.

    Qt Code:
    1. Dialog(QWidget* parent,Qt::WindowFlags f=0)
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Feb 2008
    Posts
    491
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 142 Times in 135 Posts

    Default Re: Problem dynamically allocating QWidget child class

    Quote Originally Posted by Lykurg View Post
    You can define your default parameters in the declaration or definition. It does not matter. You only cannot give default parameters in both, definition and declaration.
    Obviously, I wasn't aware of this. I have always put default parameters in the declaration.

    @wasabi - glad you solved your problem. After Lykurg posted I created a small project with your Dialog class and it would not compile with the default values in the definition file but compiled fine with both default values in the declaration. And as you experienced, it complies with only the second param having a default value in the definition.

Similar Threads

  1. Replies: 1
    Last Post: 16th September 2010, 16:57
  2. Replies: 4
    Last Post: 17th April 2010, 03:42
  3. Add a Qwidget dynamically
    By assismvla in forum Newbie
    Replies: 4
    Last Post: 3rd September 2009, 07:14
  4. Replies: 10
    Last Post: 17th August 2006, 16:12
  5. Replies: 2
    Last Post: 22nd February 2006, 15:58

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.