Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simple parent and child window question
QtWS25 Last Chance

Simple parent and child window question

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 21.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    donturner
    wrote on last edited by
    #1

    I am trying to create a window which shows a child window as soon as it is created, I am using the following code in main.cpp:

    @#include <QtGui>
    #include <QApplication>

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

    QWidget mainWindow;
    QWidget *popup = new QWidget(&mainWindow);
    mainWindow.show();
    popup->show();
    
    return a.exec&#40;&#41;;
    

    }@

    Problem is, the main window shows but the child popup window does not. I think I'm missing something obvious but can't figure out what it is. Please could someone help.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thisisbhaskar
      wrote on last edited by
      #2

      When you call mainWindow.show(), it automatically shows all its children. you don't need to call popup->show() again.

      And your mainWindow or popup are just empty QWidgets. They don't have any content. Create some widgets like button and added them to your mainWindow or popup. Then you can see something.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        The widget you named "popup" is just an sub widget of "mainWindow" instead of an real Popup Window.

        If you want an Popup Window, you can use QDialog.

        1 Reply Last reply
        1
        • D Offline
          D Offline
          donturner
          wrote on last edited by
          #4

          Thank you both for your quick responses, that's sorted it:

          @#include <QtGui>
          #include <QApplication>

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

          QWidget mainWindow;
          mainWindow.show();
          
          QDialog popup(&mainWindow);
          popup.show();
          
          return a.exec&#40;&#41;;
          

          }@

          I'm guessing when QWidget is instantiated without a parent it defaults to creating a window which shows automatically, but when a parent is given in the constructor it assumes that it's not a window and that the parent will handle its display. Does that make sense?

          1 Reply Last reply
          0
          • T Offline
            T Offline
            thisisbhaskar
            wrote on last edited by
            #5

            [quote author="donturner" date="1310118497"]
            I'm guessing when QWidget is instantiated without a parent it defaults to creating a window which shows automatically, but when a parent is given in the constructor it assumes that it's not a window and that the parent will handle its display. Does that make sense?[/quote]

            yes you are true.

            How you can display for example is to add a button to main window, and connect button clicked signal to popup open() ( not show() ) function.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              changsheng230
              wrote on last edited by
              #6

              QWidget *popup = new QWidget(&mainWindow); Means that popup widget will be a sub componnet of the mainWindow.

              If you expect to see two seperate widgets show up at the same time, do like this:

              @int main(int argc, char *argv[])
              {
              QApplication a(argc, argv);
              QWidget mainWindow;
              //QWidget *popup = new QWidget(&mainWindow);
              QWidget *popup = new QWidget(); // do not set mainWindow as its parent
              mainWindow.show();
              popup->show();
              return a.exec();
              }
              @

              Chang Sheng
              常升

              1 Reply Last reply
              0
              • D Offline
                D Offline
                donturner
                wrote on last edited by
                #7

                Thanks very much for all your help, you guys rock.

                I wish the BlackBerry developer forums were this good!!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved