Results 1 to 5 of 5

Thread: Cannot take the adress of ...

  1. #1
    Join Date
    Jul 2017
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default 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 :

    Qt Code:
    1. connectionForm::connectionForm(QWidget *parent) :QWidget(parent), ui(new Ui::connectionForm)
    2. {
    3. ui->setupUi(this);
    4. QLineEdit hostLine (&this); // error here, end of my post for error message
    5. QLineEdit userLine (&this); // error here, end of my post for error message
    6. QLineEdit passLine (&this); // error here, end of my post for error message
    7. QLineEdit dbLine (&this); // error here, end of my post for error message
    8. connection_class co1 (&this); // error here, end of my post for error message
    9. //The code continues but has no bug
    To copy to clipboard, switch view to plain text mode 

    Here the connectionForm.h

    Qt Code:
    1. #include <QWidget>
    2. #include "connection.h"
    3. namespace Ui {
    4. class connectionForm;
    5. }
    6.  
    7. class connectionForm : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit connectionForm(QWidget *parent = 0);
    13. ~connectionForm();
    14.  
    15. private:
    16. void setConnectionGeomConnec(QWidget *co);
    17. void setLineEditGeom(QWidget *leg, int x, int y, int h, int w);
    18.  
    19. Ui::connectionForm *ui;
    20. QLineEdit hostLine;
    21. QLineEdit userLine;
    22. QLineEdit passLine;
    23. QLineEdit dbLine;
    24. connection_class co1 ();
    25. };
    To copy to clipboard, switch view to plain text mode 


    and finally, the constructor of connection_class .

    Qt Code:
    1. connection_class (QWidget *parent, QLineEdit *host,QLineEdit *user,QLineEdit *pass,QLineEdit *dbn);
    To copy to clipboard, switch view to plain text mode 
    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...

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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.
    Qt Code:
    1. #include <QWidget>
    2. #include "connection.h"
    3. namespace Ui {
    4. class connectionForm;
    5. }
    6.  
    7. class connectionForm : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit connectionForm(QWidget *parent = 0);
    13. ~connectionForm();
    14.  
    15. private:
    16. void setConnectionGeomConnec(QWidget *co);
    17. void setLineEditGeom(QWidget *leg, int x, int y, int h, int w);
    18.  
    19. Ui::connectionForm *ui;
    20. QLineEdit* m_pHostLine;
    21. QLineEdit* m_pUserLine;
    22. QLineEdit* m_pPassLine;
    23. QLineEdit* m_pDbLine;
    24. connection_class* pCo1;
    25. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. connectionForm::connectionForm(QWidget *parent) :QWidget(parent), ui(new Ui::connectionForm)
    2. {
    3. ui->setupUi(this);
    4. m_pHhostLine = new QLineEdit(this); // declared as QLineEdit* m_pHostLine in the header file.
    5. m_pUserLine = new QLineEdit(this);// similar as above
    6. m_pPassLine = new QLineEdit(this);//
    7. m_pDbLine = new QLineEdit(this); //
    8. pCo1 = new connection_calss(this,m_pHostLine,m_pUserLine,m_pPassLine,m_pDbLine); //
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by high_flyer; 26th July 2017 at 17:59.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2017
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default 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

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Cannot take the adress of ...

    .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 :-)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jul 2017
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Cannot take the adress of ...

    Well yes I messed up even between your post and your signature
    Anyway my code was such a mess that "clean code" seemed right to me !

Similar Threads

  1. Ip adress search
    By Mayssa in forum Qt Programming
    Replies: 2
    Last Post: 16th December 2011, 14:03
  2. Add adress bar and buttons to toolbar
    By halvors in forum Qt Tools
    Replies: 1
    Last Post: 27th April 2010, 17:44
  3. Replies: 1
    Last Post: 12th November 2008, 09:15
  4. get an adress from navigator using Qt
    By nina08 in forum Qt Programming
    Replies: 1
    Last Post: 21st May 2008, 16:13
  5. QHttp : Host vs IP adress
    By Nyphel in forum Newbie
    Replies: 5
    Last Post: 15th April 2007, 12:21

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.