QT一些操作,类型转换,QLabel显示图片(自适应)

1、格式化字符串:

str.sprintf("%s_第%d帧_第%d个",filePath.toUtf8().data(),fame,i);//中文%s和%d同时出现
str.arg(%1%2).arg(str).arg(int)

2、QString -->Char

QString("hello").toStdString.c_str

3、QString-->Char*

char* ch
QByteArray ba = QString("hello").toLatin1();
ch=ba.data();

4、QLabel显示图片(充满)

QImage img=load("filePath");
//设定图像大小自适应label窗口的大小
img = img.scaled(ui->label_ImgContainer->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
ui->label->setPixmap(QPixmap::fromImage(img));

5、QLabel按原比例显示图片

void showImage(cv::Mat img,QLabel *labelID)
{
    int imgWidth=img.cols;
    int imgHeight=img.rows;
    int labelWidth=labelID->width();
    int labelHeight=labelID->height();
    double widthRate=(double)labelWidth/(double)imgWidth;
    double heightRate=(double)labelHeight/(double)imgHeight;
    if(widthRate<=heightRate)
    {
        QPixmap qimg=cvMatToQPixmap(img);
        qimg = qimg.scaled(QSize(labelWidth,(int)imgHeight*heightRate), Qt::KeepAspectRatio);
        labelID->setAlignment(Qt::AlignCenter);
        labelID->setPixmap(qimg);
    }
    else
    {
        QPixmap qimg=cvMatToQPixmap(img);
        qimg = qimg.scaled(QSize((int)imgWidth*widthRate,labelHeight), Qt::KeepAspectRatio);
        labelID->setAlignment(Qt::AlignCenter);
        labelID->setPixmap(qimg);
    }
}


你可能感兴趣的:(QT)