tags:

views:

95

answers:

3

A friend of mine and myself are working on this application for QT4. Each of us has created parts of the gui that work. I am trying to integrate his work into the main window of the gui (his work is another form that should load up). As of now this is the code I am using to try and load his second window:

//connect buttons and such
connect(exitbtn, SIGNAL(triggered()),this,SLOT(terminated()));
connect(add, SIGNAL(triggered()),this,SLOT(add_rec()));


void MainWindowImpl::add_rec()
{
  //form quits as soon as it loads...?
  DialogImpl dia;//name of his form
  dia.show();
}

I have an include statement up top to include his header file. The program compiles just fine, but when I hit the trigger his form loads up for maybe 1/2 a second and then quits. Does anyone know what I am doing wrong?

A: 

Apparently QT4 only allows one instance of an object at a time, however pointers are another matter. Change both the main.cpp and what ever your main window to look something like this:

DialogImpl *dia=new DialogImpl;
dia->show();
Piratebill
This is wrong. The problem is that dia is getting destructed at the end of the function. (Qt4 definitely allows you to have multiple instances of a class at a time.)
Bill
+2  A: 

You have almost get it right. This is because the RAII of C++. If you allocate the Dialog on stack, it would be destructed as soon as the function return.

J-16 SDiZ
Set dia as a MainWindowImpl member variable and just show it in the add_rec function.
Patrice Bernassola
+2  A: 

Assuming MainWindowImpl inherits publically from QWidget, you're looking for this:

void MainWindowImpl::add_rec() 
{
  // passing "this" to the constructor makes sure dialog will be cleaned up.
  // Note that DialogImpl will need a constructor that takes a
  // QObject* parent parameter.
  DialogImpl* dialog = new DialogImpl(this);

  dialog->show(); 
}

Look at the Qt documentation for examples of how the constructors should look.

Bill