Why is my mainwindow closing automatically when called from a differnet class?

StackOverflow https://stackoverflow.com/questions/14063614

  •  12-12-2021
  •  | 
  •  

سؤال

I can't seem to figure out what went wrong so I'm here to ask you. I have made a simple class called BOBSNetworkSessionManager defined below. It is a simple class that inherits the QOBject so that I can use signals and slots but it does not have a dialog or any kind of window associated with it. It will eventually call a log in dialog and use the credentials to connect to a tcp server that I have created. This class serves as a layer to manage the connection state of the program because it will only run properly when connected to the server and when being used within 15 minutes without break due to p.c.i. compliance. If these conditions are not true this class will lock the window and force a new login. As of right now I just try to arbitrarily open the main window as though credentials had passed and i wasbconnected to the server. The problem is when I open the mainwindow it disapears right away. I cannot seem to figure out why it is diappearing. I have included all of my files.

BOBSDCNetworkSessionManager .h header file

#ifndef BOBSDCNETWORKSESSIONMANAGER_H
#define BOBSDCNETWORKSESSIONMANAGER_H

#include <QObject>
#include <QSettings>

class BOBSDCNetworkSessionManager : public QObject
{
    Q_OBJECT
public:
    explicit BOBSDCNetworkSessionManager(QObject *parent = 0);

protected:
    void destroyed(QObject *);

signals:

public slots:

private:
void readSettings();
void writeSettings();
QSettings networkSettings;

};

#endif // BOBSDCNETWORKSESSIONMANAGER_H

BOBSDCNetworkSessionManager Implementation .cpp file

#include "bobsdcnetworksessionmanager.h"
#include "bobsmainwindow.h"

 BOBSDCNetworkSessionManager::BOBSDCNetworkSessionManager(QObject *parent) :
    QObject(parent)
{
    BOBSMainWindow w;
    w.show();
}

Main.cpp file

#include "bobsdcnetworksessionmanager.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setApplicationName("Enterprise Management Suite");
    a.setApplicationVersion("Beta Version: 0.0.0.01");
    a.setOrganizationName("Enigma Web Consulting");
    a.setOrganizationDomain("http://www.EnigmaWebCo.com");

    BOBSDCNetworkSessionManager netMgr;

    return a.exec();
 }
هل كانت مفيدة؟

المحلول

The problem is here:

{
    BOBSMainWindow w;
    w.show();
}

w.show() is not a blocking call. So you're creating a window, showing it, and then it immediately is destructed when it goes out of scope. You should either declare w as a member variable or construct it on the heap:

BOBSMainWindow *w = new BOBSMainWindow(this);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top