Files
hello-world-qt/main.cpp
T
2026-10-17 16:56:59 -07:00

43 lines
1.2 KiB
C++

#include <QApplication>
#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QMainWindow mainWindow;
mainWindow.setWindowTitle("Hello World Qt");
mainWindow.resize(400, 300);
// Create central widget and layout
QWidget *centralWidget = new QWidget(&mainWindow);
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
// Create hello world label
QLabel *helloLabel = new QLabel("Hello World!", centralWidget);
helloLabel->setAlignment(Qt::AlignCenter);
helloLabel->setStyleSheet("font-size: 24px; font-weight: bold;");
// Create close button
QPushButton *closeButton = new QPushButton("Close", centralWidget);
closeButton->setStyleSheet("font-size: 16px; padding: 10px;");
// Connect button to close action
QObject::connect(closeButton, &QPushButton::clicked, &mainWindow, &QMainWindow::close);
// Add widgets to layout
layout->addStretch();
layout->addWidget(helloLabel);
layout->addWidget(closeButton);
layout->addStretch();
// Set central widget
mainWindow.setCentralWidget(centralWidget);
// Show the window
mainWindow.show();
return app.exec();
}