Skip to content

Commit 3a1533f

Browse files
committed
A few more QT edits
1 parent cb6b19d commit 3a1533f

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

13-Graphical-User-Interfaces-with-Qt.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ You would scream, but you have no menu entry for that.
5353
### Takeaways
5454

5555
- Experiment with Qt to build Graphical User Interfaces
56+
- Get a taste of event-driven programming
5657
- Learn a bit about how large libraries and projects are organized
5758
- Appreciate the simplicity of programming Command Line Interface applications.
5859

@@ -72,9 +73,9 @@ It's just a little less.
7273
Qt's preprocessor is called the Meta Object Compiler (`moc`).
7374
Fortunately, you don't have to work with it (or `g++`) directly, since Qt can generate a `Makefile` for you!
7475

75-
So...you don't need to run `moc`, `g++`, or even make your own `Makefile`... so what *do* you have to do?
76+
So...you don't need to run `moc`, `g++`, or even make your own `Makefile`...so what *do* you have to do?
7677

77-
Qt uses project files, which end in `.pro` to determine how to build your projects.
78+
Qt uses project files, which end in `.pro`, to determine how to build your projects.
7879
You can generate a new `.pro` file for a Qt project by running the command `qmake -project`.
7980

8081
Qt is a big library, so to speed up compile times not everything gets added in all at first.
@@ -87,7 +88,7 @@ QT += widgets
8788
That tells `qmake` to include the widgets library files when it generates the makefile.
8889
Once we've added this line, we can just `#include <QtWidgets>` and use all the widgets Qt provides to our hearts' content.
8990

90-
If you already have a `.pro` file, then you can just run `qmake` to generate a `Makefile`.
91+
Once you have a `.pro` file, you can run `qmake` to generate a `Makefile`.
9192
Then you can run `make` to compile everything together!
9293

9394
~~~shell
@@ -100,7 +101,7 @@ $ echo "QT += widgets" >> project_name.pro
100101
# Run this once to create your Makefile
101102
$ qmake
102103

103-
# ... then this whenever you want to recompile
104+
# ...then this whenever you want to recompile
104105
$ make
105106
~~~
106107

@@ -114,7 +115,7 @@ Although you instantiate the `QApplication` in `main()`, you can access it throu
114115

115116
So that's nice, right?
116117
A `QApplication` is an object that represents your entire application.
117-
Not the windows, no buttons... it's the *whole* thing.
118+
Not the windows, no buttons...it's the *whole* thing.
118119

119120
All of those clickable things that we all love to click: those are called **widgets**.
120121
If you want a useful application, you can't work with just a `QApplication`.
@@ -153,7 +154,7 @@ We then ask Qt to display our text editor window using the `.show()` member func
153154
So we've got an **application** set up with a text editing **widget**, and we've asked Qt to show it.
154155
In order to see our application in action, we need to ask it to run using `app.exec()`.
155156
156-
![Our first app! Boy howdy. That sure ain't vim.](13/not_vim.png){width=80%}
157+
![Our first app! Boy howdy. That sure ain't vim.](13/not_vim.png){width=45%}
157158
158159
### Laying out your App
159160
@@ -169,7 +170,7 @@ To position a bunch of widgets on screen, we use a **layout** widget.
169170
For example, let's say we want to put a quit button above our text editor (in the same window of course).
170171
We can use a `QVBoxLayout` to **vertically** (hence the `V`) stack our widgets.
171172
172-
![We can organize the widgets in our application by creating a vertical stack. First we add the button (to the top) then we add the text editor (beneath the button).](13/layout.png)
173+
![We can organize the widgets in our application by creating a vertical stack. First we add the button (to the top) then we add the text editor (beneath the button).](13/layout.png){width=45%}
173174
174175
~~~{.cpp .numberLines}
175176
// #includes left out for the sake of brevity
@@ -205,15 +206,15 @@ A couple of odd things to note:
205206

206207
1. On line 7, the `&` tells Qt to set up a keyboard shortcut, \keys{\Alt + q}, that 'presses' the button.[^QShortcut]
207208
2. You may notice that on lines 6, 7, and 9, we allocate memory with `new` but never call `delete`.
208-
Unlike normal C++ objects, Qt objects are written so that they clean up their children when they are destructed.
209+
Unlike typical C++ objects, Qt objects are written so that they clean up their children when they are destructed.
209210
In this case, our `QTextEdit` and `QPushButton` are added as children to our `QVBoxLayout` object,
210211
and that layout object is added as a child to the `QWidget` created on line 13.
211212
This means that as long as we clean up that `QWidget`, all our other objects will get cleaned up automatically!
212213
(And, since that `QWidget` is a stack-allocated variable, it gets destructed whenever `main()` returns.)[^ObjectTrees]
213214

214215
The rest is similar to the last example.
215216
With layouts, we have the ability to specify how we want our widgets organized on screen.
216-
In addition to vertical layouts there are horizontal layouts (`QBoxLayout`) and grid layouts (`QGridLayout`) and a handful of others.
217+
In addition to vertical layouts there are horizontal layouts (`QBoxLayout`), grid layouts (`QGridLayout`), and a handful of others.
217218

218219
So, that's dandy...but our quit button doesn't actually do anything!
219220
To make our buttons work, we need to talk about Signals and Slots.
@@ -329,10 +330,10 @@ The `QMainWindow` has one big ol' widget that goes in the middle of the window
329330
`setCentralWidget()` is a member function of `QMainWindow` that sets this widget.
330331

331332
To create your menus (File, Edit, whatever you want) you need to add them to your `QMainWindow`.
332-
`menuBar()` is a member function that returns a pointer to the menubar, which you can use to add new menus.
333-
Similarly, `addToolBar()` is a member function that creates a new toolbars.
333+
It has a `menuBar()` member function that returns a pointer to the menubar, which you can use to add new menus.
334+
Similarly, there is an `addToolBar()` member function that creates a new toolbars.
334335

335-
Since toolbar buttons and menu items do the same thing (fire an event when clicked), Qt represens them both as `QActions`.
336+
Since toolbar buttons and menu items do the same thing (fire an event when clicked), Qt represents them both as `QActions`.
336337
You can add the same `QAction` to both a menu and a toolbar --- so there's less code repetition as well!
337338
`QAction`s have a `triggered()` signal that is emitted whenever their menu item or button is clicked.
338339

@@ -557,7 +558,7 @@ We define our signal in the `Notepad` class definition in `notepad.h`:
557558
void useFile(QString fileName);
558559
~~~
559560
560-
This tells Qt that the QButton is capable of hootin' and hollerin' about being clicked.
561+
This tells Qt that `Notepad` is capable of hootin' and hollerin' about files.
561562
`signals` is another keyword understood by moc.
562563
Unlike with slots, you do **not** implement the signal function --- you just have to declare it.
563564

0 commit comments

Comments
 (0)