June 12, 2011

Leon Leon
Robot Herder
400 posts

[Solved] How to hide and show a mainwindow?

Page  
1

So a dialog can be hide with hide(); and can been shown with show();
But what about if it is not a dialog but the mainwindow?
hide(); doesn’t hide my mainwindow….

21 replies

June 12, 2011

NarutoUzumakis NarutoUzumak..
Lab Rat
8 posts

why would you want to hide your mainwindow at all??can you be more specific

June 12, 2011

loladiro loladiro
Lab Rat
596 posts

I just tried it out, hide() hides my MainWindow. Are you reimplementing the event() method?

June 12, 2011

Stavros Filippidis Stavros Filippidis
Ant Farmer
354 posts
Leon wrote:
So a dialog can be hide with hide(); and can been shown with show(); But what about if it is not a dialog but the mainwindow? hide(); doesn’t hide my mainwindow….

How (eg: by using a push button?) and why do you plan to hide the main window? How and why do you plan to re-display the main window?

June 13, 2011

Leon Leon
Robot Herder
400 posts

My app pops app a dialog and from there you can take a screenshot. And it has the ability to hide the dialog and after the screenshot has been take show it again. But i also want to hide the mainwindow..

So at the mainwindow hide(); doesn’t hide the window.. What’s wrong?

June 13, 2011

Eddy Eddy
Area 51 Engineer
1296 posts

Using hide() should work on QMainwindow.

I just tested it to be sure since i never needed this.

So there must be something else going on
I suggest you make a minimal compilable example that reproduces your problem or show us the relevant code.

 Signature 

Qt Certified Specialist
Qt Ambassador

June 13, 2011

Leon Leon
Robot Herder
400 posts

It must be a bug of Ubuntu 11.04 again…

As for an example… Putting hide(); at the constructor of the mainwindow.cpp won’t hide the window…

June 13, 2011

Stavros Filippidis Stavros Filippidis
Ant Farmer
354 posts

Leon wrote:
My app pops app a dialog and from there you can take a screenshot. And it has the ability to hide the dialog and after the screenshot has been take show it again. But i also want to hide the mainwindow..

So at the mainwindow hide(); doesn’t hide the window.. What’s wrong?

Just a guess: maybe you are calling hide() method/function of the wrong object (ie: not for the main window) by accident? Check it! ;-)

June 13, 2011

Eddy Eddy
Area 51 Engineer
1296 posts

try to use hide in your main()

there you make an instance of your mainwindow class and you hide that instance.

 Signature 

Qt Certified Specialist
Qt Ambassador

June 13, 2011

jim_kaiser jim_kaiser
Lab Rat
144 posts

As for an example… Putting hide(); at the constructor of the mainwindow.cpp won’t hide the window…

I suspected that’s what you were doing. I don’t think that should be expected to work. Although, the QMainWindow is constructed by then, i think you should try it after the construction of the MainWindow object is complete. Also, are you saying that the main window is shown without calling show()? Because you call hide() in the MainWindow constructor, where do you call show?

June 13, 2011

Eddy Eddy
Area 51 Engineer
1296 posts

could you make an new widget based Qt project, choose a QMainwindow as class and use the following code in your main.

like this :

  1. int main(int argc, char *argv[])
  2. {
  3.     QApplication a(argc, argv);
  4.     MainWindow w;
  5.     w.show();
  6.    
  7.     QTimer timer;
  8.    
  9.     timer.singleShot(1000, &w, SLOT(hide()));
  10.    
  11.     return a.exec();
  12. }

normally this should work unless you have other issues.

 Signature 

Qt Certified Specialist
Qt Ambassador

June 13, 2011

Eddy Eddy
Area 51 Engineer
1296 posts

@jim_kaiser
it is possible to use hide() in the constructor of a QMainwindow class.
I tested it using a singleshot timer too.

you’re right asking where Leon uses his show() function, probably this one is called after the hide() function.

 Signature 

Qt Certified Specialist
Qt Ambassador

June 13, 2011

jim_kaiser jim_kaiser
Lab Rat
144 posts

@eddy

Thanks for the clarification about the hide() in the constructor. Assumed there might be events from the desktop which the main windows reacts to after the construction and just thought its a bit iffy.

June 13, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

Hi,

by default each widget is hiodden, unless it has a visible parent. So hide in the constructorn is not needed. But mostly, the main contains a show call, remove that and it is hidden.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

June 13, 2011

Eddy Eddy
Area 51 Engineer
1296 posts

Hi Gerolf,

You’re right it’s hidden unless you show it first.

But what Leon wants is to use a mainwindow and click a button to hide it. Hiding seems not to work on his system. That’s why i suggested to use a minimal working example because we don’t have code to look at.

 Signature 

Qt Certified Specialist
Qt Ambassador

June 13, 2011

Leon Leon
Robot Herder
400 posts

Ok so probably it was wrong to put hide(); at the constructor.. Putting it in a void function will work..
I made an example that hides both the dialog and the mainwindow :) (Link [dl.dropbox.com])

Page  
1

  ‹‹ How to set QIcon::fromTheme() to a label?      Detecting Qt libraries in the ’framework’ format ››

You must log in to post a reply. Not a member yet? Register here!