2

当我在 Qt 5.7 中使用我的自定义弹出窗口实现时,我得到了下面的运行时错误(Qt 5.7 是必要的,因为引入了 Popup 并且需要使用
-developer-build标志编译它,以便导出私有符号(QQuickPopup 类))。

您可以尝试在MyItem.qml 中将CPopup“”替换为“” ,您会看到它有效,因此问题需要在我的类中继承的某个地方。PopupQQuickPopupCustomPopup

错误: ASSERT: "_currentList.object" in file qml/qqmlobjectcreator.cpp, line 945

https://github.com/qt/qtquickcontrols2/blob/5.7/src/quicktemplates2/qquickpopup_p.h https://github.com/qt/qtquickcontrols2/blob/5.7/src/quicktemplates2/qquickpopup.cpp https:// github.com/qt/qtdeclarative/blob/dev/src/qml/qml/qqmlobjectcreator_p.h https://github.com/qt/qtdeclarative/blob/dev/src/qml/qml/qqmlobjectcreator.cpp

项目:弹出测试

popuptest.pro

TEMPLATE = app

QT += gui qml quick quickcontrols2
QT_PRIVATE += quick-private core-private gui-private qml-private quickcontrols2-private quicktemplates2-private

CONFIG += c++11

SOURCES += main.cpp \
    custompopup.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Default rules for deployment.
include(deployment.pri)

HEADERS += \
    custompopup.h

custompopup.h

#ifndef CUSTOMPOPUP_H
#define CUSTOMPOPUP_H

#include <private/qquickpopup_p.h>

class CustomPopup : public QQuickPopup
{

    Q_OBJECT
    //Q_PROPERTY(QQmlListProperty<QObject> contentData READ contentData FINAL)
    //Q_CLASSINFO("DefaultProperty", "contentData")
public:
    CustomPopup(QObject *parent = nullptr);

protected:
    bool overlayEvent(QQuickItem *item, QEvent *event) override;

private:
    /*Q_DISABLE_COPY(CustomPopup)
    Q_DECLARE_PRIVATE(QQuickPopup)*/
};


QML_DECLARE_TYPE(CustomPopup)

#endif // CUSTOMPOPUP_H

custompopup.cpp

#include "custompopup.h"

CustomPopup::CustomPopup(QObject *parent) : QQuickPopup(parent)
{

}

bool CustomPopup::overlayEvent(QQuickItem *item, QEvent *event)
{
        /*QQuickPopup implementation
        Q_D(QQuickPopup);
        switch (event->type()) {
        case QEvent::KeyPress:
        case QEvent::KeyRelease:
        case QEvent::MouseMove:
        case QEvent::Wheel:
            if (d->modal)
                event->accept();
            return d->modal;

        case QEvent::MouseButtonPress:
        case QEvent::MouseButtonRelease:
            if (d->modal)
                event->accept();
            d->tryClose(item, static_cast<QMouseEvent *>(event));
            return d->modal;

        default:
            return false;
        }
        */
   // Q_D(QQuickPopup);
    return QQuickPopup::overlayEvent(item, event);
}

主文件

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "custompopup.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    qmlRegisterType<CustomPopup>("Custom", 1, 0, "CPopup");

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

main.qml

import QtQuick 2.7
import QtQuick.Controls 1.5

ApplicationWindow {
    visible: true
    width: 600
    height: 480

    MyItem {
        anchors.fill: parent
    }
}

我的项目.qml

import QtQuick 2.0
import QtQuick.Controls 2.0
import Custom 1.0

Item {

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.RightButton
        onClicked: {
            menu.x = mouse.x
            menu.y = mouse.y
            menu.open()
        }

    }

    CPopup {
        id: menu
        Rectangle {
            width: 100
            height: 100
            color: "red"
        }
    }
}
4

0 回答 0