Results 1 to 8 of 8

Thread: new Form(QWidget) don't show when passing parent

  1. #1
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default new Form(QWidget) don't show when passing parent

    Hello.
    I stumble upon (very easy problem, but can't figure it out) problem with showing another Form.
    I created application with "main" form, then I want show "About/Info" form but when I pass as parent (this) another form don't show.
    Qt Code:
    1. Form fr = new Form(this);
    2. fr->setWindowTitle(tr("Form Info/About"));
    3. fr->show();
    To copy to clipboard, switch view to plain text mode 
    Form is "standard" form generated with QtCreator (class Form : public QWidget {...)

    I studied examples and my code is practically identical.
    Also when I pass no argument form show but is a independent window (shows in task bar) and after closing "main" window form window stays open.
    Any suggestion are more then welcome.
    Qt 4.6 Win
    PS. same situation with qt4.6.1 on Win/Lin x64

    PS2. ok I SOLVED my problem by doing :

    Qt Code:
    1. fr = new Form(this);
    2. fr->setWindowFlags( Qt::Window);
    3. fr->setWindowModality( Qt::ApplicationModal );
    4. fr->show();
    5. fr->setFocus();
    To copy to clipboard, switch view to plain text mode 
    and from what I see the "problem" is "this" in Form. It seams that this not points to parent Form but i.e MainWindow.
    What helps is "fr->setWindowFlags( Qt::Window);" that probably change parent.
    I would like to know why is that happening.

    Best regards
    Last edited by Talei; 22nd January 2010 at 03:18.

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

    Default Re: new Form(QWidget) don't show when passing parent

    A widget with a parent doesn't become a window but is embedded into its parent. If you don't put it in a layout it might end up in a strange place on the parent and might have an arbitrary size (0x0 included). So if you want your widget to be a window, simply don't pass a parent at all.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: new Form(QWidget) don't show when passing parent

    Thank you for reply.
    That's explains why qt examples work correctly (they manually put it in layout) and my code don't. (Form generated with it's own Ui in QtCreator). I think (not sure about this please correct me) that when I use, generated with QtCreator, Form with it's own Ui :
    Qt Code:
    1. Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form)
    2. {
    3. ui->setupUi(this);
    4. }
    To copy to clipboard, switch view to plain text mode 
    the "this" refers to parent (Form), but when I use :
    Qt Code:
    1. Form fr = new Form(this);
    To copy to clipboard, switch view to plain text mode 
    the "this" refers to top level window i.e. MainWindow. (And from my "experiments" when I use in ui->setupUi(this->parentWidget()) content of Form is "over painted" in MainWindow, that's exactly what you explained in previous post, Ui is not setup properly). Is that thinking correct?

    Can you also explain to me, why when I use i.e. :
    Qt Code:
    1. fr->setWindowFlags( Qt::Window );
    To copy to clipboard, switch view to plain text mode 
    window is shown properly, i.e. is NOT independent window but child window of MainWindow and is not present in taskbar (that's exactly what I want to achieve ). From doc, setWindowFlags() call method setParent() that resets window flags, but when I try to use fallowing code:
    Qt Code:
    1. fr = new Form(this);
    2. fr->setParent( this );
    3. fr->show();
    To copy to clipboard, switch view to plain text mode 
    Form is "over painted" on MainWindow. Problem is "fr->setParent( this );" and pointer "this" that referes to MainWindow. How to point it to Form?

    Sorry for my "engrish"
    Best regards

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

    Default Re: new Form(QWidget) don't show when passing parent

    Quote Originally Posted by Talei View Post
    I think (not sure about this please correct me) that when I use, generated with QtCreator, Form with it's own Ui :
    Qt Code:
    1. Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form)
    2. {
    3. ui->setupUi(this);
    4. }
    To copy to clipboard, switch view to plain text mode 
    the "this" refers to parent (Form),
    Not really. It refers to "this" object which is not any parent.

    but when I use :
    Qt Code:
    1. Form fr = new Form(this);
    To copy to clipboard, switch view to plain text mode 
    the "this" refers to top level window i.e. MainWindow.
    No, it also refers to the current (this) object which then becomes the parent of the widget.

    Can you also explain to me, why when I use i.e. :
    Qt Code:
    1. fr->setWindowFlags( Qt::Window );
    To copy to clipboard, switch view to plain text mode 
    window is shown properly, i.e. is NOT independent window but child window of MainWindow and is not present in taskbar (that's exactly what I want to achieve ).
    I'm not sure what you mean. It should be an independent window, that's what the Qt::Window flag does. Maybe we understand the word "independent" differently. In my understanding it means that if you move the MainWindow, the other object won't move and it will have its own title bar, etc. "Child window" would mean the "child" is embedded ("overpainted" in your words) in the "parent".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: new Form(QWidget) don't show when passing parent

    Sorry for not being clear from the beginning.

    First attachment 1.jpg (two "independent" windows, I can close one (no mater which one), and second stays open):
    Qt Code:
    1. Form *fr = new Form();
    2. fr->setVisible(true);
    To copy to clipboard, switch view to plain text mode 
    and qDebug() << this-parent(); gave me: QObject(0x0)

    Second attachment 2.jpg (Form overpainted on MainWindow):
    Qt Code:
    1. Form *fr = new Form( this );
    To copy to clipboard, switch view to plain text mode 
    and qDebug() << this-parent(); gave me: MainWindow(0x22fe80, name = "MainWindow")

    and third example 3.jpg, that's what I want to achieve (Form as "child" window to MainWindow, don't know if "naming" is correct):
    Qt Code:
    1. Form *fr = new Form( this );
    2. fr->setWindowFlags( Qt::Window );
    To copy to clipboard, switch view to plain text mode 
    and qDebug() << this-parent(); gave me: MainWindow(0x22fe80, name = "MainWindow")

    To test parent I use:
    Qt Code:
    1. Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form)
    2. {
    3. ui->setupUi(this);
    4. qDebug() << this->parent();
    5. }
    To copy to clipboard, switch view to plain text mode 

    And I don't know why/how setWindowFlags() resets window flags or sets properly parent object.
    Also haw can I use setParent(QWidget *parent) to set parent in that scenario?

    Best regards and thanks for help.
    Attached Images Attached Images
    • File Type: jpg 1.JPG (11.3 KB, 23 views)
    • File Type: jpg 2.JPG (10.8 KB, 20 views)
    • File Type: jpg 3.JPG (9.4 KB, 19 views)

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

    Default Re: new Form(QWidget) don't show when passing parent

    Inherit your "Form" class from QDialog instead of QWidget and you'll get what you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: new Form(QWidget) don't show when passing parent

    The "problem" is that I want to know why using method setWindowFlags( Qt::Window ); it works like I want.
    And also in examples "qt\examples\widgets\windowflags" they use QWidget and it works.
    It's not a problem, but I'm just curious and want to learn.

    best regards

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

    Default Re: new Form(QWidget) don't show when passing parent

    Quote Originally Posted by Talei View Post
    The "problem" is that I want to know why using method setWindowFlags( Qt::Window ); it works like I want.
    Because it forces a widget with a parent set to become a window. Unless you really, really know you should use it, don't. Deriving from QDialog is much more straightforward and maintainable.

    The example you mention is exactly that - an example, and its goal is to present different window flags so deriving from QDialog there would serve no purpose.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    Talei (23rd January 2010)

Similar Threads

  1. Promoting the parent QWidget of a QWidget form
    By extrakun in forum Qt Tools
    Replies: 6
    Last Post: 16th April 2010, 14:19
  2. Passing an object from one form to the other.
    By cbarmpar in forum Qt Programming
    Replies: 10
    Last Post: 3rd September 2008, 14:12
  3. QWidget::exec() and QWidget::show()
    By MarkoSan in forum Qt Programming
    Replies: 6
    Last Post: 18th October 2007, 21:39
  4. Replies: 2
    Last Post: 17th October 2006, 19:25
  5. Passing event to parent
    By QPissedOff in forum Newbie
    Replies: 1
    Last Post: 26th April 2006, 16: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.