(C++) How to draw on a QBitmap with using the GUI designer?
February 24, 2017 · View on GitHub
(C++)
How to draw on a QBitmap with using the GUI designer?
This page describes how to draw on a QBitmap with using the GUI designer, resulting in this screenshot. You can also directly download the CppQtBitmapDrawGui source and binary code.
Probably, you do not want to use a QBitmap, as it can only display monochrome (that is black and white) color!
To create this program in Qt Creator, you can follow the steps below:
- Step #1: Setting up the Qt4 Gui application project
- Step #2: Setting up the GUI
- Step #3: Coding the declarations of the dialog
- Step #4: Coding the definitions of the dialog
- Step #5: 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 'HelloWorldGui'. Click next
- In the select required modules dialog, all checks are correct (only QtCore and QtGui are checked), so click 'Next'
- In the class Information dialog, change the 'Base class' to 'QDialog' and 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: Setting up the GUI
-
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 'QtHelloWorldGui' project folder, containing the following files:
- QtHelloWorldGui.pro
- main.cpp
- dialog.cpp
- dialog.h
- dialog.ui
Double-click on 'dialog.ui' to be taken to the GUI designer.
-
Your screen might then look like this. I prefer to press 'Alt-0' to remove the Project manager sidebar and my screen looks like this.
-
From the Widget toolbox, place a PushButton (under 'Buttons') and Label (under 'Display Widgets') on the MainWindow.
-
Click on the PushButton and change the Text property to 'Invert colors', like this screenshot.
-
Click on the Label and change the Bitmap property to 'Choose file' and select an image like 256x256.png, like this screenshot.
-
Right-click on the Dialog and select 'Layout -> Lay out vertically'.
-
Right-click on the Dialog and select 'Size contraints -> Set minimum size'.
You have now successfully set up the GUI. Your screen might then look like this.
Step #3: Coding the declarations of the dialog
-
Press 'ALT-0' to view the Project Manager sidebar again. Your screen might then look like this.
-
Double-click on 'dialog.h to be taken to the source of dialog.h:
#ifndef DIALOG_H #define DIALOG_H namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); protected: void changeEvent(QEvent *e); private: Ui::Dialog *ui; }; #endif // DIALOG_H -
Change this code to the following:
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QPainter> #include <QBitmap> #include <QTimer> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); protected: void changeEvent(QEvent *e); private slots: void OnClick(); void OnTick(); private: Ui::Dialog *ui; QBitmap m_bitmap; QPainter m_painter; int m_z; QTimer m_timer; }; #endif // DIALOG_H
Note that if you try to execute the program, you get the following (link) error:
/MyFolder/moc_mainwindow.cpp:66: undefined reference to 'Dialog::OnClick()' /MyFolder/moc_mainwindow.cpp:66: undefined reference to 'Dialog::OnTick()'
This (link) error is correct: the member functions Dialog::OnClick and Dialog::OnTick are declared but not yet defined.
Step #4: Coding the definitions of the dialog
-
Press 'ALT-0' to view the Project Manager sidebar again. Your screen might then look like this.
-
Double-click on 'dialog.cpp to be taken to the source of dialog.cpp:
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; } void Dialog::changeEvent(QEvent *e) { QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } -
Change this code to the following:
#include <QPainter> #include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog), m_bitmap("256x256.png"), m_painter(&m_bitmap), m_z(0) { ui->setupUi(this); assert(QFile::exists("256x256.png") && "Please put a file called 256x256.png in the executable its folder"); QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(OnClick())); QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(OnTick())); OnTick(); m_timer.start(10); } Dialog::~Dialog() { delete ui; } void Dialog::changeEvent(QEvent *e) { QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } void Dialog::OnTick() { ++m_z; for (int x = 0; x!=256; ++x) { for (int y = 0; y!=256; ++y) { m_painter.setPen(QColor((m_z+x)%256,(m_z+y)%256,(m_z+x+y)%256)); m_painter.drawPoint(x,y); } } ui->label->setPixmap(m_bitmap); } void Dialog::OnClick() { m_z+=128; }
Step #5: Running the program
- Press CTRL-R to start the program
- Your screen might then look like this