Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Setting variables to be addressed anywhere



  • I have this function in MainWindow.cpp:

    void MainWindow::getMainWindowSize()
    {
        const QRect geo = MainWindow::centralWidget()->geometry();
        m_top = geo.top();
        m_left = geo.left();
        m_bottom = geo.bottom();
        m_right = geo.right();
        return;
    }
    

    It sets the variables declared in mainwindow.h I intend to call it using a slot any time the main window gets changed to keep it up to date.

    I want to be able to read these variables from anywhere in the program so I can calculate and set the position of any pop up window. Is there a way without making globals? Or, is there a way to call MainWindow::centralWidget()->geometry(); from a non-member function?

    I tried making the function static but then I couldn't call geometry. I tried copying the variables to static variables but couldn't.


  • Lifetime Qt Champion

    Hi,

    If you are thinking about QDialog then giving the current widget as parent will make it open centred on it.



  • I am actually thinking about QMessageBox, which takes the same parameters. Hence my earlier question about finding the pointer to "this".

    I've got about 12 hours invested and decided to put all the functions into mainwindow.cpp which eliminates the problems. I'll work it out later. Going to be working 60 hrs per week for the foreseeable future, so this project will have to take a back seat. :(

    Thanks for your help!
    Bart


  • Moderators

    hi @bart.hollis ,
    if you have a continuous parent-child relation, you could use something like this function to retrieve your wanted information:

    QRect getRootGeometry(QWidget *obj)
    {
        if(obj->parentWidget())
            return getRootGeometry(obj->parentWidget());
        else
            return obj->geometry();
    }
    


  • I'm going to have another try at this. In my program is the usual MainWindow.cpp file. It creates an instance of QMainWindow. That MainWindow has a memory location with a pointer to it called "this".

    Now, I add a C++ code file to my project. My intention is to put all the database creation, testing and so on functions in this file. Including any and all error messages that may occur during execution of these functions. I want to be able to force the error messages to stay within the boundaries of the MainWindow. In order to do this, I have to use the geometry attributes of the MainWindow.

    If I try to do this from a function in my .cpp file, I get "Cannot call member function QMainWindow* MainWindow::getMainWindow() without object."

    OK, I remove to .cpp file and create an object file. Then, I can't call the MainWindow file from the newly created obgect or I get the attributes on the newly created object which is not what I need.

    So, I move the getAttributes function to the MainWindow source file. Good! I get just what I need... except, I can't find a way to transfer the attributes to the database management file.

    There has GOT to be a way other than writing this stuff to disk and then reading it from the functions that need it! And, although this may be more of a C++ problem, I thought this might be the place to ask. Can someone please tell me the secret?


  • Lifetime Qt Champion

    If you have a database management class, then equip it with the needed signals to propagate whatever information you want. Then connect it to your UI for whatever way you want to show these information.

    The database manager shouldn't care about how the errors are presented, it's not its role. That way you keep a clean separation between UI and application logic.


Log in to reply