Widgets Tutorial - Creating a Window

Files:

If a widget is created without a parent, it is treated as a window, or top-level widget, when it is shown. Since it has no parent object to ensure that it is deleted when no longer needed, it is up to the developer to keep track of the top-level widgets in an application.

In the following example, we use QWidget to create and show a window with a default size:

  1. #include <QtGui>
  2.  
  3. int main(int argc, char *argv[])
  4.  {
  5.     QApplication app(argc, argv);
  6.     QWidget window;
  7.     window.resize(320, 240);
  8.     window.show();
  9.     window.setWindowTitle(
  10.         QApplication::translate("toplevel", "Top-level widget"));
  11.     return app.exec();
  12. }

To create a real GUI, we need to place widgets inside the window. To do this, we pass a QWidget instance to a widget's constructor, as we will demonstrate in the next part of this tutorial.

Notes provided by the Qt Community

No notes