Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to remove the intersection of a circle and a square?



  • How to remove the intersection of a circle and a square? I want to do as in the picture

    Screenshot_20211013_180534.png

    but I can only do this
    Screenshot_20211013_180459.png

    here is the code :

    void Tank::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this); // Создаём объект отрисовщика
        painter.setRenderHint(QPainter::Antialiasing); // убираем резкие кубики
        QPen pen_osn(Qt::black, 2, Qt::SolidLine, Qt::FlatCap); // кисть  для основного тела
        QPen pen_bashn(Qt::black, 4, Qt::SolidLine, Qt::FlatCap);  // кисть для башни
    
    //    painter.translate(this->width()/2, this->height()/2); // смещение отрисовки
        razmer_tank(this->width(), this->height()); // выставляем размеры
    
        painter.setPen(pen_osn);     // Устанавливаем кисть для линий и текста
    
    //    painter.drawLine(0,0,this->width(), this->height());
    
        painter.drawLine(_myTexpr.m_t_razm[0],  _myTexpr.m_t_razm[1],
                _myTexpr.m_t_razm[0], _myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5]);
    
        painter.drawLine(_myTexpr.m_t_razm[0],  _myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5],
                _myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4],_myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5]);
    
        painter.drawLine(_myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4], _myTexpr.m_t_razm[1],
                _myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4], _myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5]);
    
    
        painter.drawLine(_myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4],         _myTexpr.m_t_razm[1],
                _myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4]/2,     _myTexpr.m_t_razm[1]+ _myTexpr.m_t_razm[1]*cos(8));
    
        painter.drawLine(_myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4]/2.0,    _myTexpr.m_t_razm[1]+ _myTexpr.m_t_razm[1]*cos(8),
                _myTexpr.m_t_razm[0],       _myTexpr.m_t_razm[1]);
    
        painter.setPen(pen_bashn);     // Устанавливаем кисть для пушек и обвесов
    
    //    painter.drawEllipse(_myTexpr.m_t_razm[0]/*+_myTexpr.m_t_razm[4]/2*/,    _myTexpr.m_t_razm[1]/*+_myTexpr.m_t_razm[5]/2.0*/,
    //            _myTexpr.m_t_razm[6],    _myTexpr.m_t_razm[6]);
        painter.drawEllipse(QPoint(_myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4]/2.0,   _myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5]/3.2),
                _myTexpr.m_t_razm[6]/2.5,    _myTexpr.m_t_razm[6]/2.5);
    
    
        painter.drawLine(_myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4]/2,
                _myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5]/3.2-_myTexpr.m_t_razm[6]/2.5,
                _myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4]/2,
                _myTexpr.m_t_razm[1]-_myTexpr.m_t_razm[1]*0.2);
        // перемещаем позицию отрисовки а также проворачиваем (угол в другой системме кординат)
        // для отрисовки "подвесов"
         painter.translate(_myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4]/2+(_myTexpr.m_t_razm[7]*1.3),
                _myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5]/3.2-_myTexpr.m_t_razm[6]/2.5);
    
        painter.rotate(45.0);
        painter.drawRect(0,0,_myTexpr.m_t_razm[7],  _myTexpr.m_t_razm[7]);
        painter.rotate(-45.0);
        // возвращаем позицию отрисовки (мировых координат)
        painter.translate(-(_myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4]/2+(_myTexpr.m_t_razm[7]*1.3)),
                  -(_myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5]/3.2-_myTexpr.m_t_razm[6]/2.5));
    
        painter.translate(_myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4]/2-(_myTexpr.m_t_razm[7]*1.3),
               _myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5]/3.2-_myTexpr.m_t_razm[6]/2.5);
    
        painter.rotate(45.0);
        painter.drawRect(0,0,_myTexpr.m_t_razm[7],  _myTexpr.m_t_razm[7]);
        painter.rotate(-45.0);
    
        // возвращаем позицию отрисовки (мировых координат)
        painter.translate(-(_myTexpr.m_t_razm[0]+_myTexpr.m_t_razm[4]/2-(_myTexpr.m_t_razm[7]*1.3)),
                  -(_myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5]/3.2-_myTexpr.m_t_razm[6]/2.5));
    
        QString ctroka;                                                 // строка для вывода цифр
        QFont razm_shrift("Areal", _myTexpr.m_t_razm[9], QFont::Light); // свойства шрифта
        painter.setFont(razm_shrift);                                   // устанавливаем размер шрифта
        QFontMetrics stroka_razm = painter.fontMetrics();               // метрика шрифта
        // Вот тут текст
        ctroka = "RPM \n 50";
    
    
        painter.drawText(_myTexpr.m_t_razm[0]*0.96,
                _myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5]/3.8,
                _myTexpr.m_t_razm[7]*2.0,_myTexpr.m_t_razm[7]*2,0,ctroka,nullptr);
    
        painter.drawText(_myTexpr.m_t_razm[0]*1.12,
                _myTexpr.m_t_razm[1]+_myTexpr.m_t_razm[5]/3.8,
                _myTexpr.m_t_razm[7]*2.0,_myTexpr.m_t_razm[7]*2,0,ctroka,nullptr);
    
    }
    


  • @timob256 said in How to remove the intersection of a circle and a square?:

    How to remove the intersection of a circle and a square?

    Instead of drawing the square / rectangle line by line, you could draw a QRect and fill it with black color (then it might look like the left one)

    Or clear the area and repaint the rectangles. Then you need the exact coordinates of your squares to repaint this exact area.

    Edit:

    @timob256 said in How to remove the intersection of a circle and a square?:

    painter.drawRect(0,0,_myTexpr.m_t_razm[7], _myTexpr.m_t_razm[7]);

    Ah sorry did not see that...

    Are these your 2 squares? Then you know the coordinates and you can repaint this area. But you need to fill it with some QBrush, since the inside of the rect does not have any "color" when drawing just the outter lines, so the part of the circle will be visible.

    Another possible way would be to draw an arc instead of a full circle and stop the arc right before the squares begin.
    Like here:



  • painter.fillRect( squre_rect, background color or black ); /*you may pay attention to the order of drawing */

    Another way to do it is to use graphics items for different shapes and texts in your app. You can set display order for them.


Log in to reply