Problem dynamically allocating QWidget child class
This'll be easier if I start with the code:
Code:
//FaultLine.h
{
//...
private:
Dialog();
Dialog
(QWidget* parent,Qt
::WindowFlags f
);
Dialog(const QWidget& parent,Qt::WindowFlags f);
};
{
//...
};
Code:
//Canvas.cpp
#include "FaultLine.h"
//...
void Canvas::HorizonDialog()
{
Dialog* dlg = new Dialog(this);
//...
}
//...
Code:
//Dialog.cpp
//...
#include "FaultLine.h"
Dialog::Dialog()
{
}
Dialog
::Dialog(QWidget* parent
=0, Qt
::WindowFlags f
=0) : QDialog(parent,f
){
this->setLayout(&Layout);
}
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.
Re: Problem dynamically allocating QWidget child class
How does the definition of
Code:
Dialog(const QWidget& parent,Qt::WindowFlags f);
look like?
Re: Problem dynamically allocating QWidget child class
It is very, very basic (I got this error when I was still building up the class).
Code:
{
Q_OBJECT
public:
Dialog();
Dialog
(QWidget* parent,Qt
::WindowFlags f
);
public slots:
void ok_pressed();
signals:
};
Dialog
::Dialog(QWidget* parent
=0, Qt
::WindowFlags f
=0) : QDialog(parent,f
){
this->setLayout(&Layout);
}
Re: Problem dynamically allocating QWidget child class
I can't reproduce your problem here. Please make a compilable example reproducing the problem.
Re: Problem dynamically allocating QWidget child class
Put the default arguments in the definition in your header file:
Code:
//FaultLine.h
{
//...
Dialog
(QWidget* parent
=0 ,Qt
::WindowFlags f
=0);
and take them out of your cpp file:
Code:
//Dialog.cpp
//...
Dialog
::Dialog(QWidget* parent, Qt
::WindowFlags f
) : QDialog(parent,f
){
this->setLayout(&Layout);
}
Re: Problem dynamically allocating QWidget child class
Quote:
Originally Posted by
norobro
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.
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.
Code:
Dialog
(QWidget* parent,Qt
::WindowFlags f
=0)
Re: Problem dynamically allocating QWidget child class
Quote:
Originally Posted by
Lykurg
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.:confused: