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. Call MainWindow function from static one
Qt 6.11 is out! See what's new in the release blog

Call MainWindow function from static one

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 955 Views 2 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.
  • C Offline
    C Offline
    column
    wrote on last edited by
    #1

    Hello,

    How to solve problem defined below? How to call nst() from st() ? I suppose I need to get pointer of class object that has nst() method.

    .h

    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void nst();
        static void st();
    private slots:
    
    
    private:
        Ui::MainWindow *ui;
    };
    

    *.cpp

    Ui::MainWindow *inst =NULL;
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        inst=ui;
        i=this;
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::nst()
    {
    
    }
    
    void MainWindow::st()
    {
    if (inst != NULL) inst->nst();
    
    }
    
    K 1 Reply Last reply
    0
    • C column

      Hello,

      How to solve problem defined below? How to call nst() from st() ? I suppose I need to get pointer of class object that has nst() method.

      .h

      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
          void nst();
          static void st();
      private slots:
      
      
      private:
          Ui::MainWindow *ui;
      };
      

      *.cpp

      Ui::MainWindow *inst =NULL;
      
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          inst=ui;
          i=this;
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      
      void MainWindow::nst()
      {
      
      }
      
      void MainWindow::st()
      {
      if (inst != NULL) inst->nst();
      
      }
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by koahnig
      #2

      @column

      That is certainly one way to do it. But you might ask yourself why you want to have a static function there in the first place.

      BTW that is standard C++ and has nothing to do with Qt.

      Vote the answer(s) that helped you to solve your issue(s)

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

        Hi,

        First by reviewing your design. Trying to call a member function from a static method of the same class is sign of something fishy going on.

        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
        3

        • Login

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