From 5feaf45463a185519239d9615973f100284bebff Mon Sep 17 00:00:00 2001 From: Valentin Veremchuk Date: Thu, 12 Mar 2020 21:50:07 +0300 Subject: [PATCH 1/2] Add don't working window --- qml/FileOpenWindow.qml | 41 +++++++++++++++++++++++++++++++++++++++++ qml/NewTable.qml | 5 ++++- qml/main.qml | 5 ++--- qml/qml.qrc | 1 + src/executionfile.cpp | 32 +++++++++++++++++++++++++++++--- src/executionfile.h | 15 ++++++++++----- src/main.cpp | 3 +-- 7 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 qml/FileOpenWindow.qml diff --git a/qml/FileOpenWindow.qml b/qml/FileOpenWindow.qml new file mode 100644 index 0000000..5902324 --- /dev/null +++ b/qml/FileOpenWindow.qml @@ -0,0 +1,41 @@ +import QtQuick 2.12 +import QtQuick.Window 2.12 +import QtQuick.Controls 2.5 + +import space.developers 1.0 + +ApplicationWindow { + id: openImageWindow + signal signalExit + minimumWidth: 300 + minimumHeight: 300 + width: 400 + height: 400 + visible: true + + Rectangle { + width: parent.width + height: parent.height - anotherWindow_button.height + color: "gray" + + WorkWithFiles { + anchors.top: parent.top + anchors.topMargin: 15 + anchors.horizontalCenter: parent.horizontalCenter + width: parent.width + height: parent.height + } + } + + Button { + id: anotherWindow_button + text: qsTr("Главное окно") + width: 180 + height: 50 + anchors.bottom: parent.bottom + anchors.horizontalCenter: parent.horizontalCenter + onClicked: { + console.log("close") + } + } +} diff --git a/qml/NewTable.qml b/qml/NewTable.qml index 9c01a45..42b4f5d 100644 --- a/qml/NewTable.qml +++ b/qml/NewTable.qml @@ -1,6 +1,8 @@ import QtQuick 2.0 import QtQuick.Controls 1.0 +import space.developers 1.0 + Rectangle { id:colorRecta color: "#F0F8FF" // crimson @@ -22,7 +24,8 @@ Rectangle { dataModel.updateData(model.path) } else { - workFile.openFile(model.path,model.flag) + workWithFiles.filePath(model.path) + workWithFiles.openFile(model.path, model.flag) } } } diff --git a/qml/main.qml b/qml/main.qml index 61f0190..4c7e790 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -17,9 +17,8 @@ Window { id: dataModel } - WorkFile { - id: workFile + WorkWithFiles { + id: workWithFiles } - } diff --git a/qml/qml.qrc b/qml/qml.qrc index 6da25f1..9066b4e 100644 --- a/qml/qml.qrc +++ b/qml/qml.qrc @@ -8,6 +8,7 @@ ColumnFlag.qml PathMenu.qml MasterButton.qml + FileOpenWindow.qml imeg/back-icon.png diff --git a/src/executionfile.cpp b/src/executionfile.cpp index a6b7d73..17635f9 100644 --- a/src/executionfile.cpp +++ b/src/executionfile.cpp @@ -1,11 +1,37 @@ #include "executionfile.h" +#include +#include -ExecutionFile::ExecutionFile(QObject *parent): QObject (parent) +ExecutionFile::ExecutionFile(QQuickItem *parent): QQuickPaintedItem(parent) { } -void ExecutionFile::openFile(QString path, QString flag) +void ExecutionFile::paint(QPainter *painter) { - m_fale=path+flag; + QPixmap pixmap(m_filePath); + QRectF source(0, 0, 0, 0); + QRectF imageRect = boundingRect(); + imageRect.setSize(pixmap.size().scaled(boundingRect().size().toSize(),Qt::KeepAspectRatio)); + imageRect.moveCenter(boundingRect().center()); + painter->drawPixmap(imageRect, pixmap, source); +} + +void ExecutionFile::openFile(const QString &path, const QString &flag) +{ + if (flag == "jpg" || flag == "jpeg" || flag == "png") { + QQuickView view; + view.setSource(QStringLiteral("qrc:/FileOpenWindow.qml")); + QQuickPaintedItem::update(); + } else { + QDesktopServices::openUrl(QUrl::fromLocalFile(path)); + } +} + +void ExecutionFile::filePath(const QString &path) +{ + if (m_filePath == path) + return; + m_filePath = path; + update(); } diff --git a/src/executionfile.h b/src/executionfile.h index dccbe1a..04a59df 100644 --- a/src/executionfile.h +++ b/src/executionfile.h @@ -1,16 +1,21 @@ #ifndef EXECUTIONFILE_H #define EXECUTIONFILE_H -#include +#include +#include -class ExecutionFile : public QObject +class ExecutionFile : public QQuickPaintedItem { Q_OBJECT public: - ExecutionFile(QObject *parent = nullptr); - Q_INVOKABLE void openFile(QString path,QString flag); + ExecutionFile(QQuickItem *parent = nullptr); + + void paint(QPainter *painter) override; + + Q_INVOKABLE void openFile(const QString &path, const QString &flag); + Q_INVOKABLE void filePath(const QString &path); private: - QString m_fale; + QString m_filePath; }; #endif // EXECUTIONFILE_H diff --git a/src/main.cpp b/src/main.cpp index 2c851f6..482814a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,12 +10,11 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); qmlRegisterType("space.developers", 1, 0, "NewTable"); - qmlRegisterType("space.developers", 1, 0, "WorkFile"); + qmlRegisterType("space.developers", 1, 0, "WorkWithFiles"); QQmlApplicationEngine engine; - const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { From 16ddd81beb45492d65f3d4a38e5e70340b537b16 Mon Sep 17 00:00:00 2001 From: Valentin-Vr Date: Sat, 14 Mar 2020 12:14:44 +0300 Subject: [PATCH 2/2] change the place of the file "FileOpenWindow.qml" call --- qml/FileOpenWindow.qml | 2 +- qml/NewTable.qml | 11 +++++++++-- src/executionfile.cpp | 20 +++++++++++--------- src/executionfile.h | 3 ++- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/qml/FileOpenWindow.qml b/qml/FileOpenWindow.qml index 5902324..c85afd9 100644 --- a/qml/FileOpenWindow.qml +++ b/qml/FileOpenWindow.qml @@ -35,7 +35,7 @@ ApplicationWindow { anchors.bottom: parent.bottom anchors.horizontalCenter: parent.horizontalCenter onClicked: { - console.log("close") + openImageWindow.close() } } } diff --git a/qml/NewTable.qml b/qml/NewTable.qml index 42b4f5d..345cad3 100644 --- a/qml/NewTable.qml +++ b/qml/NewTable.qml @@ -24,8 +24,15 @@ Rectangle { dataModel.updateData(model.path) } else { - workWithFiles.filePath(model.path) - workWithFiles.openFile(model.path, model.flag) + if (model.flag === "jpg" || model.flag === "png" || model.flag === "jpeg") { + workWithFiles.openImage(model.path) + var component = Qt.createComponent("FileOpenWindow.qml") + var window = component.createObject("root") + window.show() + } + else { + workWithFiles.openFile(model.path) + } } } } diff --git a/src/executionfile.cpp b/src/executionfile.cpp index 17635f9..9935eed 100644 --- a/src/executionfile.cpp +++ b/src/executionfile.cpp @@ -9,7 +9,8 @@ ExecutionFile::ExecutionFile(QQuickItem *parent): QQuickPaintedItem(parent) void ExecutionFile::paint(QPainter *painter) { - QPixmap pixmap(m_filePath); + qDebug() << "paint" << m_filePath; + QPixmap pixmap("C:/Users/User/Desktop/img.jpg"); QRectF source(0, 0, 0, 0); QRectF imageRect = boundingRect(); imageRect.setSize(pixmap.size().scaled(boundingRect().size().toSize(),Qt::KeepAspectRatio)); @@ -17,15 +18,9 @@ void ExecutionFile::paint(QPainter *painter) painter->drawPixmap(imageRect, pixmap, source); } -void ExecutionFile::openFile(const QString &path, const QString &flag) +void ExecutionFile::openFile(const QString &path) { - if (flag == "jpg" || flag == "jpeg" || flag == "png") { - QQuickView view; - view.setSource(QStringLiteral("qrc:/FileOpenWindow.qml")); - QQuickPaintedItem::update(); - } else { - QDesktopServices::openUrl(QUrl::fromLocalFile(path)); - } + QDesktopServices::openUrl(QUrl::fromLocalFile(path)); } void ExecutionFile::filePath(const QString &path) @@ -35,3 +30,10 @@ void ExecutionFile::filePath(const QString &path) m_filePath = path; update(); } + +void ExecutionFile::openImage(const QString &path) +{ + filePath(path); + qDebug() << "m_filePath =" << m_filePath; + QQuickPaintedItem::update(); +} diff --git a/src/executionfile.h b/src/executionfile.h index 04a59df..f1c4c19 100644 --- a/src/executionfile.h +++ b/src/executionfile.h @@ -11,8 +11,9 @@ class ExecutionFile : public QQuickPaintedItem void paint(QPainter *painter) override; - Q_INVOKABLE void openFile(const QString &path, const QString &flag); + Q_INVOKABLE void openFile(const QString &path); Q_INVOKABLE void filePath(const QString &path); + Q_INVOKABLE void openImage(const QString &path); private: QString m_filePath;