PDA

View Full Version : new Form(QWidget) don't show when passing parent



Talei
22nd January 2010, 00:45
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.


Form fr = new Form(this);
fr->setWindowTitle(tr("Form Info/About"));
fr->show();
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 :


fr = new Form(this);
fr->setWindowFlags( Qt::Window);
fr->setWindowModality( Qt::ApplicationModal );
fr->show();
fr->setFocus();
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

wysota
22nd January 2010, 11:47
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.

Talei
22nd January 2010, 20:36
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 :

Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form)
{
ui->setupUi(this);
}

the "this" refers to parent (Form), but when I use :

Form fr = new Form(this);
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. :

fr->setWindowFlags( Qt::Window );
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:

fr = new Form(this);
fr->setParent( this );
fr->show();
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

wysota
23rd January 2010, 09:50
I think (not sure about this please correct me) that when I use, generated with QtCreator, Form with it's own Ui :

Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form)
{
ui->setupUi(this);
}

the "this" refers to parent (Form),
Not really. It refers to "this" object which is not any parent.


but when I use :

Form fr = new Form(this);
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. :

fr->setWindowFlags( Qt::Window );
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".

Talei
23rd January 2010, 20:45
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):

Form *fr = new Form();
fr->setVisible(true);
and qDebug() << this-parent(); gave me: QObject(0x0)

Second attachment 2.jpg (Form overpainted on MainWindow):

Form *fr = new Form( this );
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):

Form *fr = new Form( this );
fr->setWindowFlags( Qt::Window );
and qDebug() << this-parent(); gave me: MainWindow(0x22fe80, name = "MainWindow")

To test parent I use:

Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form)
{
ui->setupUi(this);
qDebug() << this->parent();
}

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.

wysota
23rd January 2010, 21:45
Inherit your "Form" class from QDialog instead of QWidget and you'll get what you want.

Talei
23rd January 2010, 23:55
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

wysota
23rd January 2010, 23:59
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.