Qt tip qlabel 如何设置图片,图片大小设置为qlabel等同大小

  • 设置qlabel填充图片,并且将一张图片大小宽高设置为qlabel的边框宽高的大小。

ui->dangqian_tianqi_img_label->width() //为qlabel的宽
ui->dangqian_tianqi_img_label->height() //为qlabel的高


QPixmap tianqi_pixmap(":/img/ico/qing.png");
//qDebug() << tianqi_pixmap.width() << " /" << tianqi_pixmap.height();
tianqi_pixmap = tianqi_pixmap.scaled(ui->dangqian_tianqi_img_label->width(), ui->dangqian_tianqi_img_label->height(),
                                    Qt::IgnoreAspectRatio, Qt::SmoothTransformation);//设置图片大小和label的长宽一致
ui->dangqian_tianqi_img_label->setPixmap(tianqi_pixmap);
ui->dangqian_tianqi_img_label->show();

这里写图片描述


void Login::set_top_img(bool isSandom, int index_img)
{
    //427 185
    int set_index_img = 1;
    if(isSandom == true)//随机显示topimg
    {

        QTime time_sand;
        time_sand= QTime::currentTime();//获取当前时间
        qsrand(time_sand.msec()+time_sand.second()*1000);

        //index_img = qrand()%5 ;//在0-4中选出随机数
        set_index_img = qrand()%5 + 1 ;//在1-5中选出随机数

    }
    if(isSandom == false) //不随机显示,按index_img显示图片s
    {
        set_index_img = index_img;
    }

    QString top_img_path=":/images/top_img1.png";
    qDebug()<< "             [leo]" << top_img_path;
    QImage top_img;
    top_img_path = ":/images/top_img" + QString::number(set_index_img, 10)  + ".png";
    qDebug()<< "             [leo]" << top_img_path;
    top_img.load(top_img_path);
    QPixmap top_pic=QPixmap::fromImage(top_img.scaled(ui->label_top_img->width(),ui->label_top_img->height()));
    ui->label_top_img->setPixmap(top_pic);
    qDebug() << "          [leo]top_img width heigh:" << ui->label_top_img->width()
             << " " << ui->label_top_img->height();
}

你可能感兴趣的:(Qt,Creator,笔记)