PDA

View Full Version : Action on menu to display ui



jochen_r
9th January 2006, 15:42
Hi,

I can create an application with Qt designer to display an ui. Works fine. This ui (mainform) has a QMenubar and by choosing a certain menu item I want to display another ui (Form2).

What do I need for this? I have so far:

- 2 ui Files (mainform and Form2) created with Qt designer.
- I need a sender/slot relation Sender: menuAction Signal: activated() Receiver: mainform or Form2? Slot: showform()
- The implementation of showform so far

void mainform::showform()
{
Form2 z;
z.show(); // anlogue to what happens in main.cpp
}

Anyway the compiler throws an error, the guess is probably too easy....

What would be the correct way to do it?

Regards,
Jochen

Dusdan
9th January 2006, 15:45
are you using Qt3 or Qt4?

wysota
9th January 2006, 16:53
Anyway the compiler throws an error, the guess is probably too easy....

It would be even easier if you told us what the error thrown by the compiler is :)



What would be the correct way to do it?

Well...

1. Create ui files (you did that already)
2. Make sure there is a menu entry you want to use and remember its name
3. In "edit" menu choose connections and make the connection there. You'll need to add a new slot to the form, for example "void showForm()"

It needs to have such content:

void mainform::showForm(){
Form2 *f2 = new Form2(this); // you may omit "this" here to get a different effect
f2->show();
}
You can't do it the way you did, because if you don't declare a pointer to an object but create the object directly on stack, it'll be destroyed as soon as the function ends and you'll see nothing.

4. Save everything and compile your project. Remember to call qmake if needed.

Dusdan
9th January 2006, 16:59
void mainform::showForm(){
Form2 *f2 = new Form2(this); // you may omit "this" here to get a different effect
f2->show();
}there's a memory leak in this code, because you won't be able to delete f2. You should declare it as member of the main class, so you can delete it when you don't need it anymore.

wysota
9th January 2006, 17:13
there's a memory leak in this code, because you won't be able to delete f2. You should declare it as member of the main class, so you can delete it when you don't need it anymore.

http://doc.trolltech.com/3.3/qobject.html#QObject
http://digitalfanatics.org/projects/qt_tutorial/chapter02.html

Moreover, if you set an appropriate flag, the form will delete itself upon closing.

jacek
9th January 2006, 17:20
Moreover, if you set an appropriate flag, the form will delete itself upon closing.
But it's still a memory leak ;)


void mainform::showForm() {
static Form2 *f2 = 0; // or declare f2 as a member variable
if( f2 == 0 ) {
f2 = new Form2();
}
f2->show();
f2->setActiveWindow();
f2->raise();
}

wysota
9th January 2006, 17:25
But it's still a memory leak ;)


QWidget *w = new QWidget(0,0,Qt::WDestructiveClose);
w->show();

Is it?

jacek
9th January 2006, 17:29
QWidget *w = new QWidget(0,0,Qt::WDestructiveClose);
w->show();

Is it?
Now it isn't.

Dusdan
9th January 2006, 17:35
http://doc.trolltech.com/3.3/qobject.html#QObject
http://digitalfanatics.org/projects/qt_tutorial/chapter02.html

Moreover, if you set an appropriate flag, the form will delete itself upon closing.sure, i justed wanted to point out that things like

Form2 *f2 = new Form2();

with no parent specified lead to memory leaks.

jochen_r
10th January 2006, 03:35
Hi,

thanks for the various hints. Unfortunately it still doesn't work. :(

In Form1.ui I have specified "forward (for implementation)" "Form2.ui.h" for all tries below.


1. possibility

void Form1::showform2()
{
QWidget *w = new QWidget(0,0,Qt::WDestructiveClose);
w->show();
}

If I use this it compiles. But if I click the menu, it opens a new (empty!) window every time I click the menu. The window is never closed again. To avoid a missunderstanding, I want the new ui displayed inside the active window.

2. possibility

36 void Form1::showform2()
37 {
38 static Form2 *f2 = 0; // or declare f2 as a member variable
39 if( f2 == 0 )
40 {
41 f2 = new Form2();
42 }
43 f2->show();
44 f2->setActiveWindow();
45 f2->raise();
46 }

In file included from form1.cpp:27:
form1.ui.h: In member function `virtual void Form1::showform2()':
form1.ui.h:38: error: ISO C++ forbids declaration of `Form2' with no type
form1.ui.h:38: error: syntax error before `*' token
form1.ui.h:39: error: `f2' undeclared (first use this function)
form1.ui.h:39: error: (Each undeclared identifier is reported only once for each function it appears in.)
form1.ui.h:41: error: syntax error before `(' token
form1.ui.h:38: warning: unused variable `int Form2'


3. possibility (eventhough it has a memory leak)

35 void mainform::showForm(){
36 Form2 *f2 = new Form2(this);
37 f2->show();
38 }

In file included from form1.cpp:27:
form1.ui.h:35: error: syntax error before `::' token
form1.ui.h:37: error: syntax error before `->' token

Thanks very much so far.

Jochen

wysota
10th January 2006, 11:10
1. Forget it, it was just an example.

2. #include "form2.h"

3. Your class names don't match.


To avoid a missunderstanding, I want the new ui displayed inside the active window.

It's easier to use a widget stack (QWidgetStack) instead of opening a new form then.