Evgenii Legotckoi
Sept. 17, 2015, 8:22 p.m.

Qt/C++ - Lesson 019. How to paint triangle in Qt5. Positioning shapes in QGraphicsScene

Drawing interfaces, the formation of the database tables, work with the network - it's all good, but sometimes you want to just draw something, such as a triangle. And then of course, revive this place, so that they can be controlled, and subsequently to turn this project into a little game. Well, who does not want to write your own game, even the most simple?

Let us then take the first step toward the unpretentious games, namely deal with drawing objects to Qt, trying to draw a triangle.

Project structure

  • Triangle.pro - Project profile, created by default, and in this project does not require the correction;
  • main.cpp - file, which starts with the application, the file is called a widget, which will be located in the graphic scene with a triangle;
  • widget.h - header file of a widget with a graphic scene;
  • widget.cpp - source file of widget;
  • triangle.h - Triangle header file class that inherits from QGraphicsItem;
  • triangle.cpp - source file of Triangle class;

mainwindow.ui

The interface design simply throws QGraphicsView in the widget. Nothing more is required.

widget.h

This file is only for defining a graphical scene and the triangle object with which we work.

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3.  
  4. #include <QWidget>
  5. #include <QGraphicsScene>
  6.  
  7. #include <triangle.h>
  8.  
  9. namespace Ui {
  10. class Widget;
  11. }
  12.  
  13. class Widget : public QWidget
  14. {
  15. Q_OBJECT
  16.  
  17. public:
  18. explicit Widget(QWidget *parent = 0);
  19. ~Widget();
  20.  
  21. private:
  22. Ui::Widget *ui;
  23. QGraphicsScene *scene;
  24. Triangle *triangle;
  25. };
  26.  
  27. #endif // WIDGET_H

widget.cpp

In this file are configured objects QGraphicsView, QGraphicsScene , and the triangle object is created and installed on the graphic scene.

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3.  
  4. Widget::Widget(QWidget *parent) :
  5. QWidget(parent),
  6. ui(new Ui::Widget)
  7. {
  8. ui->setupUi(this);
  9. this->resize(600,600); // Set sizes of widget
  10. this->setFixedSize(600,600); // Fix sizes of widget
  11. scene = new QGraphicsScene(); // Init graphic scene
  12. triangle = new Triangle(); // Init Triangle
  13.  
  14. ui->graphicsView->setScene(scene); // Set graphics scene into graphicsView
  15. ui->graphicsView->setRenderHint(QPainter::Antialiasing);
  16. ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  17. ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  18.  
  19. scene->setSceneRect(-250,-250,500,500);
  20.  
  21. scene->addLine(-250,0,250,0,QPen(Qt::black)); // Add horizontal line via center
  22. scene->addLine(0,-250,0,250,QPen(Qt::black)); // Add vertical line via center
  23.  
  24. scene->addItem(triangle);
  25. triangle->setPos(0,0);
  26.  
  27.  
  28. }
  29.  
  30. Widget::~Widget()
  31. {
  32. delete ui;
  33. }

triangle.h

But now is the time to work out the class itself, which creates a triangle. In this case, we are inheriting from QGraphicsItem .

  1. #ifndef TRIANGLE_H
  2. #define TRIANGLE_H
  3.  
  4. #include <QGraphicsItem>
  5. #include <QPainter>
  6.  
  7. // Наследуемся от QGraphicsItem
  8. class Triangle : public QGraphicsItem
  9. {
  10. public:
  11. Triangle();
  12. ~Triangle();
  13.  
  14. protected:
  15. QRectF boundingRect() const; /* We define a virtual method that returns the area in which the triangle is
  16. * */
  17. /* Define a method for rendering a triangle
  18. * */
  19. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  20. };
  21.  
  22. #endif // TRIANGLE_H

triangle.cpp

Now draw a triangle in our class. Here, there is one important moment. Coordinate System QGraphicsItem object - a concept different from the coordinate system of the graphic scenes. That is, each QGraphicsItem object or inherited from this class has its own coordinate system, which is translated in QGraphicsScene coordinate system. When we assign a position where there will be an object in the graphic scene, we indicate where it will be located point of the graphic object that has the coordinates 0 on the X-axis and 0 on the Y-axis in the coordinate system of the object, so it is important that this point was the center of the graphic. This will facilitate further work, unless of course you are not consciously intend to other options.

  1. #include "triangle.h"
  2.  
  3. Triangle::Triangle() :
  4. QGraphicsItem()
  5. {
  6.  
  7. }
  8.  
  9. Triangle::~Triangle()
  10. {
  11.  
  12. }
  13.  
  14. QRectF Triangle::boundingRect() const
  15. {
  16. return QRectF(-25,-40,50,80); // We are limiting the area of triangle
  17. }
  18.  
  19. void Triangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  20. {
  21. QPolygon polygon; // Using Polygon class, to draw the triangle
  22. // We put the coordinates of points in the polygonal model
  23. polygon << QPoint(0,-40) << QPoint(25,40) << QPoint(-25,40);
  24. painter->setBrush(Qt::red); // We set the brush, which will render the object
  25. painter->drawPolygon(polygon); // Draw a triangle on a polygonal model
  26. Q_UNUSED(option);
  27. Q_UNUSED(widget);
  28. }

Result

As a result, you should get an application that displays a red triangle in the center of the graphic scenes at the intersection of the two lines, as shown in the figure.

Download project

Video

J
  • June 8, 2023, 10:14 p.m.
  • (edited)

Евгений, здравствуйте!
Решил поэкспериментировать немного с кодом из этого урока, нарисовать вместо треугольника квадрат и разобраться с координатами. В итоге, запутался. И ни документация, ни доп.литература не помогла.
Проблема, собственно, в следующем. Когда я делаю графическую форму, компоную графические слои и виджеты, я получаю размеры.

Справа мы видим координаты graphicView

Дальше берём сам код:

  1. this->resize(10000,10000); // Задаем размеры виджета, то есть окна
  2. this->setFixedSize(460,380); // Фиксируем размеры виджета
  3.  

в первой строке мы задаём размеры виджета, но если их поменять, то сам его размер не изменяется, а изменения происходят, если поменять нижнюю строчку. Почему так?

Получается вот так:
1 -это сам виджет, 2 - это наш слой. И оба они имеют свои размеры как на предыдущей картинке.

Вот так она окно выглядит при изменении второй строчки

  1. scene->setSceneRect(-250,-250,700,700); // Устанавливаем область графической сцены
  2.  
  3. map->setPos(0,0); // Устанавливаем квадрат в центр сцены

Далее вот эти строчки:
Изменяя размеры в scene->setSceneRect(), визуально ничего не меняется. Почему?
Это и есть установка размеров области нашего QGraphicsView? Если да- то что за размеры остаются в ui-файле при этом?

Далее, я пытаюсь отрисовать квадрат в классе Map, но он не появляется на сцене, хотя когда я делал на координатах вашего урока, он был. Но как только я начал изменять их, он исчез. И тут я задумался собственно. Я хотел нарисовать квадрат ровно в левом углу сцены, которая имеет координаты (-250,-250), но квадрат так и не появлялся. Только когда я ставил координаты где-то ориенитирочно(-130,-130), то он вставал как раз в левый угол


Еще, я так и не понял что делает функция

  1. QRectF Map::boundingRect() const
  2. {
  3. return QRectF(0,0,60,60); // Ограничиваем область, в которой лежит прямоугольник
  4. }

Что именно она ограничивает? Думал, что за границы, которые определяет этот метод, нельзя "выйти", т.е, отрисовать квадрат "ЗА" установленными этим методом координатами, но это не так!
В итоге решил поиграться с координатами в графическом файлу ui и квадрат после этого совсем исчез.
Подскажите, что я делаю не так? В чём моя основная ошибка? Может где-то можно об этом почитать поподробнее? Я помню ваши слова, что координаты QGraphicsScene и QGraphicsItem разные, но я всё равно не понимаю как автоматически управлять этими вещами. Например, если я хочу нарисовать сцену и отрисовать на ней квадраты с помощью цикла (к примеру, как в игре "Морской бой"). Мы же должна хорошо понимать и знать координаты сцены и самого объекта отрисовки. Особенно, координаты углов самого объекта.
Заранее благодарю за ответ, Евгений!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html