PDA

View Full Version : Problem dynamically allocating QWidget child class



Wasabi
21st September 2010, 20:20
This'll be easier if I start with the code:

//FaultLine.h

class Dialog : public QDialog
{
//...
private:
Dialog();
Dialog(QWidget* parent,Qt::WindowFlags f);
Dialog(const QWidget& parent,Qt::WindowFlags f);
};

class Canvas : public QWidget
{
//...
};

//Canvas.cpp
#include "FaultLine.h"

//...
void Canvas::HorizonDialog()
{
Dialog* dlg = new Dialog(this);
//...
}
//...

//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.

Lykurg
21st September 2010, 20:27
How does the definition of
Dialog(const QWidget& parent,Qt::WindowFlags f);look like?

Wasabi
21st September 2010, 21:04
It is very, very basic (I got this error when I was still building up the class).



class Dialog : public QDialog
{
Q_OBJECT

QString Value;
QHBoxLayout Layout;
public:
Dialog();
Dialog(QWidget* parent,Qt::WindowFlags f);
void AddToLayout(QWidget* widget);
public slots:
void ok_pressed();
signals:
void value(QString value);
};

Dialog::Dialog(QWidget* parent=0, Qt::WindowFlags f=0) : QDialog(parent,f)
{
this->setLayout(&Layout);
}

Lykurg
21st September 2010, 22:00
I can't reproduce your problem here. Please make a compilable example reproducing the problem.

norobro
21st September 2010, 22:31
Put the default arguments in the definition in your header file:
//FaultLine.h

class Dialog : public QDialog
{
//...
Dialog(QWidget* parent=0 ,Qt::WindowFlags f=0);

and take them out of your cpp file:

//Dialog.cpp
//...
Dialog::Dialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent,f)
{
this->setLayout(&Layout);
}

Lykurg
21st September 2010, 22:38
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.

Wasabi
22nd September 2010, 00:02
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.


Dialog(QWidget* parent,Qt::WindowFlags f=0)

norobro
22nd September 2010, 00:32
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: