CopperSpice API  1.7.2
Widgets Concepts

Widgets are the basic building blocks for graphical user interface (GUI) applications built with CopperSpice. Each GUI component (e.g. buttons, labels, text editor) is a widget that is placed somewhere within a user interface window, or is displayed as an independent window. Each type of widget is provided by a subclass of QWidget, which is itself a subclass of QObject.

QWidget is not an abstract class. It can be used as a container for other widgets, and it can be subclassed with minimal effort to create new, custom widgets. QWidget is often used to create a window inside which other QWidgets are placed.

As with QObjects, QWidgets can be created with parent objects to indicate ownership, ensuring that objects are deleted when they are no longer used. With widgets, these parent-child relationships have an additional meaning: Each child widget is displayed within the screen area occupied by its parent widget. This means that when you delete a window widget, all the child widgets it contains are also deleted.

Writing a main Function

Many of the GUI examples provided with CopperSpice follow the pattern of having a main.cpp file, which contains the standard code to initialize the application, plus any number of other source/header files that contain the application logic and custom GUI components.

A typical main() function in main.cpp looks like this:

#include <QtGui>
// Include header files for application components.
// ...
int main(int argc, char *argv[])
QApplication app(argc, argv);
// Set up and show widgets.
// ...
return app.exec();

First, a QApplication object is constructed, which can then be configured with arguments passed in from the command line. After the widgets have been created and shown, QApplication::exec() is called to start the CopperSpice event loop. Control passes to CopperSpice until this function returns. Finally, main() returns the value returned by QApplication::exec().

Simple widget Examples

Each of theses simple widget examples is written entirely within the main() function.