使用QT QPainter画一面彩色墙
  Y8XIq1u6ceQW 2023年11月02日 26 0

使用QT QPainter画一面彩色墙

效果

使用QT QPainter画一面彩色墙_QT

定义一个颜色表

static const QRgb colorTable[8] = {
    0xFFA500, 0xCC6666, 0x66CC66, 0x6666CC,
    0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00
};

定义砖块宽度高度

private:     
    Ui::RainbowWidget *ui;    
    enum { BrickWidth =80, BrickHeight = 40 };
    int col() { return contentsRect().width() / BrickWidth +1 ; }
    int row() { return contentsRect().height() / BrickHeight +1; }     
    void  drawBrick(QPainter &painter, int x, int y, int colorIndex);


画砖块形状

void RainbowWidget::drawBrick(QPainter &painter, int x, int y, int colorIndex)
{
    QColor color = colorTable[colorIndex];
    painter.fillRect(x + 1, y + 1,BrickWidth - 2, BrickHeight - 2,
                     color);
    painter.setPen(color.light());
    painter.drawLine(x, y + BrickHeight - 1, x, y);
    painter.drawLine(x, y, x +BrickWidth - 1, y);

    painter.setPen(color.dark());
    painter.drawLine(x + 1, y + BrickHeight - 1,
                     x + BrickWidth - 1, y + BrickHeight - 1);
    painter.drawLine(x + BrickWidth - 1, y + BrickHeight- 1,
                     x + BrickWidth - 1, y + 1);
}

使用图片加载为窗口背景

RainbowWidget::RainbowWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::RainbowWidget)
{
    ui->setupUi(this);
    QRect rect = contentsRect();
    QPixmap pixmap(rect.width() ,rect.height());
    QPainter painter(&pixmap); 
    QColor color=RGB(100,0,0);
    painter.fillRect(pixmap.rect(), color);
     //painter.fillRect(rect, color);
    for (int i = 0; i < row(); ++i) {
       // int spacing=(qrand() % 33 + 1) ;
        for (int j = 0; j < col(); ++j) {
            int colorIndex=qrand() % 7 + 1;
            if (i%2 !=0){
                drawBrick(painter,   j * BrickWidth, i * BrickHeight, colorIndex);
            }else{
                drawBrick(painter,   j * BrickWidth-(BrickWidth/2 ) , i * BrickHeight, colorIndex);
                // drawBrick(painter,   j * BrickWidth-50 , i * BrickHeight, shape1);
               // drawBrick(painter,   j * BrickWidth-spacing , i * BrickHeight, shape1);
            }
        }
    }

    //pixmap.save("wall.png");
    //背景
    QPalette palette = this->palette();
    palette.setBrush(QPalette::Window,QBrush(pixmap));
    this->setPalette(palette);
}

在这里我们可以学习到如何将图画到QPixmap上,并可以将QPixmap存为文件,这个加载背景的方式不像使用paintEvent会在窗口变化时动态刷新,应该会比paintEvent损耗资源少一些

知道在不使用paintEvent的情况下如何画图并QWidget上显示出来

在使用QT绘图QPainter对象时,发现QT在窗口QWidget画图大部分例子都是在paintEvent上进行的,如果要在别的地方画,在一个Label上画该如何做呢?

这先画到QPixmap,再以背景形式加载也是一个变通方式

QLabel *lb=ui->label;
lb->setPixmap(pixmap);

使用QT QPainter画一面彩色墙_QT_02

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  lA9hNJ8xYnRP   2023年12月12日   30   0   0 Qt
  lA9hNJ8xYnRP   2023年12月06日   32   0   0 Qt
  nIBmcPWZzwbL   2023年11月13日   33   0   0 Qt
  nIBmcPWZzwbL   2023年11月13日   46   0   0 Qt
  lA9hNJ8xYnRP   2023年12月06日   36   0   0 构造函数Qt
  lA9hNJ8xYnRP   2023年12月07日   34   0   0 Qt
  lA9hNJ8xYnRP   2023年12月11日   30   0   0 Qt
  lA9hNJ8xYnRP   2023年11月25日   38   0   0 Qt数据
  lA9hNJ8xYnRP   2023年11月30日   32   0   0 Qt表视图
  nIBmcPWZzwbL   2023年11月13日   33   0   0 Qt
  nIBmcPWZzwbL   2023年11月13日   37   0   0 Qt
  nIBmcPWZzwbL   2023年11月13日   49   0   0 Qt
Y8XIq1u6ceQW