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

Call MainWindow function from static one



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


  • @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.


  • Lifetime Qt Champion

    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.


Log in to reply