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. Issue with QMainWindow instance access
Qt 6.11 is out! See what's new in the release blog

Issue with QMainWindow instance access

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.8k 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.
  • M Offline
    M Offline
    MichaelM
    wrote on last edited by
    #1

    Hi,

    I'm having an OO-programming issue when trying to access functions contained in the class derived from QMainWindow.

    Here are the simplified corresponding parts of my code :

    In mainwindow.h :

    class MainWindow : public QMainWindow
    {
    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    public:
    void theFunctionIWantToCallOnWindowInstance();

    private:
    Ui::MainWindow *ui;

    In mainwindow.cpp :

    void MainWindow::theFunctionIWantToCallOnWindowInstance()
    {
    QMessageBox::information(this, "[DEBUG]", "theFunctionIWantToCallOnWindowInstance() called");
    }

    In main.cpp file :

    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    }

    In external.cpp :

    Basically, I simply want to call a function from external code on the window instance created in main.cpp, something like :

    #include "mainwindow.h"
    ...
    MainWindow::getInstanceCreatedInMain().theFunctionIWantToCallOnWindowInstance();

    I tried many different approaches using singletons and static (and even global) variables but I cannot solve my problem i.e. get a reference to my window from the external code.

    Thanks in advance for your help.

    Michael

    1 Reply Last reply
    0
    • Hamed.MasafiH Offline
      Hamed.MasafiH Offline
      Hamed.Masafi
      wrote on last edited by
      #2

      @MichaelM said:

      getInstanceCreatedInMain

      Where is the getInstanceCreatedInMain defined?

      Remote object sharing (OO RPC)
      http://forum.qt.io/topic/60680/remote-object-sharing-oo-rpc-solved

      Advanced, Powerful and easy to use ORM for Qt5
      https://forum.qt.io/topic/67417/advanced-powerful-and-easy-to-use-orm-for-qt5

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        Hi and welcome,

        Using globals or calling a method of a main window deep in your code are signs of flawed design. There's usually no (good) reason to do that. You could, for example, emit a signal and connect that to the main window. This way you won't create a coupling, where a child needs to know about its parent.

        To answer your question and tell you how to shoot yourself in the foot - here's how you would make your single instance of main window accessible from anywhere in the code:

        //mainwindow.h
        class MainWindow : public QMainWindow
        {
           ...
        public:
           static MainWindow* instance();
        private:
           static MainWindow* s_instance;
        };
        
        //mainwindow.cpp
        static MainWindow* MainWindow::s_instance = nullptr;
        
        MainWindow* MainWindow::instance() { return s_instance; }
        
        MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
        {
           assert(!s_instance); //another instance already exists!
           s_instance = this;
        }
        

        Then you can reference the main window from anywhere like this:

        MainWindow::instance()->doWhatever();
        

        Just to repeat - I advise against using this. You have a case of inverted relation in your design and it would be better to look into that.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MichaelM
          wrote on last edited by
          #4

          Thanks a lot for your help.
          I finally managed to solve my problem by using your method. I know about coupling but for some reasons I cannot change the structure / design of external.cpp.

          Just a detail, in the code you provided I had to replace :

          //mainwindow.cpp
          static MainWindow* MainWindow::s_instance = nullptr;

          By :

          //mainwindow.cpp
          MainWindow* MainWindow::s_instance = nullptr;

          as it throws the compilation error :
          error: 'static' may not be used when defining (as opposed to declaring) a static data member [-fpermissive]

          1 Reply Last reply
          0

          • Login

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