1. 程式人生 > >【Qt】QOpenGLWidget展示蒙版效果

【Qt】QOpenGLWidget展示蒙版效果

大小 literal ins opengl near ops rect font ==

關鍵代碼是派生QOpenGLWidget,覆寫paintEvent函數

QPainter p;
    p.begin(this);
    p.drawImage(QPoint(0, 0), m_Img);

    QLinearGradient grad(0, 0, rect().width(), rect().height());
           {
                   QGradientStops gs;
                   gs << QGradientStop(0.0, QColor(0,0,0,100))
                           
<< QGradientStop(0.5, QColor(0,0,0,100)) << QGradientStop(1.0, QColor(0,0,0,100)); grad.setStops(gs); } //定義頂部蒙版高度 int m_topHeight = 20; //定義雙側蒙版寬度 int m_sideWidth = 50; //定義底部蒙版高度 int m_bottomHeight = 100; //線條長度 int
iLineLen = 20; //底部文字框的寬高 int iTxtHeight = 50; int iTxtWidth = 150; //填充周圍蒙版 //頂部 p.fillRect(0, 0, rect().width(), m_topHeight, grad); //底部 p.fillRect(0, rect().height() - m_bottomHeight, rect().width(), m_bottomHeight, grad); //左側 p.fillRect(0, m_topHeight, m_sideWidth, rect().height() - m_topHeight - m_bottomHeight, grad);
//右側 p.fillRect(rect().width() - m_sideWidth, m_topHeight, m_sideWidth, rect().height() - m_topHeight - m_bottomHeight, grad); QPen pen; pen.setStyle(Qt::DashDotLine); pen.setWidth(1); if(m_status == 0) { pen.setBrush(Qt::green); } else if(m_status == 1) { pen.setBrush(Qt::yellow); } else if(m_status == 2) { pen.setBrush(Qt::red); } pen.setCapStyle(Qt::RoundCap); pen.setJoinStyle(Qt::RoundJoin); p.setPen(pen); //繪制中間高亮區域矩形 //矩形寬高 int iWith =rect().width()-m_sideWidth*2; int iHeight = rect().height() -m_topHeight - m_bottomHeight; p.drawRect(m_sideWidth, m_topHeight, iWith,iHeight); QPen pen2; pen2.setWidth(5); if(m_status == 0) { pen2.setBrush(Qt::green); } else if(m_status == 1) { pen2.setBrush(Qt::yellow); } else if(m_status == 2) { pen2.setBrush(Qt::red); } p.setPen(pen2); //畫四角的線條 //左上橫線 p.drawLine(m_sideWidth,m_topHeight,m_sideWidth +iLineLen, m_topHeight); //左上豎線 p.drawLine(m_sideWidth,m_topHeight+iLineLen,m_sideWidth,m_topHeight); //右上橫線 p.drawLine(rect().width() - m_sideWidth - iLineLen,m_topHeight, rect().width() - m_sideWidth,m_topHeight); //右上豎線 p.drawLine(rect().width() - m_sideWidth,m_topHeight, rect().width() - m_sideWidth, m_topHeight + iLineLen); //右下豎線 p.drawLine(rect().width() - m_sideWidth,rect().height()-m_bottomHeight,rect().width() - m_sideWidth,rect().height()-m_bottomHeight - iLineLen); //右下橫線 p.drawLine(rect().width() - m_sideWidth,rect().height()-m_bottomHeight, rect().width() - m_sideWidth - iLineLen,rect().height()-m_bottomHeight); //左下橫線 p.drawLine(m_sideWidth,rect().height()-m_bottomHeight,m_sideWidth+iLineLen, rect().height()-m_bottomHeight); //左下豎線 p.drawLine(m_sideWidth,rect().height()-m_bottomHeight, m_sideWidth,rect().height()-m_bottomHeight -iLineLen); //繪制底部提示消息 QString strMsg = ""; if(m_status == 0) { strMsg=QStringLiteral("請通行"); } else if(m_status == 1) { strMsg=QStringLiteral("請刷臉"); } else if(m_status == 2) { strMsg=QStringLiteral("請對準紅框"); } p.setPen(pen); p.drawRect(rect().width()/2 - iTxtWidth/2, rect().height()-m_bottomHeight + (m_bottomHeight/2-iTxtHeight/2), iTxtWidth, iTxtHeight ); QRect txtRect; txtRect.setX(rect().width()/2 - iTxtWidth/2); txtRect.setY(rect().height()-m_bottomHeight + (m_bottomHeight/2-iTxtHeight/2)); txtRect.setWidth(iTxtWidth); txtRect.setHeight(iTxtHeight); p.setPen(pen2); QFont font; font.setFamily("Microsoft YaHei"); // 大小 font.setPointSize(20); // 使用字體 p.setFont(font); p.drawText(txtRect, Qt::AlignCenter , strMsg); p.end();

使用QMoive播放Gif的代碼

 m_movie =new QMovie("F:/TestProject/QMoveTest/timg.gif");
    m_timer =new QTimer(this);
 ui->lblMove->setVisible(true);

        ui->lblMove->setMovie(m_movie);

        m_movie->start();

        //m_timer->start(3000);
        QTimer::singleShot(5000, this, SLOT(StopMovie()));
void MainWindow::StopMovie()
{
    m_movie->stop();
    ui->lblMove->setVisible(false);
}

最終效果:

技術分享圖片

技術分享圖片+

技術分享圖片

技術分享圖片

【Qt】QOpenGLWidget展示蒙版效果