QT5笔记:34. 视口和窗口

![image-20220504160327597](QT5 使用.assets/image-20220504160327597.png)
例子:

void Widget::paintEvent(QPaintEvent *event)
{
    int width = this->width();
    int height = this->height();

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    int sideLength = qMin(width, height);
    QRect rect((width - sideLength) / 2, (height - sideLength) / 2, sideLength, sideLength);
    painter.drawRect(rect);
    painter.drawText((width - sideLength) / 2, (height - sideLength) / 2, "这黑框是视口大小");
    painter.setViewport(rect);
    painter.setWindow(-100, -100, 200, 200);//窗口坐标,会改变原点的位置,并且映射绘制区域到设置的区域,大小会随着更改

    QLinearGradient linerGradient = QLinearGradient(-100, 0, 100, 100);//线性渐变色
    linerGradient.setColorAt(0, Qt::red);
    linerGradient.setColorAt(1, Qt::green);
    linerGradient.setSpread(QGradient::RepeatSpread);//设置颜色扩散方式
    painter.setBrush(linerGradient);
    painter.setCompositionMode(QPainter::RasterOp_NotSourceOrNotDestination);//重叠区域颜色的混合模式
    for (int i = 0; i < 36; i++) {
        painter.drawEllipse(0, 0, 50, 50);
        painter.rotate(10);
    }
}

界面:
image

posted @ 2023-03-16 13:46  echo_lovely  阅读(118)  评论(0编辑  收藏  举报