您的位置:首页 > 编程语言 > Qt开发

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只是做了播放器的最大化 最小化等功能

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()

}

}



大致就这些、、、
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片 qml qt