qt | | test qt application | Search

The code creates a simple Qt application that displays a window with three buttons arranged horizontally using a QHBoxLayout. The application runs until the user closes the window, at which point the event loop ends and the application terminates.

Run example

npm run import -- "qt application"

qt application

#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>

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

    QWidget window;
    window.setWindowTitle("Qt Layout Example");

    QPushButton *button1 = new QPushButton("Button 1");
    QPushButton *button2 = new QPushButton("Button 2");
    QPushButton *button3 = new QPushButton("Button 3");

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);

    window.setLayout(layout);
    window.show();

    return app.exec();
}

What the code could have been:

#include 
#include 
#include 
#include 

/**
 * @brief Main function responsible for creating and running the application.
 *
 * @param argc The number of command line arguments.
 * @param argv An array of command line arguments.
 *
 * @return The application's exit code.
 */
int main(int argc, char *argv[]) {
    // Create a new instance of QApplication
    QApplication app(argc, argv);

    // Create a new instance of QWidget to serve as the application window
    QWidget window;
    // Set the window title
    window.setWindowTitle("Qt Layout Example");

    // Create three instances of QPushButton
    QPushButton *button1 = new QPushButton("Button 1");
    QPushButton *button2 = new QPushButton("Button 2");
    QPushButton *button3 = new QPushButton("Button 3");

    // Create a new instance of QHBoxLayout to manage the button layout
    QHBoxLayout *layout = new QHBoxLayout;

    // Add the buttons to the layout in sequence
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);

    // Set the layout for the application window
    window.setLayout(layout);
    // Show the window on screen
    window.show();

    // Run the application's event loop
    return app.exec();
}

Overview

The provided code is a simple Qt application that creates a window with a horizontal layout containing three buttons.

Includes

Main Function

Initialization

Creating UI Components

Creating a Layout

Setting Up the Window

Running the Application