qml图像没有重新加载/刷新错误

时间:2016-03-22 03:30:28

标签: c++ image qml qt5 reload

每当调用main.qml中的'videoUpdate'函数时,'line H'(在MainGUI.qml中)执行,requestImage(在ImageProvider.cpp中)返回一个QImage对象。但是,返回的图像不会出现在MainGUI(视频)图像上。 (程序执行后,只有一次,苹果图像出现并永远停留在那里。) 我做错什么了吗?

*重要(可能):使用的编译器是“catkin_make”(对于ROS项目),它调用cmake而不是“qmake后跟make”,这是“Qt Creator”(qt5)中的默认编译步骤。 / strong>

main.qml

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    visible: true
    width: 360
    height: 460
    property alias mainForm: mainForm
    property int videoWidth: 314
    property int videoHeight: 236

    property int imageSN : 0
    function videoUpdate() {
        imageSN++;
        mainForm.videoTube.source = "image://images/" + imageSN
        console.log("video frame ID#: " + imageSN)
        return ""
    }

    MainGUI {
        id: mainForm
        objectName: "mainForm"
        anchors.fill: parent

        signal startSignal()
        signal stopSignal()

        mouseArea_start.onClicked: {
            mainForm.startSignal();
        }

        mouseArea_stop.onClicked: {
            mainForm.stopSignal();
        }

        mouseArea_quit.onClicked: {
            mainForm.stopSignal();
            Qt.quit();
        }
    }
}

MainGUI.qml

import QtQuick 2.5

Rectangle {
    id: mainRect
    property alias mouseArea_quit: mouseArea_quit
    property alias mouseArea_start: mouseArea_start
    property alias mouseArea_stop: mouseArea_stop
    property alias videoTube: videoTube

    anchors.fill: parent

    Rectangle {
        id: rect_start
        x: 40
        y: 27
        width: 132
        height: 56
        color: "#ffffff"
        radius: 10
        border.width: 5

        Text {
            id: text1
            x: 0
            y: 21
            width: 132
            height: 15
            color: "#0f650d"
            text: qsTr("시작")
            horizontalAlignment: Text.AlignHCenter

            font.bold: true
            font.pixelSize: 16
        }

        MouseArea {
            id: mouseArea_start
            objectName: "mouseArea_start"

            x: 0
            y: 0
            width: 132
            height: 56
        }
    }

    Rectangle {
        id: rect_stop
        x: 190
        y: 27
        width: 132
        height: 56
        color: "#ffffff"
        radius: 10
        border.width: 5

        Text {
            id: text2
            x: 9
            y: 20
            width: 110
            height: 15
            color: "#ff0000"
            text: qsTr("멈춤")
            horizontalAlignment: Text.AlignHCenter
            font.bold: true
            font.pixelSize: 16
        }

        MouseArea {
            id: mouseArea_stop
            objectName: "mouseArea_stop"
            x: 10
            y: 0
            width: 110
            height: 56
        }
    }

    Rectangle {
        id: rect_quit
        x: 167
        y: 379
        width: 162
        height: 55
        color: "#ffffff"

        Text {
            x: 30
            y: 18
            anchors.centerIn: parent
            text: "Quit Program"
            font.bold: true
            font.pointSize: 12
            anchors.verticalCenterOffset: 0
            anchors.horizontalCenterOffset: 0
        }

        MouseArea {
            id: mouseArea_quit
            x: 0
            y: 0
            anchors.rightMargin: 0
            anchors.bottomMargin: 0
            anchors.leftMargin: 0
            anchors.topMargin: 0
            anchors.fill: parent
        }
    }

    Image {
        id: videoTube
        objectName: "videoTube"
        width: videoWidth
        height: videoHeight
        anchors.rightMargin: 20
        anchors.leftMargin: 20
        anchors.bottomMargin: 30
        anchors.top: rect_start.bottom
        anchors.right: parent.right
        anchors.bottom: rect_quit.top
        anchors.left: parent.left
        anchors.topMargin: 30
        source : "image://images/" + imageSN
        onSourceChanged: {
            console.log("source changed") // line H
        }
        cache: false
        sourceSize.width: videoWidth
        sourceSize.height: videoHeight
        fillMode: Image.PreserveAspectFit
    }

    Image {
        id: sm_img
        x: 20
        y: 357
        width: 100
        height: 100

    }
}

ImageProvider.cpp

#include "ImageProvider.h"
#include <QDebug>

ImageProvider::ImageProvider(QSize size)
    : QQuickImageProvider(QQuickImageProvider::Image)
{
    apple = new QImage();
    pear = new QImage();

    if (apple->load("apple.jpg")) {
    } else {
        qDebug() << "apple loading failed";
    }

    if (pear->load("pear.jpg")) {
    } else {
        qDebug() << "pear loading failed";
    }

    currentShot = new QImage();
    if (currentShot->load("echo.png")) {
        QImage *temp = currentShot;
        currentShot = new QImage(currentShot->scaled(
                                     size, Qt::KeepAspectRatio));
        delete temp;
    } else {
        delete currentShot;
    }
}

/**
 * @brief ImageProvider::requestImage
 * @param id : unique ID, generated in qml code (specify image to fecth)
 *             In case of WMPC, this parameter is needless.
 *              -- jbpark03@gmail.com
 * @param size : new image size if applicable
 * @param requestedSize : desired size; if valid, resize original and
 *                        return resized version and modify 3rd size para'.
 * @return
 */
QImage ImageProvider::requestImage(const QString &id, QSize *size,
                                   const QSize &requestedSize)
{
    if (id.toInt() % 3 == 0) {
        *size = apple->size();
        return *apple;
    } else if (id.toInt() % 3 != 0) {
        *size = pear->size();
        return *pear;
    }

    if (requestedSize.isValid() &&
            sizeDiffers(requestedSize, currentShot->size()))
    {
        QImage * modifiedImg =
                new QImage(currentShot->scaled(requestedSize, Qt::KeepAspectRatio));

        delete currentShot;
        currentShot = nullptr;

        *size = modifiedImg->size();
        return *modifiedImg;
    } else {
        qDebug() << "invalid or size matched";

        // do nothing: just return currentShot
        QImage *temp = currentShot;
        *size = currentShot->size();

        QImage temp2 = QImage(*currentShot).convertToFormat(QImage::Format_RGB32);
        qDebug() << "return size: " << temp2.size() << ", type: "
                 << temp2.format() << "\n";
        temp2.save("echo.png");
        *size = temp2.size();
        return temp2;
    }
}

bool ImageProvider::sizeDiffers(QSize ltSize, QSize rtSize)
{
    if (std::abs(ltSize.width() - rtSize.width()) > 1 ||
            std::abs(ltSize.height() - rtSize.height()) > 1)
    {
        return true;
    } else {
        return false;
    }
}

0 个答案:

没有答案