Skip to content

Commit 2ee70e3

Browse files
author
XuhuaHuang
committed
Update qt hello world example
1 parent ec9cc75 commit 2ee70e3

4 files changed

Lines changed: 68 additions & 33 deletions

File tree

Qt/HelloGUI/.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json",
3+
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
4+
}

Qt/HelloGUI/TestWidget.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
1+
// clang-format off
12
/*****************************************************************//**
23
* \file TestWidget.cpp
3-
* \brief
4+
* \brief Implementation of the TestWidget class.
45
*
56
* \author Xuhua Huang
67
* \date February 11, 2021
78
*********************************************************************/
9+
// clang-format on
810

911
#include "TestWidget.h"
1012

1113
// TestWidget class constructor
1214
// pointer parent is passed to parent class constructor
1315
TestWidget::TestWidget(QWidget* parent)
14-
: QWidget(parent) {
15-
// instanciate a vertical box layout manager
16-
QVBoxLayout* vbox = new QVBoxLayout();
16+
: QWidget(parent) {
17+
// instanciate a vertical box layout manager
18+
QVBoxLayout* vbox = new QVBoxLayout();
1719

18-
// instantiate a label with "new" keyword and call QLabel constructor
19-
label = new QLabel("Hello Qt World");
20-
label->setFont(QFont("Times New Roman", 11));
20+
// instantiate a label with "new" keyword and call QLabel constructor
21+
label = new QLabel("Hello Qt World");
22+
label->setFont(QFont("Times New Roman", 11));
2123

22-
// set the background to green and text to white
23-
label->setStyleSheet("QLabel { background-color : green; color : white; }");
24+
// set the background to green and text to white
25+
label->setStyleSheet("QLabel { background-color : green; color : white; }");
2426

25-
// create a new label to implement very first hyperlink
26-
QLabel* label2 = new QLabel();
27-
showHyperlink(label2); // call function
27+
// create a new label to implement very first hyperlink
28+
label2 = new QLabel();
29+
showHyperlink(label2); // call function
2830

29-
// add the label to the layout manager
30-
vbox->addWidget(label);
31-
vbox->addWidget(label2); // label2 still exists since it is a pointer
31+
// add the label to the layout manager
32+
vbox->addWidget(label);
33+
vbox->addWidget(label2); // label2 still exists since it is a pointer
3234

33-
// notice that there can only be one setLayout(): at the end of the
34-
// constructor
35-
this->setLayout(vbox);
35+
// notice that there can only be one setLayout(): at the end of the
36+
// constructor
37+
this->setLayout(vbox);
3638
}
3739

3840
// TestWidget class destructor is empty since
@@ -42,8 +44,8 @@ TestWidget::~TestWidget() noexcept {
4244

4345
// content modified by function will stay since it is passed the address
4446
void TestWidget::showHyperlink(QLabel* const label) {
45-
label->setText("<a href=\"www.qt-project.org/\">Get Started With Qt</a>");
46-
label->setTextFormat(Qt::RichText);
47-
label->setTextInteractionFlags(Qt::TextBrowserInteraction);
48-
label->setOpenExternalLinks(true);
47+
label->setText("<a href=\"www.qt-project.org/\">Get Started With Qt</a>");
48+
label->setTextFormat(Qt::RichText);
49+
label->setTextInteractionFlags(Qt::TextBrowserInteraction);
50+
label->setOpenExternalLinks(true);
4951
}

Qt/HelloGUI/TestWidget.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
// clang-format off
12
/*****************************************************************//**
23
* \file TestWidget.h
3-
* \brief
4+
* \brief Declaration of the TestWidget class.
45
*
56
* \author Xuhua Huang
67
* \date February 11, 2021
78
*********************************************************************/
9+
// clang-format on
810

911
#ifndef TESTWIDGET_H
1012
#define TESTWIDGET_H
@@ -14,23 +16,24 @@
1416
#include <QWidget>
1517

1618
class TestWidget : public QWidget {
17-
Q_OBJECT
19+
Q_OBJECT
1820

1921
public:
20-
// constructor includes a null pointer
21-
TestWidget(QWidget* parent = nullptr);
22+
// constructor includes a null pointer
23+
TestWidget(QWidget* parent = nullptr);
2224

23-
// destructor
24-
~TestWidget() noexcept;
25+
// destructor
26+
~TestWidget() noexcept;
2527

2628
private:
27-
// label
28-
QLabel* label;
29+
// label
30+
QLabel* label;
31+
QLabel* label2;
2932

30-
// layout manager
31-
QVBoxLayout* vbox;
33+
// layout manager
34+
QVBoxLayout* vbox;
3235

33-
void showHyperlink(QLabel* const label);
36+
void showHyperlink(QLabel* const label);
3437
};
3538

3639
#endif // TESTWIDGET_H

Qt/HelloGUI/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @file main.cpp
3+
* @author Xuhua Huang
4+
* @brief
5+
* @version 0.1
6+
* @date 2025-03-23
7+
*
8+
* @copyright Copyright (c) 2025
9+
*
10+
*/
11+
12+
#include <QApplication>
13+
#include <QMainWindow>
14+
15+
#include "TestWidget.h"
16+
17+
int main(int argc, char** argv) {
18+
QApplication app(argc, argv);
19+
20+
TestWidget window;
21+
window.setFixedSize(400, 300);
22+
window.setWindowTitle("Hello Qt GUI");
23+
window.show();
24+
25+
return app.exec();
26+
}

0 commit comments

Comments
 (0)