QML无边框窗体怎么能调整大小?



  • 具体来说就是,窗口右下角那个东西用来调整大小的,但是无边框之后那个东西没有了

    ApplicationWindow {
        flags:  Qt.Window|Qt.FramelessWindowHint
    }
    

    无边框是这样设置的


  • 网站研运

    @hezhongfeng 还是可以通过MouseArea捕捉鼠标按下事件,然后借此通过修改ApplicationWindow的width和height可以制作更改窗口大小的小功能。



  • @jcy
    明白了,看来只能自己弄,并没有原生部件提供.Thanks



  • @hezhongfeng 写了一些代码。如下。希望能够提供一些思路给你。

    如下是: BackgroundItem.qml

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    
    Item {
        id: backgoundItem
        property ApplicationWindow mainWindow: null
        property real windowBorderWidth: 10
    
        readonly property rect leftTop:  Qt.rect(0, 0, windowBorderWidth, windowBorderWidth) // left top
        readonly property rect leftSide: Qt.rect(0, windowBorderWidth, windowBorderWidth, backgoundItem.height - 2 * windowBorderWidth) // left side
        readonly property rect leftBottom: Qt.rect(0, backgoundItem.height-windowBorderWidth, windowBorderWidth, windowBorderWidth) // left bottom
        
        readonly property rect topSide: Qt.rect(windowBorderWidth, 0, backgoundItem.width-2*windowBorderWidth, windowBorderWidth) // top side
        readonly property rect center: Qt.rect(windowBorderWidth, windowBorderWidth, backgoundItem.width-2*windowBorderWidth, backgoundItem.height - 2 * windowBorderWidth) // center
        readonly property rect bottomSide: Qt.rect(windowBorderWidth, backgoundItem.height-windowBorderWidth,
                                                   backgoundItem.width-2*windowBorderWidth,  backgoundItem.width-2*windowBorderWidth) // bottom
        
        readonly property rect rightTop: Qt.rect(backgoundItem.width-windowBorderWidth, 0, windowBorderWidth, windowBorderWidth) // right top
        readonly property rect rightSide: Qt.rect(backgoundItem.width-windowBorderWidth, windowBorderWidth, windowBorderWidth, backgoundItem.height - 2 * windowBorderWidth) // right side
        readonly property rect rightBottom: Qt.rect(backgoundItem.width-windowBorderWidth, backgoundItem.height-windowBorderWidth, windowBorderWidth, windowBorderWidth) // left bottom
    
        Rectangle {
            anchors.fill: parent
            // anchors.margins: windowBorderWidth
            color: "white"
            border.width: 1
            border.color: "#ccc"
            // 最底层
        }
    
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            property point oldPosition
    
            Component.onCompleted: {
                oldPosition = Qt.point(mouseX, mouseY);
            }
    
            onPositionChanged: {
                var currentPosition = Qt.point(mouse.x, mouse.y);
    
                var p = Qt.point(mouse.x, mouse.y);
    
                if(rectContains(leftTop, p) || rectContains(rightBottom, p)) {
                    cursorShape = Qt.SizeFDiagCursor;
                    if(pressed) {
    
    //                    mainWindow.x = mouse.x;
    //                    mainWindow.y = mouse.y;
                        var height_d = currentPosition.y - oldPosition.y;
                        var width_d =  currentPosition.x - oldPosition.x;
                        mainWindow.x += width_d
                         mainWindow.y += height_d
                        mainWindow.width += width_d;
                        mainWindow.height += height_d;
    
    
                        //console.log("pressed")
    
                    }
    
                    return;
                } else if(rectContains(leftBottom, p) || rectContains(rightTop, p)) {
                    cursorShape = Qt.SizeBDiagCursor;
                    return;
                } else if(rectContains(topSide, p) || rectContains(bottomSide, p)) {
                    cursorShape = Qt.SizeVerCursor;
                    return;
                } else if(rectContains(rightSide, p) || rectContains(leftSide, p)) {
                    cursorShape = Qt.SizeHorCursor;
                    return;
                }
    
                cursorShape = Qt.ArrowCursor;
                oldPosition = Qt.point(mouse.x, mouse.y);
            }
    
    
        }
        function rectContains(rect, point) {
            var x_d = point.x - rect.x;
            var y_d = point.y - rect.y;
    
            return (x_d > 0 && x_d < rect.width)
                    && (y_d > 0 && y_d < rect.height);
        }
    }
    

    main.qml 中使用:

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Controls.Styles 1.4
    
    
    ApplicationWindow {
        id: app
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        flags:  Qt.Window|Qt.FramelessWindowHint
    
        property real windowBorderWidth: 10
    
        style: ApplicationWindowStyle {
            background:
                BackgoundItem {
                id: backgoundItem
                 windowBorderWidth: app.windowBorderWidth
                 mainWindow: app
            }
        }
    
        Rectangle {
            anchors.fill: parent
            anchors.margins: 10
            color: "yellow"
            focus: true
            Keys.onEscapePressed: Qt.quit()
        }
    }
    

    这样。当鼠标在窗体边上时,就是改变鼠标图标,然后实现一些逻辑即可。不过上诉代码仅仅只是思路,具体的代码逻辑需要自行解决。



  • @qyvlikQML无边框窗体怎么能调整大小? 中说:

    就是改变鼠标图标,然后实现一些逻辑即可

    谢谢。
    在官方论坛也问了,基本还是得自己做。
    思路和这个差不多
    目前还是学习阶段



  • @hezhongfeng 你这个问题解决了吗?我也在寻求相关的资料。菜鸟表示很头疼


Log in to reply
 

走马观花

最近的回复

  • 萌梦 男孩,女孩,和蛋

    menghome.png

    read more
  • 设计模式-工厂模式

    使用qt/qml来演示设计模式效果,便于学习理解

    1)定义创建对象的接口,封装对象的创建
    2)使具体化类的工作延迟到工厂子类中

    bg.png
    image.png

    1. 工厂类

    createProduct使用了参数来选择要创建哪个产品

    #ifndef FACTORY_H #define FACTORY_H #include <QObject> class Product; class QString; class Factory: public QObject { Q_OBJECT public: virtual ~Factory() = 0; virtual Product* createProduct(QString type) = 0; protected: Factory(); }; class ConcreteFactory: public Factory { Q_OBJECT public: ~ConcreteFactory(); ConcreteFactory(); public slots: Product* createProduct(QString type); }; #endif // FACTORY_H #include "factory.h" #include "product.h" #include <QtQml/qqml.h> Factory::~Factory() { } Factory::Factory() { qmlRegisterType<Product>("Product", 1, 0, "Product"); } ConcreteFactory::~ConcreteFactory() { } ConcreteFactory::ConcreteFactory() { } Product *ConcreteFactory::createProduct(QString type) { if(type == "boy") return static_cast<Product *>(new ConcreteProduct1()); else if(type == "girl") return static_cast<Product *>(new ConcreteProduct2()); return static_cast<Product *>(new ConcreteProduct1()); } 2 产品类

    一个产品是萌梦男,一个产品是萌梦女

    #ifndef PRODUCT_H #define PRODUCT_H #include <QObject> class Product: public QObject { Q_OBJECT public: virtual ~Product() = 0; Q_PROPERTY(QString icon READ icon NOTIFY iconChanged) QString m_icon; QString icon() const { return m_icon; } signals: void iconChanged(QString icon); protected: Product(); signals: public slots: }; class ConcreteProduct1: public Product { Q_OBJECT public: ~ConcreteProduct1(); ConcreteProduct1(); }; class ConcreteProduct2: public Product { Q_OBJECT public: ~ConcreteProduct2(); ConcreteProduct2(); }; #endif // PRODUCT_H #include "product.h" Product::~Product() { } Product::Product() { } ConcreteProduct1::~ConcreteProduct1() { } ConcreteProduct1::ConcreteProduct1() { m_icon = "qrc:/images/boy.png"; } ConcreteProduct2::~ConcreteProduct2() { } ConcreteProduct2::ConcreteProduct2() { m_icon = "qrc:/images/girl.png"; } 3. main.qml

    使用timer,canvas,listview等实现一个自动化生产的动画效果

    源代码

    Fork me on Gitee

    read more
  • blender建模 章鱼
    捕获2.PNG 😵

    read more

关注我们

微博
QQ群