Results 1 to 11 of 11

Thread: Action on menu to display ui

  1. #1
    Join Date
    Jan 2006
    Posts
    15
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Action on menu to display ui

    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

  2. #2
    Join Date
    Jan 2006
    Posts
    32
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Action on menu to display ui

    are you using Qt3 or Qt4?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Action on menu to display ui

    Quote Originally Posted by jochen_r
    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:
    Qt Code:
    1. void mainform::showForm(){
    2. Form2 *f2 = new Form2(this); // you may omit "this" here to get a different effect
    3. f2->show();
    4. }
    To copy to clipboard, switch view to plain text mode 
    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.

  4. #4
    Join Date
    Jan 2006
    Posts
    32
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Action on menu to display ui

    Quote Originally Posted by wysota
    Qt Code:
    1. void mainform::showForm(){
    2. Form2 *f2 = new Form2(this); // you may omit "this" here to get a different effect
    3. f2->show();
    4. }
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Action on menu to display ui

    Quote Originally Posted by Dusdan
    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/...chapter02.html

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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Action on menu to display ui

    Quote Originally Posted by wysota
    Moreover, if you set an appropriate flag, the form will delete itself upon closing.
    But it's still a memory leak

    Qt Code:
    1. void mainform::showForm() {
    2. static Form2 *f2 = 0; // or declare f2 as a member variable
    3. if( f2 == 0 ) {
    4. f2 = new Form2();
    5. }
    6. f2->show();
    7. f2->setActiveWindow();
    8. f2->raise();
    9. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Action on menu to display ui

    Quote Originally Posted by jacek
    But it's still a memory leak
    Qt Code:
    1. QWidget *w = new QWidget(0,0,Qt::WDestructiveClose);
    2. w->show();
    To copy to clipboard, switch view to plain text mode 

    Is it?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Action on menu to display ui

    Quote Originally Posted by wysota
    Qt Code:
    1. QWidget *w = new QWidget(0,0,Qt::WDestructiveClose);
    2. w->show();
    To copy to clipboard, switch view to plain text mode 

    Is it?
    Now it isn't.

  9. #9
    Join Date
    Jan 2006
    Posts
    32
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Action on menu to display ui

    Quote Originally Posted by wysota
    http://doc.trolltech.com/3.3/qobject.html#QObject
    http://digitalfanatics.org/projects/...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.

  10. #10
    Join Date
    Jan 2006
    Posts
    15
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Action on menu to display ui

    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

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Action on menu to display ui

    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.

Similar Threads

  1. Docked Widget Menu
    By Chuk in forum Qt Programming
    Replies: 6
    Last Post: 3rd July 2013, 14:12
  2. Tray context menu display issues
    By r2hubert in forum Newbie
    Replies: 4
    Last Post: 16th July 2008, 16:44
  3. Tray context menu display issue
    By r2hubert in forum Qt Programming
    Replies: 1
    Last Post: 7th July 2008, 20:15
  4. Display only PNG image on desktop
    By durbrak in forum Qt Programming
    Replies: 32
    Last Post: 15th March 2008, 21:55
  5. ToolBar Action With Menu
    By indifference in forum Qt Programming
    Replies: 4
    Last Post: 10th September 2007, 19:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.