English 中文(简体)
QML MouseArea
原标题:QML Move frameless ApplicationWindow holding the MouseArea

配置(目前我从我的公司获得的版本越多):

  • Qt 5.12
  • Red Hat 7.9 Linux server
  • GCC 4.8.5

我正在开展一个项目,在这个项目中,窗户的所有权线可以像我所希望的那样实现个人化(增加原木,更新纽特权......)。 因此,我创造了一个无情的传彩礼(有Qt.FramelessWindowHint和Qt.Window旗),并在顶上添加了一条我所希望的所有其他内容。

但是,如果没有窗户的原始所有权条码,我只能把窗户移走。 因此,我补充说,“MouseArea”填补了我的空白,并计算了窗口的新位置,如:

The main.qml:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5

ApplicationWindow
{
    id: mainWindow

    property int    previousX:      0
    property int    previousY:      0
    property bool   isFullScreen:   false

    visible:        true
    visibility:     Qt.Window
    title:          ""
    minimumWidth:   1400
    minimumHeight:  900
    flags:          Qt.Window | Qt.FramelessWindowHint

    Rectangle
    {
        id: mainWindowTitleBar
        anchors
        {
            top:        parent.top
            left:       parent.left
            right:      parent.right
        }
        width:          parent.width
        height:         30
        color:          "white"
        z:              1

        MouseArea
        {
            id: mouseAreaToChangePosition
            anchors.fill: parent

            onPressed:
            {
                previousX = mouseX
                previousY = mouseY
            }

            onMouseXChanged:
            {
                var dx = mouseX - previousX
                mainWindow.setX(mainWindow.x + dx)
            }

            onMouseYChanged:
            {
                var dy = mouseY - previousY
                mainWindow.setY(mainWindow.y + dy)
            }

            onDoubleClicked:
            {
                if (isFullScreen)
                {
                    mainWindow.visibility = Qt.WindowMinimized
                }
                else
                {
                    mainWindow.visibility = Qt.WindowFullScreen
                }

                isFullScreen = !isFullScreen
            }
        }
    }
}

改为<代码>main.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QQmlContext>

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

        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;

        engine.load(QUrl("qrc:/main.qml"));
        if (engine.rootObjects().isEmpty())
            return -1;

        return app.exec();
    }
    catch(const std::exception& ex)
    {
        // Handle any exceptions that might occur during application execution
        qDebug().noquote() << "Error: " << ex.what();
        return 1;
    }
}

But, doing this, the move is not correct for me, because I can t "hide" half the window dragging it on the border of the screen. The minimum and maximum position accepted are the ones for which the application is fully display on screen.

是否有可能以任何方式直接叫“女权者”来进一步履行这一职能? 我在互联网上看到在QML中暴露QGuiApplication,但我不知道如何派有新立场的活动。

提前感谢。

最佳回答

您可以请QWindow:startSystemve 摘自QML:

MouseArea {
    anchors.fill: parent
    onPressed: mainWindow.startSystemMove()
}

注:您也可通过<条码>的可操作性:是否完整易懂? Qt.WindowFullScreen : Qt.Window in You Window and in You MouseArea.

问题回答

I ve found that there are several issues with the accepted answer and the concept provied by author:

  1. <代码>即“FullScreen<>/code> 财产是多余的,如果通过其他手段尽量减少窗口,则会造成问题,而不是在MouseArea内部进行双重点击。 例如,如果用户首先通过双重点击来尽量扩大窗口,然后通过在MouseArea内部的指令而援引<条码>启动SystemMove(的功能,则窗口将尽量缩小其规模,但<条码>将留守<>true<>>>>>。 造成下一个两点倒置,不去尽量扩大窗口。

  2. 最好是在MouseArea内部使用DragHandler而不是>,因为当用户实际上开始绕过窗户,而不是仅仅在MouseArea内点击时,这将会援引<条码>。

在这方面,是执行我上述内容的“无疆界的”小 window:

import QtQuick
import QtQuick.Controls

ApplicationWindow {
    id: root
    width: 1280
    height: 720
    visible: true
    flags: Qt.Window | Qt.FramelessWindowHint

    // Menu bar in top portion of the window, with a visible frame around it
    menuBar: Frame {
        height: 50

        // Allow window dragging when inside menu bar
        Item {
            anchors.fill: parent
            DragHandler {
                id: handler
                onActiveChanged: if (active) root.startSystemMove()
            }
        }

        // Allow fullscreen <-> minimized switching
        MouseArea {
            anchors.fill: parent
            onDoubleClicked: {
                root.visibility = (root.visibility != Qt.WindowFullScreen) ? Qt.WindowFullScreen : Qt.WindowMinimized
            }
        }

    }




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?