找回密码
 立即注册
收起左侧

QML界面显示问题

10
回复
4488
查看
[复制链接]
累计签到:198 天
连续签到:1 天
来源: 2020-3-4 21:21:12 显示全部楼层 |阅读模式
1Qter豆
本帖最后由 keyiangel 于 2020-3-4 21:25 编辑

最近新学习QML,编写很简单的画面就遇到BUG了,也不知道原因,请教一下。
正常程序运行画面如图:

出异常时如图:




代码如下:

main.qml
import QtQuick 2.14
import QtQuick.Window 2.14

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    EditMenu{
       id:editmenu1
        x:100
       y:100
    }
    FileMenu{
        id:filemenu1
        x:100
        y:300
    }

}



Button.qml
import QtQuick 2.0


    Rectangle{
        id:button
        //color: "grey"
        property color buttonColor: "lightblue"
        property color onHoverColor: "gold"
        property color borderColor: "white"
        property string label: ""
        radius: 37.5
        width:150;height: 75
        signal buttonClick()
        onButtonClick: {
            console.log(button.label+"clicked")
        }

        Text {
            id: buttonlabel
            text:label
            anchors.centerIn: parent
        }

        MouseArea{
            id:buttonMouseArea
            anchors.fill: parent
            onClicked: parent.buttonClick()
            hoverEnabled: true
            onEntered: parent.border.color=parent.onHoverColor
            onExited: parent.border.color=parent.borderColor
        }

        color: buttonMouseArea.pressed ? Qt.darker(buttonColor,1.5):buttonColor


    }



EditMenu.qml
import QtQuick 2.0


    Row{
//        anchors.centerIn: parent
        spacing: parent.width/6

        Button{
            id:copy
            buttonColor:"lightgrey"
            label:"Copy"
        }
        Button{
            id:paste
            buttonColor:"green"
            label:"Paste"
        }
        Button{
            id:selectall
            buttonColor:"grey"
            label:"SelectAll"
        }
    }




FileMenu.qml
import QtQuick 2.0


    Row{
//        anchors.centerIn: parent
        spacing: parent.width/6

        Button{
            id:loadButton
            buttonColor:"lightgrey"
            label:"Load"
        }
        Button{
            id:saveButton
            buttonColor:"grey"
            label:"Save"
        }
        Button{
            id:exitButton
            buttonColor:"darkgrey"
            label:"Exit"
            onButtonClick: Qt.quit()
        }
    }

附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册
回复

使用道具 举报

累计签到:198 天
连续签到:1 天
2020-3-4 21:27:14 显示全部楼层
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}


test.pro
QT += quick

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

RESOURCES += qml.qrc

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

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
回复

使用道具 举报

累计签到:198 天
连续签到:1 天
2020-3-6 13:24:14 显示全部楼层
自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶
回复

使用道具 举报

累计签到:198 天
连续签到:1 天
2020-3-7 16:33:43 显示全部楼层
自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶
回复

使用道具 举报

累计签到:198 天
连续签到:1 天
2020-3-8 19:24:20 显示全部楼层
自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶
回复

使用道具 举报

累计签到:198 天
连续签到:1 天
2020-3-9 11:56:42 显示全部楼层
自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶
回复

使用道具 举报

累计签到:198 天
连续签到:1 天
2020-3-10 09:19:09 显示全部楼层
自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶
回复

使用道具 举报

累计签到:198 天
连续签到:1 天
2020-3-11 13:00:15 显示全部楼层
自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶
回复

使用道具 举报

累计签到:198 天
连续签到:1 天
2020-3-12 17:21:21 显示全部楼层
自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶
回复

使用道具 举报

累计签到:198 天
连续签到:1 天
2020-3-13 12:21:36 显示全部楼层
自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶
回复

使用道具 举报

累计签到:198 天
连续签到:1 天
2020-3-14 08:32:52 显示全部楼层
自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶自己顶
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了