Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: want to hide mainwindow at program start up

  1. #1
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default want to hide mainwindow at program start up

    Hi,
    i have two window. First window is mainwindow and Second window is suppose form1.

    Now,
    Qt Code:
    1. if(condition)
    2. {
    3. display form1;
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: want to hide mainwindow at program start up

    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)

  3. #3
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

    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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: want to hide mainwindow at program start up

    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:
    Qt Code:
    1. MainWindow::doStuff() {
    2. // stuff
    3. if (condition) {
    4. OtherWindow *other = new OtherWindow; // no parent == top level window
    5. other->setAttribute(Qt::WA_DeleteOnClose); // important to release memory
    6. other->show();
    7. hide();
    8. }
    9. // possibly other stuff
    10. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

    thank you very much. but the problem is not solved. here is my code:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QFile file("E:/workplace/info.txt");
    8. QFile file1("E:/workplace/account_info.txt");
    9. if(file.exists() & file1.exists())
    10. {
    11. Form1 *frm1 = new Form1;
    12. frm1->setAttribute(Qt::WA_DeleteOnClose);
    13. frm1->show();
    14. this->hide();
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    where is the wrong???

  6. #6
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: want to hide mainwindow at program start up

    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.

  7. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: want to hide mainwindow at program start up

    you must be calling mainwindow->show() somewhere else in code... most likely in main()

  8. #8
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

    Hi;

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QFile file("E:/workplace/info.txt");
    8. QFile file1("E:/workplace/account_info.txt");
    9. if(file.exists() & file1.exists())
    10. {
    11. Form1 *frm1 = new Form1;
    12. frm1->setAttribute(Qt::WA_DeleteOnClose);
    13. frm1->setVisible(true);
    14. this->setVisible(false);
    15. frm1->setParent(0);
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

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

  9. #9
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

    call setParent before frm1->setVisible

  10. The following user says thank you to FelixB for this useful post:

    masuk (1st February 2011)

  11. #10
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

    i have call setParent before frm1->setVisible . but no change in output.
    Last edited by masuk; 1st February 2011 at 10:03.

  12. #11
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: want to hide mainwindow at program start up

    Have you read MrDeath's post?
    Can we see your main function?

  13. #12
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

    in main.cpp
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5.  
    6. MainWindow w;
    7. w.setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, w.size(), qApp->desktop()->availableGeometry()));
    8. //w.show(); //-------------------------------------------->>>> just add comment tag here like i do... your form will not shown at startup...
    9. //i used it for system tray icon
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  14. #13
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

    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

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QFile>
    5. #include "form1.h"
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12.  
    13. QFile file("E:/workplace/info.txt");
    14. QFile file1("E:/workplace/account_info.txt");
    15. if(file.exists() & file1.exists())
    16. {
    17. Form1 *frm1 = new Form1;
    18. frm1->setAttribute(Qt::WA_DeleteOnClose);
    19. frm1->setParent(0);
    20. frm1->setVisible(true);
    21. this->setVisible(false);
    22. }
    23.  
    24. }
    25.  
    26. MainWindow::~MainWindow()
    27. {
    28. delete ui;
    29. }
    To copy to clipboard, switch view to plain text mode 


    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    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)
    Last edited by masuk; 1st February 2011 at 11:15.

  15. #14
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

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

  16. The following user says thank you to FelixB for this useful post:

    masuk (1st February 2011)

  17. #15
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

    masuk please write solution code here... thanks

  18. #16
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

    THANX ALL FOR REPLY AND SUGGESTIONS.

    I SOLVED MY PROBLEM USING THIS

    Qt Code:
    1. this->setAttribute(Qt::WA_DontShowOnScreen);
    To copy to clipboard, switch view to plain text mode 

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

    thanx to all
    Last edited by masuk; 1st February 2011 at 11:39.

  19. #17
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: want to hide mainwindow at program start up

    well i dont think thats a good solution.

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

    Qt Code:
    1. mainwindow::mainwindow()
    2. {
    3. if ( mycondition)
    4. {
    5. from->show();
    6. }
    7. else
    8. this->show();
    To copy to clipboard, switch view to plain text mode 

    }

  20. #18
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

    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
    Qt Code:
    1. this->setAttribute(Qt::WA_DontShowOnScreen);
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. QFile file("E:/workplace/info.txt");
    2. QFile file1("E:/workplace/account_info.txt");
    3. if(file.exists() & file1.exists())
    4. {
    5. Form1 *frm1 = new Form1;
    6. this->setAttribute(Qt::WA_DontShowOnScreen);
    7. frm1->setVisible(true);
    8. }
    To copy to clipboard, switch view to plain text mode 

    but i need further better solution
    Last edited by masuk; 1st February 2011 at 17:43.

  21. #19
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Angry Re: want to hide mainwindow at program start up

    Quote Originally Posted by masuk View Post
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QFile file("E:/workplace/info.txt");
    8. QFile file1("E:/workplace/account_info.txt");
    9. if(file.exists() & file1.exists())
    10. {
    11. Form1 *frm1 = new Form1;
    12. frm1->setAttribute(Qt::WA_DeleteOnClose);
    13. frm1->setVisible(true);
    14. this->setVisible(false);
    15. frm1->setParent(0);
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. MainWindow w; // << Constructor is called here during object creation
    6.  
    7. // the other form may be visible here,
    8. // but probably not because you have yet to reach the event loop
    9.  
    10. w.show(); // << and then you show the main window
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by ChrisW67; 1st February 2011 at 21:33.

  22. #20
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: want to hide mainwindow at program start up

    Thanks ChrisW67

Similar Threads

  1. how to start a program in the background
    By JustinTerpstra in forum Qt Programming
    Replies: 2
    Last Post: 11th October 2010, 23:51
  2. Start program with right button
    By Pablo128 in forum Qt Programming
    Replies: 1
    Last Post: 29th August 2010, 13:03
  3. Can't start a qt-program on embedded linux
    By Tschounes in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 5th June 2010, 23:39
  4. add a program to start menu
    By kpmsivachand in forum KDE Forum
    Replies: 1
    Last Post: 26th January 2009, 20:19
  5. How to hide mainwindow at program start up
    By palmer in forum Qt Programming
    Replies: 4
    Last Post: 13th September 2008, 14:35

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.