qml图片显示器传参版本
2016-12-28 12:44
477 查看
直接上代码了,记录防止以后自己忘记了。。。
main.cpp
#include <cmath>
#include <QtCore/QtDebug>
#include <QtCore/QFile>
//#include <QtGui/QtGui>
#include <QtGui/QGuiApplication>
#include <QQuickItem>
#include <QtQml/QQmlEngine>
#include <QtQml/QQmlContext>
#include <QtGui/QScreen>
#include <QtQuick/QQuickView>
#include <QFileInfo>
#include <QApplication>
#include <QDesktopWidget>
//#include <QWidget>
//#include <QMainWindow>
//#include <QDesktopWidget>
int main(int argc, char *argv[])
{
if (argc < 2) {
return 0;
}
QApplication app(argc, argv);
/*
获取屏幕分辨率
*/
QDesktopWidget *d = QApplication::desktop();
int width = d->width();
int height = d->height();
int w = width - 140;
int h = height - 80;
// qDebug() << "w:" << w;
// qDebug() << "h:" << h;
QString file = app.arguments().at(1);
if (file.isEmpty() || !QFile(file).exists()) {
qDebug() << "Invalid file:" << file;
return -1;
}
QString title = QFileInfo(file).baseName();
// Use second parameter as title
title = app.arguments().at(2);
QQuickView viewer;
//设置大小
viewer.setResizeMode(QQuickView::SizeRootObjectToView);
//无窗口
//viewer.setFlags(Qt::FramelessWindowHint);
// viewer.setFlags(Qt::FramelessWindowHint
// | Qt::WindowSystemMenuHint
// | Qt::WindowMinimizeButtonHint
// | Qt::Window);
QQmlEngine *engine = viewer.engine();
engine->rootContext()->setContextProperty("win", &viewer);
viewer.setSource(QUrl("qrc:///qml/Main.qml"));
viewer.setTitle(title);
viewer.show();
// QDesktopWidget *dwsktopwidget = QApplication::desktop();
//QRect deskrect = dwsktopwidget->availableGeometry();
// QRect screenrect = dwsktopwidget->screenGeometry();
// qCritical("screenrect.w==%s\n",qPrintable(QString::number(screenrect.width())));
//viewer.setWindowState(Qt::FramelessWindowHint);
//判断参数个数
if (argc == 3) {
viewer.setGeometry(70,30,w,h);
}
if(argc == 4){
viewer.setWindowState(Qt::WindowNoState);
}
//viewer.move ((QApplication::desktop()->width() - w.width())/2,(QApplication::desktop()->height() - w.height())/2);
file.prepend("file:");
file.replace("\\", "/");
QMetaObject::invokeMethod(viewer.rootObject(), "init", Q_ARG(QVariant, file));
return app.exec();
}
main.qml
import QtQuick 2.0
import QtWebKit 3.0
import QtQuick.Window 2.0
Rectangle {
id: root
objectName: "root"
width: 800
height: 450
color: "black"
//传参数
function init(url) {
img.source = url;
console.log("img.source: " + img.source);
}
Image {
id: img
objectName: "img"
width: parent.width
height: parent.height
fillMode: Image.PreserveAspectFit
x: (root.width-img.width)/2
y: (root.height-img.height)/2
rotation: 0
}
PinchArea {
anchors.fill: parent
pinch.target: img
// pinch.minimumRotation: -360
// pinch.maximumRotation: 360
pinch.minimumScale: 0.5
pinch.maximumScale: 10
MouseArea {
id: dragArea1
// hoverEnabled: true
anchors.fill: parent
drag.target: img
onPressed: {
console.log("what`s this?")
console.log("root.width:",root.width);
console.log("img.width:",img.width);
toolbar.toggleVisible()
}
}
}
//MouseArea {
//anchors.fill: parent
//// drag.target: root
//onPressed: {
// console.log("what`s this?")
// toolbar.toggleVisible()
//}
//}
Toolbar {
id: toolbar
anchors {
left: parent.left
bottom: parent.bottom
right: parent.right
}
}
DropArea {
anchors.fill: parent
onEntered: {
if (!drag.hasUrls)
return;
console.log(drag.urls)
img.source = drag.urls[0]
}
}
}toolBar.qml只是做了播放器的最大化 最小化等功能
大致就这些、、、
main.cpp
#include <cmath>
#include <QtCore/QtDebug>
#include <QtCore/QFile>
//#include <QtGui/QtGui>
#include <QtGui/QGuiApplication>
#include <QQuickItem>
#include <QtQml/QQmlEngine>
#include <QtQml/QQmlContext>
#include <QtGui/QScreen>
#include <QtQuick/QQuickView>
#include <QFileInfo>
#include <QApplication>
#include <QDesktopWidget>
//#include <QWidget>
//#include <QMainWindow>
//#include <QDesktopWidget>
int main(int argc, char *argv[])
{
if (argc < 2) {
return 0;
}
QApplication app(argc, argv);
/*
获取屏幕分辨率
*/
QDesktopWidget *d = QApplication::desktop();
int width = d->width();
int height = d->height();
int w = width - 140;
int h = height - 80;
// qDebug() << "w:" << w;
// qDebug() << "h:" << h;
QString file = app.arguments().at(1);
if (file.isEmpty() || !QFile(file).exists()) {
qDebug() << "Invalid file:" << file;
return -1;
}
QString title = QFileInfo(file).baseName();
// Use second parameter as title
title = app.arguments().at(2);
QQuickView viewer;
//设置大小
viewer.setResizeMode(QQuickView::SizeRootObjectToView);
//无窗口
//viewer.setFlags(Qt::FramelessWindowHint);
// viewer.setFlags(Qt::FramelessWindowHint
// | Qt::WindowSystemMenuHint
// | Qt::WindowMinimizeButtonHint
// | Qt::Window);
QQmlEngine *engine = viewer.engine();
engine->rootContext()->setContextProperty("win", &viewer);
viewer.setSource(QUrl("qrc:///qml/Main.qml"));
viewer.setTitle(title);
viewer.show();
// QDesktopWidget *dwsktopwidget = QApplication::desktop();
//QRect deskrect = dwsktopwidget->availableGeometry();
// QRect screenrect = dwsktopwidget->screenGeometry();
// qCritical("screenrect.w==%s\n",qPrintable(QString::number(screenrect.width())));
//viewer.setWindowState(Qt::FramelessWindowHint);
//判断参数个数
if (argc == 3) {
viewer.setGeometry(70,30,w,h);
}
if(argc == 4){
viewer.setWindowState(Qt::WindowNoState);
}
//viewer.move ((QApplication::desktop()->width() - w.width())/2,(QApplication::desktop()->height() - w.height())/2);
file.prepend("file:");
file.replace("\\", "/");
QMetaObject::invokeMethod(viewer.rootObject(), "init", Q_ARG(QVariant, file));
return app.exec();
}
main.qml
import QtQuick 2.0
import QtWebKit 3.0
import QtQuick.Window 2.0
Rectangle {
id: root
objectName: "root"
width: 800
height: 450
color: "black"
//传参数
function init(url) {
img.source = url;
console.log("img.source: " + img.source);
}
Image {
id: img
objectName: "img"
width: parent.width
height: parent.height
fillMode: Image.PreserveAspectFit
x: (root.width-img.width)/2
y: (root.height-img.height)/2
rotation: 0
}
PinchArea {
anchors.fill: parent
pinch.target: img
// pinch.minimumRotation: -360
// pinch.maximumRotation: 360
pinch.minimumScale: 0.5
pinch.maximumScale: 10
MouseArea {
id: dragArea1
// hoverEnabled: true
anchors.fill: parent
drag.target: img
onPressed: {
console.log("what`s this?")
console.log("root.width:",root.width);
console.log("img.width:",img.width);
toolbar.toggleVisible()
}
}
}
//MouseArea {
//anchors.fill: parent
//// drag.target: root
//onPressed: {
// console.log("what`s this?")
// toolbar.toggleVisible()
//}
//}
Toolbar {
id: toolbar
anchors {
left: parent.left
bottom: parent.bottom
right: parent.right
}
}
DropArea {
anchors.fill: parent
onEntered: {
if (!drag.hasUrls)
return;
console.log(drag.urls)
img.source = drag.urls[0]
}
}
}toolBar.qml只是做了播放器的最大化 最小化等功能
import QtQuick 2.0
import QtQuick.Window 2.0
import "app.js" as App
Rectangle {
id: root
color: "black"
opacity: 0.9
height: 60
property bool hiding: false
property int timeFlag: 0
property bool sendORStop:true
function toggleFullScreen() {
maxBtn.checked = !maxBtn.checked
}
gradient: Gradient {
GradientStop { position: 0.0; color: "#aa445566" }
GradientStop { position: 0.618; color: "#bb1a2b3a" }
GradientStop { position: 1.0; color: "#ff000000" }
}
MouseArea {
anchors.fill: parent
onPressed: {
if (timer.running) //timer may ran a few seconds(<3) ago
timer.stop();
root.aniShow()
}
}
Item {
anchors {
top: parent.top
bottom: parent.bottom
left: parent.left
right: parent.right
margins: 10
}
Row {
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
spacing: 5
Button {
id: closeBtn
bgColor: "transparent"
bgColorSelected: "transparent"
width: 36
height: 36
icon: App.res("images/close.png")
visible: true
onClicked: {
App.stop(img.source);
win.close();
}
}
Button {
id: minBtn
bgColor: "transparent"
bgColorSelected: "transparent"
width: 36
height: 36
icon: App.res("images/min.png")
onClicked: win.showMinimized()
}
Button {
id: maxBtn
checkable: true
checked: false
bgColor: "transparent"
bgColorSelected: "transparent"
width: 36
height: 36
icon: App.res("images/max.png")
iconChecked: App.res("images/maxMin.png")
visible: true
onClicked: {
if (win.width>800) {
console.log("checked == true?:" + checked);
win.width = 800
win.height = 450
win.x = (Screen.desktopAvailableWidth-800)/2
win.y = (Screen.desktopAvailableHeight-450)/2
hideIfTimedout();
}else {
console.log("checked == flase?:",checked);
win.x = 70
win.y = 30
win.width = Screen.desktopAvailableWidth-140
win.height = Screen.desktopAvailableHeight-40
}
}
// onCheckedChanged: {
//// console.log("check " + checked)
// if (checked) {
//// console.log("min check " + checked)
// win.showNormal()
//// win.setWidth(800)
//// win.setHeight(450)
// hideIfTimedout()
//}
// else
//// console.log("max check " + checked)
// win.showFullScreen()
//// win.setX(60)
//// win.setY(30)
//// win.setWidth(1800)
//// win.setHeight(1020)
//}
}
Button {
id: leftRotationBtn
bgColor: "transparent"
bgColorSelected: "transparent"
width: 36
height: 36
icon: App.res("images/leftRotation.png")
visible: true
onClicked: {
img.rotation = ( img.rotation - 90.0 ) % 360
}
}
Button {
id: rightRotationBtn
bgColor: "transparent"
bgColorSelected: "transparent"
width: 36
height: 36
icon: App.res("images/rightRotation.png")
visible: true
onClicked: {
img.rotation = ( img.rotation + 90.0 ) % 360
}
}
}
Row {
anchors.left: parent.right
anchors.verticalCenter: parent.verticalCenter
spacing: 10
anchors.margins: -50
Button {
id: sendBtn
bgColor: "transparent"
bgColorSelected: "transparent"
x: 50
width: 36
height: 36
icon: App.res("images/send.png")
onClicked: {
App.send(img.source)
sendORStop = true
sendtimer.start()
sendBtn.visible = false;
progressRectangle.visible = true
}
}
Button {
id: sendStopBtn
bgColor: "transparent"
bgColorSelected: "transparent"
x: 50
width: 36
height: 36
icon: App.res("images/sendStop.png")
onClicked: {
App.stop(img.source);
sendORStop = false
sendtimer.start()
sendStopBtn.visible = false;
progressRectangle.visible = true
}
visible: false;
}
Button{
id: progressRectangle
bgColor: "transparent"
bgColorSelected: "transparent"
width: 4
height: 36
icon: App.res("images/progressRectangle.png")
visible:false
}
Timer{
id: sendtimer
interval: 100
repeat: true
onTriggered: {
timeFlag++
progressRectangle.width = progressRectangle.width + 1
if(progressRectangle.width >= 36){
progressRectangle.width = 4
}
if(timeFlag >= 30){
timeFlag = 0
progressRectangle.width = 4
sendtimer.stop()
progressRectangle.visible = false
if(sendORStop){
sendStopBtn.visible = true
}else{
sendBtn.visible = true
}
}
}
}
}
}
Timer {
id: timer
interval: 3000
onTriggered: {
root.aniHide()
}
}
function hideIfTimedout() {
timer.start()
}
PropertyAnimation {
id: anim
target: root
properties: "opacity"
function reverse() {
duration = 1500
to = 0.2
from = root.opacity
}
function reset() {
duration = 200
from = root.opacity
to = 0.9
}
}
function aniShow() {
hiding = false
anim.stop()
anim.reset()
anim.start()
}
function aniHide() {
hiding = true
anim.stop()
anim.reverse()
anim.start()
}
function toggleVisible() {
// Only toggle at fullscreen
if (!maxBtn.checked)
console.log("checked == ?:" + maxBtn.checked);
return
if (hiding)
aniShow()
else
aniHide()
}
}
大致就这些、、、
相关文章推荐
- QT学习 第一章:基本对话框
- 使用Shiboken为C++和Qt库创建Python绑定
- windows用windeployqt发布qt quick application程序
- Linux 下最棒的 11 个图片查看器
- Qt 5.6更新至RC版,最终版本近在咫尺
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- upload上传单张图片
- 图片引发的溢出危机(图)
- C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法
- C#实现把彩色图片灰度化代码分享
- C#将图片和字节流互相转换并显示到页面上
- C#监控文件夹并自动给图片文件打水印的方法
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- 如何使用C#从word文档中提取图片
- C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- C#图片添加水印的实现代码
- 随鼠标移动的图片或文字特效代码
- CSS 图片横向排列实现代码
- C#实现将Email地址转成图片显示的方法