Interface component Qt Widgets getting started guide, making cross-platform embedded interface development easier!

Qt  is currently the most advanced and complete cross-platform C++ development tool. It not only fully realizes one-time writing, but also runs on all platforms indiscriminately, and provides almost all tools needed in the development process. Today, Qt has been used in more than 70 industries, thousands of enterprises, supporting millions of devices and applications.

Click to get Qt Widget component download (Q technology exchange: 166830288)

introduce

Widgets are the basic building blocks of graphical user interface (GUI) applications built with Qt. Each GUI component (such as a button, label, text editor) is a widget that is placed somewhere in the UI window, or Displayed as a separate 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 easily subclassed to create new custom widgets. QWidgets are generally used to create a window in which to place other QWidgets.

Like QObjects, QWidgets can be created with a parent object to indicate ownership, ensuring that the object is deleted when it is no longer used. For widgets, these parent-child relationships have additional implications: each child widget is displayed within the screen area occupied by its parent widget. This means that when you delete a widget, all child widgets it contains are also deleted.

Write the Main function

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

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

#include <QtWidgets>

// 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 be configured with parameters passed in from the command line. After creating and displaying the widget, call QApplication::exec() to start Qt's event loop. Control is passed to Qt until the function returns, and finally main() returns the value returned by QApplication::exec().

Real-world widget example

In these more advanced examples, the code to create widgets and layouts is stored in other files. For example, the GUI of the main window can be created in the constructor of a QMainWindow  subclass.

build example

If you installed a binary package to get Qt, or compiled Qt yourself, the examples described in this tutorial should already be built and ready to run. If you wish to modify and recompile them, follow these steps:

  1. At the command prompt, enter the directory containing the modified examples.
  2. Type qmake and hit enter, if that doesn't work, make sure the executable is on your path, or enter its full location.
  3. On Linux/Unix and macOS, type make and press Return; on Windows using Visual Studio, type nmake and press Return.

Create an executable in the current directory, on Windows this may be in a debug or release subdirectory, which you can run to see the sample code in action.

Guess you like

Origin blog.csdn.net/AABBbaby/article/details/131889483