Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting variables to be addressed anywhere
Forum Updated to NodeBB v4.3 + New Features

Setting variables to be addressed anywhere

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • bart.hollisB Offline
    bart.hollisB Offline
    bart.hollis
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • bart.hollisB Offline
        bart.hollisB Offline
        bart.hollis
        wrote on last edited by
        #3

        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

        J.HilkJ 1 Reply Last reply
        0
        • bart.hollisB bart.hollis

          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

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          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();
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • bart.hollisB Offline
            bart.hollisB Offline
            bart.hollis
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved