Results 1 to 9 of 9

Thread: Public variables

  1. #1
    Join Date
    Feb 2012
    Posts
    33
    Qt products
    Qt4

    Default Public variables

    Hi,

    I am a QT project with 3 forms (mainwindow, form2, form3).

    I am also a lot of variables in use in the mainwindow, and I need to read and write these variables into form2 and form3.

    I have to define my variables public in the main() (I think so), but I do not know how to do it.

    Thanks.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Public variables

    You can make the variables part of MainWindow.. then provide some setter functionsi n form2 and form3.
    This way you can set them in the forms.

    Other way is to use signals and slots

  3. #3
    Join Date
    Feb 2012
    Posts
    33
    Qt products
    Qt4

    Default Re: Public variables

    I provide some setter functions in form2 and form3? what it means?
    Can you give an example?

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Public variables

    You will have some class for your forms, right ?
    Lets say
    class MyWidget : public QWidget
    {
    int m_x;
    public:
    void setVariableV1(int x);
    }

    Then you can use object of the widget from your mainwindow-
    objMyWidget->setVariableV1(newVariableValue);

    Hope you get the hint

  5. #5
    Join Date
    Feb 2012
    Posts
    33
    Qt products
    Qt4

    Default Re: Public variables

    This in my mainwindows.h


    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    int Test;



    Now, in my Form2 I want to write the variable "Test".

    MainWindow::Test=3;



    This is the compiler answer:

    .....illegal reference to non-static member 'MainWindow::Test

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Public variables

    I guess you need to work a little on C++.
    Give some time to learning and you will be able to solve yourself.

  7. #7
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Public variables

    You must be having a main.cpp right?

    How are you accessing your main window class? You must have created an object of the main window class some where int he main.cpp and then called mainwindow.show().
    Similarly you can access the variables of that class by creating object of the class.

  8. #8
    Join Date
    Mar 2012
    Location
    Tver, Russia
    Posts
    9
    Thanks
    1
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Public variables

    I think you may use signal/slot mechanism to set these variables in form2 and form3 from mainwindow.
    If your variables are of the same type, you may want to use QHash< VariableName, VariableValue > to store variable data -- in this case you need one signal from mainwindow and only oneslot in form2 and form3.

  9. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Public variables

    looks like eltec really needs to learn some basics of OOP and c++.


    Variables etc that are members of a class need an *instance* to be available. To set/get members, or use member functions, an *instance* must be available.

    Qt Code:
    1. class MyClass
    2. {
    3. public:
    4. int x;
    5. };
    6.  
    7. int main()
    8. {
    9. MyClass myclass; // make an instance!
    10. myclass.x = 5; // set the variable
    11. int x = myclass.x; // get the variable
    12.  
    13.  
    14. MyClass::x; // wrong!
    15. myclass->x; // wrong! myclass is not a pointer.
    16.  
    17. MyClass.x // wrong! MyClass is not an instance
    18. }
    To copy to clipboard, switch view to plain text mode 

    OP, please learn a little (well, ok more than a little) about c++ - this will also involve learning some about oop.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Qt Public Repository Launched!
    By lpotter in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2009, 00:38
  2. Why Qt can not find a public slot?
    By Alex Snet in forum Qt Programming
    Replies: 13
    Last Post: 13th April 2009, 08:31
  3. Replies: 2
    Last Post: 14th October 2008, 09:05
  4. private and public slots ??
    By salmanmanekia in forum Qt Programming
    Replies: 2
    Last Post: 13th August 2008, 11:49
  5. GUI Look and Feel -- public repository available?
    By Byngl in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2006, 06:37

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.