PDA

View Full Version : want to hide mainwindow at program start up



masuk
1st February 2011, 06:16
Hi,
i have two window. First window is mainwindow and Second window is suppose form1.

Now,


if(condition)
{
display form1;
}


i can display form1 but at the same time mainwindow also displayed. But i need only form1.

Is there any way how to hide mainwindow at program startup.

maybe someone posted this question before but i failed to get ans.

advance thnx.

zgulser
1st February 2011, 07:50
Hi,

I'm not sure I quite understood your problem but, tuning visibility properties of both your form and mainwindow may make you achieve what you want..(by setting form->setParent(0) before doing it)

masuk
1st February 2011, 07:59
thnx zgulser for reply.

But i cannot solve my problem. i tried hide(),show(),visibility properties but failed to solve my problem.

please give detailed solution.

advance thanks

ChrisW67
1st February 2011, 08:17
Is there any way how to hide mainwindow at program startup.
If you don't want the main window then don't instantiate and show() it ;)

How about this:


MainWindow::doStuff() {
// stuff
if (condition) {
OtherWindow *other = new OtherWindow; // no parent == top level window
other->setAttribute(Qt::WA_DeleteOnClose); // important to release memory
other->show();
hide();
}
// possibly other stuff
}

masuk
1st February 2011, 08:34
thank you very much. but the problem is not solved. here is my code:



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

QFile file("E:/workplace/info.txt");
QFile file1("E:/workplace/account_info.txt");
if(file.exists() & file1.exists())
{
Form1 *frm1 = new Form1;
frm1->setAttribute(Qt::WA_DeleteOnClose);
frm1->show();
this->hide();
}
}


where is the wrong???

zgulser
1st February 2011, 08:39
What's your output according to the code you provided above? Nothing shown?

Defining form->setParent(0) explicitly may solve your problem..

By the way I strongly suggest you to use setVisible instead show or hide methods.

nish
1st February 2011, 08:40
you must be calling mainwindow->show() somewhere else in code... most likely in main()

masuk
1st February 2011, 09:01
Hi;



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

QFile file("E:/workplace/info.txt");
QFile file1("E:/workplace/account_info.txt");
if(file.exists() & file1.exists())
{
Form1 *frm1 = new Form1;
frm1->setAttribute(Qt::WA_DeleteOnClose);
frm1->setVisible(true);
this->setVisible(false);
frm1->setParent(0);
}
}


but output shows both window(mainwindow & form1). i need only the form1

FelixB
1st February 2011, 09:16
call setParent before frm1->setVisible

masuk
1st February 2011, 09:23
i have call setParent before frm1->setVisible . but no change in output.

nix
1st February 2011, 10:29
Have you read MrDeath's post?
Can we see your main function?

kosasker
1st February 2011, 10:43
in main.cpp

int main(int argc, char *argv[])
{
QApplication a(argc, argv);


MainWindow w;
w.setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, w.size(), qApp->desktop()->availableGeometry()));
//w.show(); //-------------------------------------------->>>> just add comment tag here like i do... your form will not shown at startup...
//i used it for system tray icon
return a.exec();
}

masuk
1st February 2011, 11:16
hi kosasker,

you are right. but i need to hide the main window only when the condition is true otherwise mainwindow will display.

thanx

mainwindow.cpp



#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include "form1.h"

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

QFile file("E:/workplace/info.txt");
QFile file1("E:/workplace/account_info.txt");
if(file.exists() & file1.exists())
{
Form1 *frm1 = new Form1;
frm1->setAttribute(Qt::WA_DeleteOnClose);
frm1->setParent(0);
frm1->setVisible(true);
this->setVisible(false);
}

}

MainWindow::~MainWindow()
{
delete ui;
}




main.cpp


#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


i am using qt gui. i want to show only second window(form1) when condition work. but in my output both windows is shown (mainwindow and form1)

FelixB
1st February 2011, 11:21
override showEvent for your MainWindow and check your condition there...

kosasker
1st February 2011, 11:30
masuk please write solution code here... :o thanks

masuk
1st February 2011, 11:31
THANX ALL FOR REPLY AND SUGGESTIONS.

I SOLVED MY PROBLEM USING THIS



this->setAttribute(Qt::WA_DontShowOnScreen);


If all of you have any other better solution please share.

thanx to all

nish
1st February 2011, 13:46
well i dont think thats a good solution.

just comment out show() call in main() and do a show() in mainwindow constructor.


mainwindow::mainwindow()
{
if ( mycondition)
{
from->show();
}
else
this->show();

}

masuk
1st February 2011, 17:37
Hi MrDeath

i tried using your code but problem was not solved because when check file exists output display second window but when check file does not exist mainwindow need tobe displayed but no window displayed (actually this->show() is not working). But when i used
this->setAttribute(Qt::WA_DontShowOnScreen); the problem is solved. though i think this is not a good solution. i need better solution.

Hi kosasker

i solved my problem using this:


QFile file("E:/workplace/info.txt");
QFile file1("E:/workplace/account_info.txt");
if(file.exists() & file1.exists())
{
Form1 *frm1 = new Form1;
this->setAttribute(Qt::WA_DontShowOnScreen);
frm1->setVisible(true);
}



but i need further better solution

ChrisW67
1st February 2011, 21:22
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QFile file("E:/workplace/info.txt");
QFile file1("E:/workplace/account_info.txt");
if(file.exists() & file1.exists())
{
Form1 *frm1 = new Form1;
frm1->setAttribute(Qt::WA_DeleteOnClose);
frm1->setVisible(true);
this->setVisible(false);
frm1->setParent(0);
}
}


but output shows both window(mainwindow & form1). i need only the form1

Line 13 and 14 are the exact equivalent of show() and hide().
Line 15 is unneeded because frm1 already has no parent.

The problem here is that you are doing this in the constructor for your MainWindow which has yet to be shown when it is invoked in:


int main(int argc, char *argv[])
{
QApplication a(argc, argv);

MainWindow w; // << Constructor is called here during object creation

// the other form may be visible here,
// but probably not because you have yet to reach the event loop

w.show(); // << and then you show the main window

return a.exec();
}

So you set up the second form and fiddle with visibility of the main window, which has not yet been shown, and the call show() on the main window.

You need to move the conditional block out of the constructor and either call it directly or use a single shot QTimer or other mechanism to arrange it to run automatically when the event loop is reached. Search for QTimer::singleShot recently in this forum.

masuk
2nd February 2011, 02:17
Thanks ChrisW67

FelixB
2nd February 2011, 07:53
but i need further better solution

there you go:


override showEvent for your MainWindow and check your condition there...

ferdna
20th October 2011, 19:01
QWidget *m;
if (condition)
m = new dlgMain1();
else
m = new dlgMain2();

m->show();

CodeLurker
26th November 2015, 20:27
This one had been biting me on the hiney for some time, so I thought I'd share what I went with.

If you do a "hide()" in the ctor, and then yer main looks like:



#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


what you have done is told it to hide(), and then show(). That won't work. I made a public variable in my subclassed QMainWindow, so that I do something similar to:



#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;

if (w.bRunHidden)
w.hide();
else
w.show();

return a.exec();
}


It also doesn't run right without the hide(). I do realize that this doesn't address your problem of showing the other form. If the parent is not visible, neither will the child be. Thus, you would have to make a parentless form, which makes sense on Windows, in any event. It will create its own taskbar entry. You will have to keep up with it's hiding and showing, however. If a user makes the MainWindow visible, and then minimizes it, your child form may need to be hidden too; and restore when the parent is restored. Depending on what you are doing, you might be taking the wrong approach. I made a serial number screen for a prog I'm working on; and simply created it, showed it and dealt with the response in the main, before I ever created the MainWindow. Also, I suppose making that a function is better form.