Cannot take the adress of ...
Hello,
First of all sorry for the ugly syntax/style of my code...
Well so I am creating my small GUI, I have a main window (no problem here), and a secondary window (pops up for connecting to database). to connect to the db i have a connection class (a QPhushButton) wich takes the input of 4 QLineEdits. The problem is : I can create my connection class/Line Edits if i set their parent as the Main window but not if I pass my connectionForm class (which inherit from QWidget since it is a window )
I do not know if I expressed myself well so here is the code :
First the connectionForm constructor :
Code:
connectionForm
::connectionForm(QWidget *parent
) :QWidget(parent
), ui
(new Ui
::connectionForm){
ui->setupUi(this);
QLineEdit hostLine
(&this
);
// error here, end of my post for error message QLineEdit userLine
(&this
);
// error here, end of my post for error message QLineEdit passLine
(&this
);
// error here, end of my post for error message QLineEdit dbLine
(&this
);
// error here, end of my post for error message connection_class co1 (&this); // error here, end of my post for error message
//The code continues but has no bug
Here the connectionForm.h
Code:
#include <QWidget>
#include "connection.h"
namespace Ui {
class connectionForm;
}
class connectionForm
: public QWidget{
Q_OBJECT
public:
explicit connectionForm
(QWidget *parent
= 0);
~connectionForm();
private:
void setConnectionGeomConnec
(QWidget *co
);
void setLineEditGeom
(QWidget *leg,
int x,
int y,
int h,
int w
);
Ui::connectionForm *ui;
connection_class co1 ();
};
and finally, the constructor of connection_class .
The error message precisely is :
cannot take the adress of a rvalue of type 'connectionForm'
I understand it means that I used a wrong type for initialisation but why ? Since connectionForm inherits from QWidget...
Re: Cannot take the adress of ...
First of all take the '&' from the 'this' pointer assignments.
The error you have comes probably from your usage of widgets allocated in the scope of the constructor, which die when the constructor ends, and thus they are only temporary.
You probably what to keep you QLineEdits - so you better use member variables, preferably pointers.
Code:
#include <QWidget>
#include "connection.h"
namespace Ui {
class connectionForm;
}
class connectionForm
: public QWidget{
Q_OBJECT
public:
explicit connectionForm
(QWidget *parent
= 0);
~connectionForm();
private:
void setConnectionGeomConnec
(QWidget *co
);
void setLineEditGeom
(QWidget *leg,
int x,
int y,
int h,
int w
);
Ui::connectionForm *ui;
connection_class* pCo1;
};
Code:
connectionForm
::connectionForm(QWidget *parent
) :QWidget(parent
), ui
(new Ui
::connectionForm){
ui->setupUi(this);
m_pHhostLine
= new QLineEdit(this);
// declared as QLineEdit* m_pHostLine in the header file. m_pUserLine
= new QLineEdit(this);
// similar as above pCo1 = new connection_calss(this,m_pHostLine,m_pUserLine,m_pPassLine,m_pDbLine); //
However, since you are using a ui form, why not already place them in the ui with designer?
Then you wont need to initialize them at all.
Re: Cannot take the adress of ...
Sorry, I forgot a lot of thing here...I think I messed up between "clean" and "not clean" code, totally stupid from my part, I cleaned all of that and it just work.
What a stupid mistake...
Anyway thanks
Re: Cannot take the adress of ...
Quote:
.I think I messed up between "clean" and "not clean" code,
If you are referring to the clean code mentioned in my post (lower part) - its part of my signature, it was not part of my answer to you :-)
Re: Cannot take the adress of ...
Well yes I messed up even between your post and your signature :eek:
Anyway my code was such a mess that "clean code" seemed right to me !