当前位置: 代码迷 >> 综合 >> QML中FRAMELESS 和 transparent 窗体的拖动实现
  详细解决方案

QML中FRAMELESS 和 transparent 窗体的拖动实现

热度:97   发布时间:2024-01-22 06:21:57.0

之前的blog中写到如何设置窗体无边框以及设置背景透明,参考Qt中设置QML窗体无边框和背景透明

因为,窗体设置成无边框之后,窗体的拖动就要靠自己写的程序来完成了!

本文参考Resize Qml window这篇文章,结合自己的需求解决了拖动窗体的需要!

 首先,介绍上面这个参考文章里的解决方法:

核心思想是:

在main中引入QMainWindow对象,将qml文件作为该对象的widget,并将该对象注册到qml中,然后再qml中通过识别鼠标的位移来更改这个mainwindow的pos属性。

 

实现:

1、win.cpp:

#include <QApplication>  #include <QDeclarativeView>  #include <QMainWindow>  #include <QDeclarativeContext>    int main(int argc, char *argv[])  {
          QApplication app(argc, argv);        QMainWindow window;        QDeclarativeView* v = new QDeclarativeView;      window.setCentralWidget(v);        v->setSource(QUrl::fromLocalFile(("draw_rectangles.qml")));           // expose window object to QML      v->rootContext()->setContextProperty