binu j
binu j

Reputation: 399

Showing different Class from main.cpp

I am new to qt. i have an application which have several forms.

i am trying to select the specific form from main.cpp, but it just flickered the form. But i am getting the debug values of the form and the form is invisible.

My main.cpp code

#include "dialog.h"
#include "design1.h"
#include <QApplication>
#include <QtCore>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    int theme  = 2;
    if(theme == 1)
    {
       Design1 w;
       w.showMaximized();
       w.show();
   }
   else
   {
      Dialog w;
      w.showMaximized();
      w.show();
   }
 return a.exec();
}

Upvotes: 1

Views: 53

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

The problem is that w in both cases have a limited scope inside the "if" so they are destroyed instantly. One solution is to use pointers managing dynamic memory, for example using QScopedPointer:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QScopedPointer<QWidget> w;

    int theme  = 2;

    if(theme == 1)
    {
        w.reset(new Design1);
    }
    else{
      w.reset(new Dialog);
    }

    if(w){
        w->showMaximized();
        w->show();
    }

    return a.exec();
}

Upvotes: 2

Related Questions