QT在页面使用QLabel显示图片

新建一个label控件:label_Test,函数完成把图片填充到label中,并且改变背景颜色,显示提示字体函数

	/*控件显示图片
    path:   需要显示的图片地址
    content:    显示的内容
    result:     测试状态,改变控件颜色
    strSize:    设置显示内容的字符串大小
    */
    void pictureDisplay(QString path,  QString content, QString result, int strSize = 45);

    /*功能:不显示图片,仅在控件上显示文字和状态颜色
    content:    需要显示的字符串
    result:     状态,改变控件颜色
    */
    bool statusDisplay(QString content, QString result);

    /*功能:初始化之前的显示
    * 
    */
    void initStationDisplay();

对应的.cpp文件


void pictureDisplay(QString path, QString content, QString result, int strSize)
{
    initStationDisplay();
    if (path == "")
        path = ":image/black.png";			//可改成其他的图片

 	resize_width = ui->label_Test->width();
    resize_heigh = ui->label_Test->height();
    
    QPixmap pixmap(path);
    QPixmap fitpixmap = pixmap.scaled(resize_width, resize_heigh, Qt::KeepAspectRatio, Qt::SmoothTransformation);  // 按比例缩放

    QPainter painter1;
    painter1.begin(&fitpixmap);
    QFont font;
    font.setPixelSize(strSize);
    painter1.setFont(font);
    painter1.setPen(Qt::yellow);
    QLabel* labelTemp = new QLabel();
    //输入图片显示的控件
    labelTemp = ui->label_Test;
    //根据传入的result 改变控件的背景颜色
    if (result == "PASS")
    {
        painter1.setPen(Qt::green);
        labelTemp->setStyleSheet("QLabel{border:3px solid rgb(0,255,0);}");
    }
    else if (result == "FAIL")
    {
        painter1.setPen(Qt::red);
        labelTemp->setStyleSheet("QLabel{border:3px solid rgb(255,0,0);}");
    }
    else if (result == "Testing")
    {
        painter1.setPen(QColor(239, 137, 3));
        labelTemp->setStyleSheet("QLabel{border:3px solid rgb(239, 137, 3);}");
    }
    else
    {
        painter1.setPen(QColor(239, 137, 3));
        labelTemp->setStyleSheet("QLabel{border:3px solid rgb(0,0,0);}");
    }
    painter1.setFont(QFont("Microsoft YaHei", 16,50));
    painter1.drawText(220, 150, 1000, strSize, Qt::AlignBottom, content);
    //painter1.drawText(resize_heigh * 5 / 12, resize_width * 5 / 12,content);
    painter1.end(); 
    labelTemp->setPixmap(fitpixmap);
    labelTemp->setScaledContents(true);
}

bool statusDisplay(QString content, QString result)
{
    initStationDisplay();
    QPalette pa;
    if (result == "PASS")
        pa.setColor(QPalette::Background, QColor(0, 255, 0));
    else if (result == "FAIL")
        pa.setColor(QPalette::Background, QColor(255, 0, 0));
    else if (result == "Testing")
    {
        pa.setColor(QPalette::Background, QColor(239, 137, 3));
    }
    else
        pa.setColor(QPalette::Background, QColor(0, 0, 0));
    
     ui->label_Test->setStyleSheet("color: rgb(0, 86, 184);font: 75 20pt '微软雅黑';");
     ui->label_Test->setText(content);
     ui->label_Test->setAutoFillBackground(true);
     ui->label_Test->setPalette(pa);
     
    return true;
}

void initStationDisplay()
{
   ui->label_Test->clear();
   ui->label_Test->setStyleSheet("color: rgb(0, 86, 184);font: 75 20pt '微软雅黑';");

}

显示图片的pictureDisplay函数跟statusDisplay切换时,需要清空之前的设置,否则可能会出现从pictureDisplay切换成statusDisplay出现,之前的图片并未被清除。

你可能感兴趣的:(C++,#,QT,qt,开发语言)