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.
npm run import -- "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();
}
#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();
}
The provided code is a simple Qt application that creates a window with a horizontal layout containing three buttons.
QApplication
: The main application object that manages the event loop.QWidget
: The base class for all user interface objects.QPushButton
: A button widget that can be clicked by the user.QHBoxLayout
: A layout manager that arranges widgets horizontally.main
function is the entry point of the application.QApplication
is instantiated with the command line arguments argc
and argv
.QWidget
is created to serve as the main window.QPushButton
instances are created with different labels.QHBoxLayout
is created to arrange the buttons horizontally.addWidget
.setLayout
.show
.exec
function is called on the QApplication
instance to start the event loop.exec
function returns, indicating the end of the event loop.