欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Qt文档阅读笔记-Image QML官方解析与实例

发布时间:2025/3/15 32 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Qt文档阅读笔记-Image QML官方解析与实例 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

目录

 

 

官方解析

博主例子


 

官方解析

Image
Image用于展示图片。
使用source属性可以使用URL指定一张图片。
只要是Qt能打开的图片都支持,如果要展示动态图,要使用:BnimatedSprite和AnimatedImage。
如果width和height属性没有被指定,将会使用图像原来的大小。fillMode属性可以使得图像被拉伸和平铺。

例子:
最简单的图像用法!

import QtQuick 2.0Image {source: "pics/qtlogo.png"}

 

博主例子

此处的例子有2个功能,一个是补充官方的伪代码,二是把以前的方图,变化为圆图(非拉伸)

程序运行截图如下:

 

程序结构图如下:

源码如下:

QMLImageDemo.pro

QT += core gui qml quickgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = QMLImageDemo TEMPLATE = appDEFINES += QT_DEPRECATED_WARNINGSSOURCES += main.cppHEADERS +=RESOURCES += \resources.qrc

main.cpp

#include <QApplication> #include <QQmlApplicationEngine> #include <QDebug>int main(int argc, char *argv[]) {QApplication a(argc, argv);QQmlApplicationEngine engine;engine.load(QUrl("qrc:/main.qml"));if(engine.rootObjects().isEmpty()){qDebug() << "engine load failed!";return -1;}return a.exec(); }

main.qml

import QtQuick 2.7 import QtQuick.Controls 2.0 import QtGraphicalEffects 1.0ApplicationWindow {visible: truewidth: 300height: 300// Image {// anchors.centerIn: parent // source: "img/logo.png" // }Image {anchors.centerIn: parentid: pigpigpigsource: "img/logo.png"sourceSize: Qt.size(parent.width, parent.height)smooth: truevisible: false}Rectangle {id: maskwidth: parent.widthheight: parent.heightradius: height / 2color: "red"visible: false}OpacityMask {anchors.fill: pigpigpigsource: pigpigpigmaskSource: mask}}

 

总结

以上是生活随笔为你收集整理的Qt文档阅读笔记-Image QML官方解析与实例的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。