Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
This simple 2D graphics program has the goal to create a colored gradient on a QMainWindow/QDialog by setting the colors of individual pixels.
This guide is not yet finished: I haven't gotten it to work yet. My unsuccessful trials are at the bottom of this page.
The screenshots from this guide are made using Qt Creator 1.3.1 running under Ubuntu 10.04.
|
To create simple 2D graphics program in Qt Creator, you can follow the steps below:
- Step #1: Setting up the Qt4 Gui application project
- Step #2: Coding the declaration of paintEvent
- Step #3: Coding the definition of paintEvent
- Step #4: Running the program
Step #1: Setting up the Qt4 Gui application project
- Start Qt Creator.
- If you are not shown the welcome screen, click on 'Welcome' on the left
- If, in the welcome screen, the 'Develop' tab is not shown, click on 'Develop' to view the Welcome screen Develop tab
- In the Welcome screen Develop tab, click 'Create New Project' to go to the New project dialog
- In the New project dialog, click 'QT4 Gui Application' to go to the introduction and project location dialog
- In the Introduction and project location dialog, you must specify a name and location to save your (project) files. For example, after 'Name' type 'CppQt2dGraphics'. Click next
- In the select required modules dialog, make sure QtCore and QtGui are checked and click 'Next'
- In the class Information dialog, click 'Next'
- In the project management dialog, click 'Finish'
Now your Qt4 Gui application project is successfully set up. Your screen might look like this.
Step #2: Coding the declaration of paintEvent
- On the left menu bar, you can see you are now in 'Edit' mode (instead of 'Welcome' mode). You can see your project right of this menu bar. You can see a 'CppQt2dGraphics' project folder, containing the following folders and files:
- CppQt2dGraphics.pro
- main.cpp (in folder 'Sources')
- mainwindow.cpp (in folder 'Sources')
- mainwindow.h (in folder 'Headers')
- mainwindow.ui (in folder 'Forms')
Double-click on 'mainwindow.h' to be taken to the code editor.
- Your screen then might look like this with the following source code:
- Change this code to the following:
Note that if you try to execute the program, you get the following (link) error:
moc_mainwindow.o:(.rodata._ZTV10MainWindow[vtable for MainWindow]+0x7c): undefined reference to 'MainWindow::paintEvent(QPaintEvent*)'
|
This (link) error is correct: the member function MainWindow::paintEvent is declared but not yet defined.
Step #3: Coding the definition of paintEvent
- Double-click on 'mainwindow.cpp to be taken to the source of mainwindow.cpp:
- Change this code to the following:
Step #4: Running the program
- Press CTRL-R to start the program
Unsuccessful trials
Below I show my unsuccessfull attempts that might have worked, but do not.
Simply start drawing
The attempt below does not work. It draws only a single randomly-drawn line, like this.
Draw and update the QMainWindow
The attempt below does not work. It draws a single randomly-drawn line and then updates the QMainWindow. What happens is that lines are indeed drawn at random positions, but are not stored. This results in viewing only three lines at the same time, like this.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.
