Results 1 to 5 of 5

Thread: How to access to a var in the main window (QWidget) from an other window (QWidget)???

  1. #1
    Join Date
    Mar 2018
    Posts
    33
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default How to access to a var in the main window (QWidget) from an other window (QWidget)???

    In my project i have this 4 classes in separated .h and .cpp file:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent): QWidget (parent){
    2. built_sphere_window = new BuiltSphere;
    3. built_cylinder_window = new BuiltCylinder;
    4. glWidgetWindow = new GlWidget;
    5. ....}
    6.  
    7. BuiltCylinder::BuiltCylinder(QWidget *parent) : QWidget(parent){
    8. glWidgetCylinder = new GlWidget;
    9. ....}
    10.  
    11. BuiltSphere::BuiltSphere(QWidget *parent) : QWidget(parent)
    12. {
    13. glWidgetSphere = new GlWidget;
    14. ....}
    15.  
    16. GlWidget::GlWidget(QWidget *parent) : QGLWidget(QGLFormat(/* Additional format options */), parent){
    17. alpha = 0; //previous =>25
    18. beta = 0; //previous =>-25
    19. distance = 6; //previous =>2.5
    20. initializeWindow();
    21. ....}
    To copy to clipboard, switch view to plain text mode 
    from the "mainwindow" i do this calls:
    Qt Code:
    1. void MainWindow::built_sphere(){
    2. built_sphere_window->setWindowTitle("Built Sphere");
    3. built_sphere_window->setWindowModality(Qt::ApplicationModal);
    4. built_sphere_window->num_sphere = num_element_inserted->at(1)+1;
    5. built_sphere_window->show();
    6. }
    7. void MainWindow::built_cylinder(){
    8. built_cylinder_window->setWindowTitle("Built Cylinder");
    9. built_cylinder_window->setWindowModality(Qt::ApplicationModal);
    10. built_cylinder_window->num_cyl = num_element_inserted->at(0)+1;
    11. built_cylinder_window->show();
    12. }
    To copy to clipboard, switch view to plain text mode 
    I have several object to build but at this time just the previous 2.
    when i open one of this window i to read the data from the respective var & i'm right to visualize the number of the new object that i want to build.
    My question is:
    how i can set correctly the signal to send to mainwindow the number that i want only at push to ok button in the build window?
    in mainwindow.cpp i think i have to set (i write the call for one build element):
    Qt Code:
    1. connect (built_sphere_window, SIGNAL (sig_update_num(const int)), this, SLOT (update_value(const float)));
    To copy to clipboard, switch view to plain text mode 
    and in the respective slot update the num_element_inserted.
    in builtsphere.h:
    Qt Code:
    1. signals:
    2. void sig_update_num(const int);
    To copy to clipboard, switch view to plain text mode 
    in builtsphere.cpp:
    Qt Code:
    1. emit sig_update_num(num_sphere+1)
    To copy to clipboard, switch view to plain text mode 
    I think that the code write like this work, my finally question is:
    if i want to read/write a value that is stored in a variable in the main window or in all of other classes from one of it is better do a "QSharedMemory+signal in a class like shareddata.h" to do the job or with the code that i write above?
    At this time i do my application in one thread (i want to test it first that run correctly & do the job that i want), next step is do multithread to optimize the performance in multicore processor; so i think that in multithread is better to build a class shareddata.h where i can use the lock() & unlock() functions, is the right way to do the job?

  2. #2
    Join Date
    Mar 2018
    Posts
    33
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access to a var in the main window (QWidget) from an other window (QWidget

    [QUOTE=andreaQt;308744]In my project i have this 4 classes in separated .h and .cpp file:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent): QWidget (parent){
    2. built_sphere_window = new BuiltSphere;
    3. built_cylinder_window = new BuiltCylinder;
    4. glWidgetWindow = new GlWidget;
    5. ....}
    6.  
    7. BuiltCylinder::BuiltCylinder(QWidget *parent) : QWidget(parent){
    8. glWidgetCylinder = new GlWidget;
    9. ....}
    10.  
    11. BuiltSphere::BuiltSphere(QWidget *parent) : QWidget(parent)
    12. {
    13. glWidgetSphere = new GlWidget;
    14. ....}
    15.  
    16. GlWidget::GlWidget(QWidget *parent) : QGLWidget(QGLFormat(/* Additional format options */), parent){
    17. alpha = 0; //previous =>25
    18. beta = 0; //previous =>-25
    19. distance = 6; //previous =>2.5
    20. initializeWindow();
    21. ....}
    To copy to clipboard, switch view to plain text mode 
    from the "mainwindow" i do this calls:
    Qt Code:
    1. void MainWindow::built_sphere(){
    2. built_sphere_window->setWindowTitle("Built Sphere");
    3. built_sphere_window->setWindowModality(Qt::ApplicationModal);
    4. built_sphere_window->num_sphere = num_element_inserted->at(1)+1;
    5. built_sphere_window->show();
    6. }
    7. void MainWindow::built_cylinder(){
    8. built_cylinder_window->setWindowTitle("Built Cylinder");
    9. built_cylinder_window->setWindowModality(Qt::ApplicationModal);
    10. built_cylinder_window->num_cyl = num_element_inserted->at(0)+1;
    11. built_cylinder_window->show();
    12. }
    To copy to clipboard, switch view to plain text mode 
    I have several object to build but at this time just the previous 2.
    when i open one of this window i to read the data from the respective var & i'm right to visualize the number of the new object that i want to build.
    My question is:
    how i can set correctly the signal to send to mainwindow the number that i want only at push to ok button in the build window?
    in mainwindow.cpp i think i have to set (i write the call for one build element):
    Qt Code:
    1. connect (built_sphere_window, SIGNAL (sig_update_num(const int)), this, SLOT (update_value(const float)));
    To copy to clipboard, switch view to plain text mode 
    and in the respective slot update the num_element_inserted.
    in builtsphere.h:
    Qt Code:
    1. signals:
    2. void sig_update_num(const int);
    To copy to clipboard, switch view to plain text mode 
    in builtsphere.cpp:
    Qt Code:
    1. emit sig_update_num(num_sphere+1)
    To copy to clipboard, switch view to plain text mode 
    I think that the code write like this work, my finally question is:
    if i want to read/write a value that is stored in a variable in the main window or in all of other classes from one of it is better do a "QSharedMemory+signal in a class like shareddata.h" to do the job or with the code that i write above?
    At this time i do my application in one thread (i want to test it first that run correctly & do the job that i want), next step is do multithread to optimize the performance in multicore processor; so i think that in multithread is better to build a class shareddata.h where i can use the lock() & unlock() functions, is the right way to do the job?

    After some test i have integrated this code & its work:
    Qt Code:
    1. void MainWindow::built_sphere(){
    2. built_sphere_window->setWindowTitle("Built Sphere");
    3. built_sphere_window->setWindowModality(Qt::ApplicationModal);
    4. built_sphere_window->num_sphere = num_element_inserted.at(0)+1;
    5. built_sphere_window->num_sphere_label->setText(tr("Sphere %1").arg(QString::number(built_Sphere_window->num_cyl)));
    6. built_sphere_window->show();
    7. }
    To copy to clipboard, switch view to plain text mode 

    I still waiting for an answer about "QSharedMemory+signal in a class like shareddata.h"

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access to a var in the main window (QWidget) from an other window (QWidget

    It is almost impossible to understand from your description what it is that you actually want to do. Forget the "build_this", "build_that" code and all of the other irrelevant stuff. What is the design problem you are trying to solve?

    1 - What is the purpose of the variable in the main window class? What kind of value does it hold?
    2 - Why is it necessary for other class instances to know what its value is?
    3 - Do these other class instances need to read the value, or do they need read -and- write access?
    4 - Do all of the class instances need to know when the value changes?

    Until you can answer these questions and understand how to implement a general solution, you should stay away from any thoughts of multithreading, shared data, smart pointers or anything even close to that. You need to have a basic understanding first because if you don't have that, you will fail when you try to implement something more complex.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Mar 2018
    Posts
    33
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access to a var in the main window (QWidget) from an other window (QWidget

    Note: when i write like read/write i want to say: do one of the action or both the actions

    This is the flow o my action:
    1 - From mainwindow i select to build some object by select it in a toolbar => is open the respective built_OBJ_window=> for example built_sphere_window, so when it is opened i have to
    understood how many of this object is builted previously; for do this i have in mainwindow a "QVector<int> num_element_inserted;" to take trace about it.
    So when i open a new built_sphere_window i want to know with obj i'm going to work & view it in the dedicate label ->num_sphere_label, only at pressing at OK button i update
    "QVector<int> num_element_inserted" by calling "num_element_inserted.operator[](0) = num_cyl_inserted+1;" in a dedicated slot after i build the obj; if i press on a CANCEL
    button no update is done, no obj is builded. This is my 1st goal.
    2 - Next i have buided a obj & stored the data in (on glWidgetWindow):
    Qt Code:
    1. struct OBJ_draw {
    2. QString name;
    3. int color;
    4. int type_elements;
    5. QVector<QVector3D> draws_elements;
    6. };
    7. QList<OBJ_draw> list_draws_elements;
    To copy to clipboard, switch view to plain text mode 
    next step i have to build in the toolbar (at first it but next a procedure to save/load data & so on) in mainwindow a edit call that open a window where i edit one or more obj (like his
    color or its position, ecc.) so i have to access on "QVector<QVector3D> draws_elements" to do the job (i'm building an opportune class) - so i have to share some data that is stored
    in glWidgetWindow with some other classes.

    My 2nd goal is to know in general cases if is it better to build "QSharedMemory+signal in a class like shareddata.h" when i have to share lot of data => so in all the class that i will
    built i have just to include shareddata.h to access to the data that i want, and in general in a multithread application when i have a lot of data shared by more of one thread is better to
    share the data in a shared memory & use the mechanism on lock/unlock to do a right job

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access to a var in the main window (QWidget) from an other window (QWidget

    "QVector<int> num_element_inserted" by calling "num_element_inserted.operator[](0) = num_cyl_inserted+1;"
    Why are you using a QVector when you are only updating element 0? I think you are confusing counting the number of elements with the index (label) of the element.

    It does not sound like you are actually sharing any data at all. What you seem to be doing is trying to tell another dialog or widget that constructs the object what the next index is so it can set the proper label on the object. For that, all you need to do it to pass the number before you show() the window. And then you need to add signals to the widget that get emitted when the OK or Cancel buttons are clicked, and add slots for those signals to your MainWindow.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent): QWidget (parent){
    2. built_sphere_window = new BuiltSphere;
    3. built_cylinder_window = new BuiltCylinder;
    4. glWidgetWindow = new GlWidget;
    5.  
    6. connect( build_sphere_window, &BuiltSphere::okClicked, this, &MainWindow::buildSphereOKClicked );
    7. connect( build_sphere_window, &BuiltSphere::cancelClicked, this, &MainWindow::buildSphereCancelClicked );
    8.  
    9. // same for BuiltCylinder, etc.
    10. ....}
    11.  
    12. void MainWindow::built_sphere(){
    13. built_sphere_window->setWindowTitle("Built Sphere");
    14. built_sphere_window->setWindowModality(Qt::ApplicationModal);
    15. // built_sphere_window->num_sphere = num_element_inserted->at(1)+1; // NO. --Never-- expose member variables. Use method calls.
    16. build_sphere_window->setSphereIndex( num_element_inserted->at(1) + 1 );
    17. built_sphere_window->show();
    18. }
    19.  
    20. void MainWindow::buildSphereOKClicked()
    21. {
    22. build_sphere_window->hide();
    23. num_element_inserted->at(1) += 1;
    24. }
    25.  
    26. void MainWindow::buildSphereCancelClicked()
    27. {
    28. build_sphere_window->hide();
    29. }
    To copy to clipboard, switch view to plain text mode 

    My 2nd goal is to know in general cases if is it better to build "QSharedMemory+signal in a class like shareddata.h" when i have to share lot of data
    As I said, you aren't sharing data. There is a difference between telling another class instance what the value of some piece of data is when that other class instance needs to know it, and actually sharing access to the same data location. You are not sharing the variable itself, you are sharing its value. These are different things.

    You are making a common mistake in assuming that the only way to share the value of variables among different classes is to share the variables themselves. That's wrong.

    Look at the way Qt classes themselves are implemented. There are "getter / setter" methods for almost every property of a widget that is exposed to the application. If you want to set the title of a window, you do not assign the variable that holds the title string, you call a method with a string argument, and that method (inside QWidget) sets the title. That is, you are doing this:

    Qt Code:
    1. // getter
    2. QString currentTitle = myWidget->windowTitle();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // setter
    2. myWidget->setWindowTitle( "This is My Widget" );
    To copy to clipboard, switch view to plain text mode 

    and not:

    Qt Code:
    1. // wrong
    2. myWidget->windowTitle = "This is My Widget";
    To copy to clipboard, switch view to plain text mode 

    You should be doing the same in your application. I have been programming in C++ for 40 years, and have been using Qt for 12 years, and I have never had to use shared memory of any sort in an ordinary application. Even in multithreaded applications, there is never shared memory. There is shared access to the values of variables, always through getter/setter methods, and always protected by mutexes or other multithread locking methods.

    If you think you have to use shared memory, then you have a bad design which is only going to get you into trouble. Your application should be designed in a way that only one class has control (ownership) of any piece of data, and other classes have methods they call to ask for the current value of the data or ask the owner of the data to change its value.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    andreaQt (30th September 2020)

Similar Threads

  1. Replies: 2
    Last Post: 17th February 2011, 13:30
  2. Replies: 2
    Last Post: 4th August 2010, 20:10
  3. Replies: 4
    Last Post: 10th February 2010, 07:56
  4. access main window from function
    By eric in forum Qt Programming
    Replies: 6
    Last Post: 19th January 2008, 22:29
  5. Replies: 5
    Last Post: 7th November 2006, 16:01

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.