📜 ⬆️ ⬇️

Drawing in QT

QT

Introduction

QT is a technology for developing cross-platform applications implemented in C ++. Nokia has released an excellent technology and an excellent IDE to it, which, in my opinion, is a successful combination of ease of writing programs (all you have to do is know OOP and C ++) and simultaneous “controllability” (all libraries are available to view them). The result is an optimized application, which is connected, only what is needed. Of course, there are not some ready-made implementations in it, for example, events of receiving focus, but there is everything to implement it.

When studying QT, you can run into the global problem of implementing graphics. And if you try to search in Google, then you can find a forum in which the gurus sit, and “send” to the same Google, but of course there are exceptions. Max Schlee writes in his wonderful book about this, but it looks more like a nervous sketches. QT Assistant is a great thing, but there is no ready-made example to just figure it out. In general, I figured out that I used all three sources and knowledge of the PLO. Of course, you can say that if you want, you can easily figure it out, but I want the person to go to Google, type in “Drawing in QT” and go to an article in which everything will be described and described using an example.

Ideology

In QT, not like in .NET (connect the library for one class and at the same time a bunch of others as a gift and get a machine to absorb RAM). The ideology in QT is as follows: you must create a class that inherits public from QWidget. We will get the class of the object on which we will draw. You can create a class in a separate header file, but I created it mostly in “wigdet.h”:
class canvas : public QWidget {
Q_OBJECT
public:
canvas(QWidget* parent=0):QWidget(parent){}
~canvas(){}
void paintEvent(QPaintEvent*); // , ,
bool par; /* , . - , - . */

protected:
};

')
Creating an object for drawing and programming a method for drawing

Next, after you have created the class, you can proceed to the declaration of the object and the method call. All this can be done where you have events. I have this “widget.cpp”. The declaration can be carried out by some event or by downloading the program. She looks as follows
canvas* wt = new canvas; // canvas* wt = new canvas(ui->tab);
wt=new canvas(ui->tab); //ui->tab_5 ,
wt->show(); //
wt->setGeometry(0,210,500,150); //


After creating the object, we need to program the drawing itself, that is, the void paintEvent method (QPaintEvent *). At its core, it is an event, even the name says so. Therefore, it is programmed separately as any event. It is advisable to program it also in the main event file, since it is convenient to access GUI objects. Everything is simple:
void canvas::paintEvent(QPaintEvent *)
{
QPainter img(this);
img.drawLine(0,0,150,75); /* drawLine(0,0,150,75) , QPainter. */
}

Source: https://habr.com/ru/post/115381/


All Articles