Drawing Rectangle with only 2 corners rounded in Qt

前端 未结 3 1791
谎友^
谎友^ 2021-01-12 11:46

I am working on an application where I need to fill the color for the Pixmap using Painter. Pixmap is of type rectangle with (bottom edge) 2 rounded corners. Top 2 corners a

相关标签:
3条回答
  • 2021-01-12 12:07

    To extend the answer of Romha Korev. Here an example of a box with only rounded top corners (top left, top right). The rectangles in the corners are calculated based on the main rectangle!

    qreal left = 5;
    qreal top = 10;
    qreal width = 100;
    qreal height = 20;
    QRectF rect(left, top, width, height);
    
    QPainterPath path;
    path.setFillRule( Qt::WindingFill );
    path.addRoundedRect(rect, 5, 5 );
    qreal squareSize = height/2;
    path.addRect( QRect( left, top+height-squareSize, squareSize, squareSize) ); // Bottom left
    path.addRect( QRect( (left+width)-squareSize, top+height-squareSize, squareSize, squareSize) ); // Bottom right
    painter->drawPath( path.simplified() ); // Draw box (only rounded at top)
    
    0 讨论(0)
  • 2021-01-12 12:09

    You can use QPainterPath for that :

        QPainterPath path;
        path.setFillRule( Qt::WindingFill );
        path.addRoundedRect( QRect(50,50, 200, 100), 20, 20 );
        path.addRect( QRect( 200, 50, 50, 50 ) ); // Top right corner not rounded
        path.addRect( QRect( 50, 100, 50, 50 ) ); // Bottom left corner not rounded
        painter.drawPath( path.simplified() ); // Only Top left & bottom right corner rounded
    
    0 讨论(0)
  • 2021-01-12 12:14

    You can use stylesheets (on runtime or loading the file qss). You could manage to do it very easily:

    QString str = "bottom-right-radius: 10px; top-right-radius: 0px....";
    box->setStylesheet(str);
    

    I suppose the box is a pixmap inside a QLabel ( label->setPixmap(...) )

    OR

    Set the object name to something (the label), and then use the

    QLabel#name { bottom-right-radius: 10px... }

    In a stylesheet you load.

    Check this site out. It helps: http://border-radius.com/

    0 讨论(0)
提交回复
热议问题