PDA

View Full Version : Cannot take the adress of ...



Alex2822
26th July 2017, 14:31
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 :


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



#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;
QLineEdit hostLine;
QLineEdit userLine;
QLineEdit passLine;
QLineEdit dbLine;
connection_class co1 ();
};



and finally, the constructor of connection_class .



connection_class (QWidget *parent, QLineEdit *host,QLineEdit *user,QLineEdit *pass,QLineEdit *dbn);

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

high_flyer
26th July 2017, 16:26
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.


#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;
QLineEdit* m_pHostLine;
QLineEdit* m_pUserLine;
QLineEdit* m_pPassLine;
QLineEdit* m_pDbLine;
connection_class* pCo1;
};



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
m_pPassLine = new QLineEdit(this);//
m_pDbLine = new QLineEdit(this); //
pCo1 = new connection_calss(this,m_pHostLine,m_pUserLine,m_pP assLine,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.

Alex2822
26th July 2017, 21:21
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

high_flyer
27th July 2017, 09:32
.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 :-)

Alex2822
28th July 2017, 14:37
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 !