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

Widget crops while rotating about its center



  • Hi All,
    I am facing a problem where I am trying to rotate a square about its center but as I keep on increasing oreintation, square getting cropped. Below is my code for reference:

    paintEvent(QPaintEvent * )
    {
    QPainter painter(this);
    //this->resize(m_nRectWidth+230 ,m_nRectHeight+30);
    QPen pen;

    pen = QPen(m_qcGateColor,1);
    painter.setPen(pen);
    QPainterPath m_RectPath = QPainterPath();
    QPainterPath m_SelectedPath = QPainterPath();
    m_RectPath.addRect(0,0,getRectWidth(),getRectHeight());
    m_SelectedPath.addRect(0,0, getRectWidth()-1, getRectHeight()-1);
    painter.translate(getRectWidth()/2, getRectHeight()/2);
    painter.rotate(m_Orientation);
    painter.translate(-getRectWidth()/2, -getRectHeight()/2);
    painter.setBrush(QBrush(m_qcGateColor));
    painter.drawPath(m_RectPath);
    
    if(IsSelected())
    {
        painter.setBrush(Qt::transparent);
        painter.setPen(QPen(Qt::green,3));
        painter.drawPath(m_SelectedPath);
    }
    pen = QPen(Qt::black,1);
    painter.setPen(pen);
    
    QRect rect = m_SelectedPath.boundingRect().toRect();
    QRegion Mask(rect);
    setMask(Mask);
    

    }
    Please suggest.

    Thanks
    Nitin


  • Lifetime Qt Champion

    Hi,

    What version of Qt ?
    On what platform ?
    What is the size of your widget ?
    What does getRectWidth return ?
    What does getRectHeight return ?
    Can you provide the complete code of your widget ?



  • HI SGaist,
    Thanks for the response.
    Qt version: 4.8.2
    Platform Windows 10
    default size 30*30 ( Its a small square)
    getRectWidth returns width of the square
    getRectHeight returns height of the square
    Image added for your reference.

    Please let me know if you need anything else.

    Without orientation.PNG with_Orientation.PNG

    Thanks
    Nitin


  • Lifetime Qt Champion

    The complete widget code would be nice to recreate the situation locally.


Log in to reply