QtQuick2拖动无框窗口

时间:2013-09-21 00:04:06

标签: qml qt5 qtquick2

我正在寻找一种在QtQuick2中拖动无框窗口的方法。 我在论坛Link上关注了这个帖子,但它给了我一个错误。

代码的主要区别在于我的代码使用QtQuick2ApplicationViewer而不是QmlApplicationViewer,看起来QtQuick2ApplicationViewer没有“.pos”属性。

这是我的main.cpp

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlContext>

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

    QtQuick2ApplicationViewer viewer;
    viewer.rootContext()->setContextProperty("QmlApplicationViewer", (QObject *)&viewer);
    viewer.setFlags(Qt::FramelessWindowHint);
    viewer.setMainQmlFile(QStringLiteral("qml/ubusell/main.qml"));
    viewer.showExpanded();

    return app.exec();
}

这是我的main.qml

的一部分
MouseArea {
    id: mouseRegion
    anchors.fill: parent;
    property variant clickPos: "1,1"

        onPressed: {
            clickPos  = Qt.point(mouse.x,mouse.y)
        }

        onPositionChanged: {
            var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
            print(QmlApplicationViewer.pos)
            QmlApplicationViewer.pos = (20,20)
            QmlApplicationViewer.pos = Qt.point(QmlApplicationViewer.pos.x+delta.x,
                              QmlApplicationViewer.pos.y+delta.y)
        }
}

当我尝试拖动窗口时出现此错误:

  

TypeError:无法读取未定义的属性“x”

有什么想法吗? 甚至可以使用QtQuick2吗? 谢谢你的帮助!

4 个答案:

答案 0 :(得分:14)

在我的项目中,我做了:

property variant clickPos: "1,1"

onPressed: {
    clickPos  = Qt.point(mouse.x,mouse.y)
}

onPositionChanged: {
    var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
    rootWindow.x += delta.x;
    rootWindow.y += delta.y;
}

MouseArea

答案 1 :(得分:4)

同样类似于在窗口垂直边缘上方拖动窗口时最大化窗口的Windows行为:

MouseArea {
    anchors.fill: parent;
    property variant clickPos: "1,1"

    onPressed: {
        clickPos = Qt.point(mouse.x,mouse.y)
    }

    onPositionChanged: {
        var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
        var new_x = mainWindow.x + delta.x
        var new_y = mainWindow.y + delta.y
        if (new_y <= 0)
            mainWindow.visibility = Window.Maximized
        else
        {
            if (mainWindow.visibility === Window.Maximized)
                mainWindow.visibility = Window.Windowed
            mainWindow.x = new_x
            mainWindow.y = new_y
        }
    }
}

答案 2 :(得分:1)

我按照以下方式完成:

Window {
    id: window
    height: 400
    width: 250
    x: (Screen.width - width)/2     //<---start position of window
    y: (Screen.height - height)/2   //<-┘
    color: "transparent"
    flags: Qt.MSWindowsFixedSizeDialogHint | Qt.FramelessWindowHint

    MouseArea {
        anchors.fill: parent
        property point lastMousePos: Qt.point(0, 0)
        onPressed: { lastMousePos = Qt.point(mouseX, mouseY); }
        onMouseXChanged: window.x += (mouseX - lastMousePos.x)
        onMouseYChanged: window.y += (mouseY - lastMousePos.y)
    }

    Item {
        id: myFirstPage
        anchors.fill: parent
        anchors.topMargin: 50
        //...
        //This item can have some mouse area
    }
}

现在,您可以使用拖动前50个像素或任何不在任何MouseArea下的位置移动窗口。

答案 3 :(得分:0)

一个相当完整的示例:

  import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../sharePreference.dart';
import 'login.dart';
import 'login.dart';

class HomePage extends StatefulWidget {

  String user_name;
  final api_token;
  final id ;
 final  List<dynamic> notification ;
  // List data ;
  HomePage({ this.user_name, this.api_token , this.id, this.notification });


  @override
  _HomePageState createState() => _HomePageState();
}



class _HomePageState extends State<HomePage> {


  String nametoprint;
  String tokentoprint;

  @override
  void initState() {
    super.initState();
    Future name = SharedPrefrence().getName();
    name.then((data) async {
      nametoprint = data;
      print(nametoprint);
    });

    Future token= SharedPrefrence().getToken();
    token.then((data) async {
      tokentoprint= data;
      print(tokentoprint);
    });
  }



  int counter = 0;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Cash-Management"),
          backgroundColor: Colors.blue,
          actions: [

            new Stack(
              children: <Widget>[
                new IconButton(icon: Icon(Icons.notifications), onPressed: () {
                  setState(() {
                    counter = 0;
                  });
                }),
                counter != 0 ? new Positioned(
                  right: 11,
                  top: 11,
                  child: new Container(
                    padding: EdgeInsets.all(2),
                    decoration: new BoxDecoration(
                      color: Colors.red,
                      borderRadius: BorderRadius.circular(6),
                    ),
                    constraints: BoxConstraints(
                      minWidth: 14,
                      minHeight: 14,
                    ),
                    child: Text(
                      '$counter',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 8,
                      ),
                      textAlign: TextAlign.center,
                    ),
                  ),
                ) : new Container()
              ],
            ),
            IconButton(
                icon: Icon(Icons.exit_to_app),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => Login()),
                  );
                }),
          ],
        ),
        body: ListView(
          children: <Widget>[
            Container(
              height: 200,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Text(
                    "${widget.user_name}",
                    // "  ${widget.username} ",
                    style: TextStyle(fontSize: 16),
                  ),
                  Text(
                    "${widget.api_token}",
                    // "  ${widget.username} ",
                    style: TextStyle(fontSize: 16),
                  ),
                  Text(
                    "${widget.id}",
                    // "  ${widget.username} ",
                    style: TextStyle(fontSize: 16),
                  ),
                  Text(" ${nametoprint} "),
                  Text("$nametoprint"),
                  Text("$tokentoprint"),
                ],
              ),
            ),

Container(
  color: Colors.blue,
  height: 300,
  child:   ListView.builder(
      itemCount: widget.notification == null ?  0 : widget.notification.length,
      itemBuilder: (context, index){
        return ListTile(
          title: Text(widget.notification[index] ["id"]),
          subtitle: Text(widget.notification[index]["type"]),

        );
      }),
),



          ],
        ),

        floatingActionButton: FloatingActionButton(onPressed: () {
          print("Increment Counter");
          setState(() {
            counter++;
          });
        }, child: Icon(Icons.add),),
      ),
    );
  }
}