Results 1 to 11 of 11

Thread: Form , set object externally

  1. #1
    Join Date
    Dec 2009
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Form , set object externally

    Hi

    A very simple question , but I don't understand how to do it.

    I'm using QTCREATOR. I created a form with designer.

    Now I need to do some operation to this form by a new external *.ccp file.

    I put Ui::Main *ui; public

    Main::Ui->object->setText("mmmm" is visible but I get an error " Invalid use of non-static member"

    How to do it??

    In the forum nothig helped me

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Form , set object externally

    You need ui (the variable — a specific instance of your Ui class) rather than Main::Ui as the first pointer. To get that, you must have access to the class in which you declare ui.

    If you are not in some way communicating the address of the object, or of something that contains it, to your new code, then you cannot access it.

  3. #3
    Join Date
    Dec 2009
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Form , set object externally

    Mhhmmmh
    I have
    Qt Code:
    1. #ifndef PRINCIPALE_H
    2. #define PRINCIPALE_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtGui/QWidget>
    6. #include <QtNetwork/QAbstractSocket>
    7.  
    8. class QSslSocket;
    9.  
    10. namespace Ui {
    11. class Principale;
    12. }
    13.  
    14. class Principale : public QMainWindow {
    15. Q_OBJECT
    16.  
    17. public:
    18. Principale(QWidget *parent = 0);
    19. ~Principale();
    20. //........ omitted code
    21. Ui::Principale *ui;
    22.  
    23. protected:
    24. private:
    25. QSslSocket *socket;
    26. private slots:
    27. void on_lineEdit_returnPressed();
    28. //... omitted code
    29. };
    30.  
    31. #endif // PRINCIPALE_H
    To copy to clipboard, switch view to plain text mode 


    In the first post I wrote Main .... but I tried Principale::ui::abc->setText("xyz") ....
    it's recognized ( autocompletition works ) but I get the error "Invalid use of non-static member"

    in new *.cpp I wrote

    Qt Code:
    1. #include <ui_principale.h>
    2. #include <principale.h>
    To copy to clipboard, switch view to plain text mode 

    Now to access for example to a label named "abc" ???? i feel a little bit stupid

    Thank you in advance

  4. #4
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Form , set object externally

    Somewhere, probably in main, you instantiate and show Principale; that is, you have code like:
    Qt Code:
    1. // one way:
    2. //
    3. Principale primaryWindow;
    4. primaryWindow.show();
    5. //
    6. // or, another way:
    7. //
    8. Principale* primaryWindowPointer = new Principale();
    9. primaryWindowPointer->show();
    To copy to clipboard, switch view to plain text mode 

    In your new code, you need:
    Qt Code:
    1. // one way:
    2. //
    3. primaryWindow.ui->abc->setText("xyz");
    4. //
    5. // or, another way:
    6. //
    7. primaryWindowpointer->ui->abc->setText("xyz");
    To copy to clipboard, switch view to plain text mode 

    How you get “primaryWindow” or “primaryWindowPointer” to your new code depends on how you call the new code. If it’s called from main (or whatever routine creates and shows an instance of Principale), pass the same variable as an argument to your code. If it’s called from a member function of Principale, pass the this pointer.

    You could also pass primaryWindow.ui->abc directly to your new code, if it only needs that one object.

    There are any number of ways accomplish this, but somehow your new code must get a pointer to the relevant instance of abc to able to apply setText.

  5. #5
    Join Date
    Dec 2009
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Form , set object externally

    This is my main (created by QT creator)

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "principale.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Principale w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    w AND a are not recognized in my new code ....

    If I try to put in my new code

    Qt Code:
    1. Principale primaryWindow;
    2. primaryWindow.ui->setText("xyz");
    To copy to clipboard, switch view to plain text mode 

    The error does not shows up anymore but Label doesn't change

  6. #6
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Form , set object externally

    Quote Originally Posted by giacomelli.fabio View Post
    If I try to put in my new code

    Qt Code:
    1. Principale primaryWindow;
    2. primaryWindow.ui->setText("xyz");
    To copy to clipboard, switch view to plain text mode 

    The error does not shows up anymore but Label doesn't change
    Do you understand why the label doesn’t change? Consider it for a little while, then tell me what you think is the reason. (Hint: your confusion has nothing to do with Qt; it’s something rather important about C++ that you’re missing.)

  7. #7
    Join Date
    Dec 2009
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Form , set object externally

    Ah, ok that was because of I wrote it by hand in the forum ... i always use auto-completion so it's very difficult i do so big errors . I'm tired now , I will not continue.

    I think I need to study a little bit... I used gambas ( a VB clone for Linux) learning it directly .... with C++ and QT it's impossible ...

    Thank you ( I had put all my code into primary *cpp )

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Form , set object externally

    Here is a hint: your lineEdit widget is a member variable, probably one of many, in the instance pointed to by ui (built and set up by Designer code). See what you can do with:
    Qt Code:
    1. ui->lineEdit->
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Dec 2009
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Form , set object externally

    Similar but different from what I needed before ... I think it's a c++ question .... but if you have 5 minutes please help me

    I did an additional form ... I declared it :

    Qt Code:
    1. #include <dialog.h>
    2. #include <ui_dialog.h>
    3. Dialog2 myDialog;
    4. myDialog.show();
    To copy to clipboard, switch view to plain text mode 

    and it's ok. I can use its slots and its form objects

    NOW

    I need also to use FROM myDialog some objects from my first dialog

    If I try to #include <firstdialog.h> into dialog2.h ( so I could declare FirstDialog myDialog2 ) i get some errors

    it's very difficult find in google or into forum an answer ...

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Form , set object externally

    You probably don't have a guard in your include file to prevent multiple inclusion. Eg.

    Qt Code:
    1. #ifndef FIRSTDIALOG_H
    2. #define FIRSTDIALOG_H
    3.  
    4. blah blah
    5. #endif
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Dec 2009
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Form , set object externally

    No it's OK ... it was included by qtcreator ...

    but I think it's a similar problem

Similar Threads

  1. Replies: 4
    Last Post: 19th February 2009, 11:10
  2. Multiple form object creation
    By kpmsivachand in forum Qt Programming
    Replies: 2
    Last Post: 3rd February 2009, 01:09
  3. A form returning an object.
    By cbarmpar in forum Qt Programming
    Replies: 3
    Last Post: 8th September 2008, 05:21
  4. Passing an object from one form to the other.
    By cbarmpar in forum Qt Programming
    Replies: 10
    Last Post: 3rd September 2008, 14:12
  5. visible main form?
    By triperzonak in forum Newbie
    Replies: 1
    Last Post: 12th June 2008, 09:00

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
  •  
Qt is a trademark of The Qt Company.