1.无边框

Qt Quick 2.0 中 QQuickView代替了1.0中的QDeclarativeView。
无边框窗口代码如下:

  1. QQuickView viwer;
  2. //QQuickView继承自QWindow而不是QWidget
  3. viwer.setFlags(Qt::FramelessWindowHint);

2.窗口透明

setOpacity可设置整个窗口(包括控件)的透明度,而背景透明则应使用setColor

//设置窗口颜色,以下为透明,在viwer.setSource()之前使用

viwer.setColor(QColor(Qt::transparent));

//QWidget用setAttribute(Qt::WA_TranslucentBackground,true)

3.拖拽窗口

拖拽窗口需要将窗口(viewer)设置为qml中的属性

  1. viwer.rootContext()->setContextProperty("mainwindow",&viwer);

main.cpp如下

/*---main.cpp---*/
#include<QApplication>
#include<QQuickView>
#include<QColor>
#include<QQmlContext>
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
QQuickView viwer;
//无边框,背景透明
viwer.setFlags(Qt::FramelessWindowHint);
viwer.setColor(QColor(Qt::transparent));
//加载qml,qml添加到资源文件中可避免qml暴露
viwer.setSource(QUrl("qrc:/qml/main.qml"));
viwer.show();
//将viewer设置为main.qml属性
viwer.rootContext()->setContextProperty("mainwindow",&viwer);
return app.exec();
}

此时,main.qml如下即可实现透明,无边框,可拖拽

/*--main.qml--*/
importQtQuick2.
Rectangle{
width:
height:
//灰色0.9透明度
color:Qt.rgba(0.5,0.5,0.5,0.9)
MouseArea{
id: dragRegion
anchors.fill: parent
property point clickPos:"0,0"
onPressed:{
clickPos =Qt.point(mouse.x,mouse.y)
}
onPositionChanged:{
//鼠标偏移量
var delta =Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
//如果mainwindow继承自QWidget,用setPos
mainwindow.setX(mainwindow.x+delta.x)
mainwindow.setY(mainwindow.y+delta.y)
}
}
}

效果如下:

添加关闭按钮

importQtQuick2.
Rectangle{
width:
height:
//灰色0.9透明度
color:Qt.rgba(0.5,0.5,0.5,0.9)
MouseArea{
id: dragRegion
anchors.fill: parent
property point clickPos:"0,0"
onPressed:{
clickPos =Qt.point(mouse.x,mouse.y)
}
onPositionChanged:{
//鼠标偏移量
var delta =Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
//如果mainwindow继承自QWidget,用setPos
mainwindow.setX(mainwindow.x+delta.x)
mainwindow.setY(mainwindow.y+delta.y)
}
}
//要置于MouseArea之后,否则无法响应鼠标点击
Rectangle{
id:closeBtn
height:
width:
anchors.right: parent.right
anchors.rightMargin:
anchors.top: parent.top
anchors.topMargin:
color:"#aaff0000"
Text{
text:"x"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked:
{
//Qt.quit()无法关闭窗口
mainwindow.close()
}
}
}
}

QML之窗口(无边框、透明及拖拽)的更多相关文章

  1. winform 窗体设置成无边框、可拖拽、四周圆角

    最近做一个及时通讯系统的登录界面,现在将界面用到的无边框.可拖拽.四周圆角的方法分享如下: 1.无边框的窗体: 把FormBorderStyle的属性设置为none 2.可拖拽:private Poi ...

  2. [Winform]无边框窗口悬浮右下角并可以拖拽移动

    摘要 简单实现了一个这样的功能,程序启动时,窗口悬固定在右下角,并可以通过鼠标拖拽移动. 核心代码块 无边框窗口并不出现在任务栏 //无边框 this.FormBorderStyle = System ...

  3. Qt5.3中qml ApplicationWindow设置窗口无边框问题

    这个版本的qt在这里有点bug.. 设置ApplicationWindow的flags属性为Qt.FramelessWindowHint的确可以使程序无边框,但是同时程序在任务栏的图标也没了. 看文档 ...

  4. C# WinForm设置窗口无边框、窗口可移动、窗口显示在屏幕中央、控件去边框

    1)窗口去除边框 在组件属性中FormBorderStyle设为None 2)窗口随着鼠标移动而动 添加引用using System.Runtime.InteropServices; 在初始化控件{I ...

  5. WPF 无边框透明按钮

    在实际开发过程中,有时候要设置一个无边框的按钮,或者无边框的透明按钮. 按钮效果如下: 1.当你应用telerik组件中的Button时,这个直接就可以设置 telerik:StyleManager. ...

  6. WPF 无边框透明窗体

    WindowStyle="None"--无边框,如果需要其它按钮,如缩小.放大.收缩.关闭按钮,可以自定义 AllowsTransparency="True"- ...

  7. 2017年11月20日 WinForm窗体 窗口无边框可移动&&窗口阴影 控制窗口关闭/最小化

    弹框 MessageBox.Show(); 清空 clear() 字符串拼接 string 公共控件 button 按钮 checkbox 复选框 checklistbox 多个复选框 combobo ...

  8. C# 实现窗口无边框,可拖动效果

    #region 无边框拖动效果 [DllImport("user32.dll")]//拖动无窗体的控件 public static extern bool ReleaseCaptu ...

  9. pyqt5 窗口无边框和透明

    https://blog.csdn.net/FanMLei/article/details/79433229 按钮圆形方法属性border-radius:30px; QScrollArea 无法滚动用 ...

随机推荐

  1. Java中抽象类和接口的用法和区别

    一.抽象类 1.抽象类 包含一个抽象方法的类就是抽象类 2.抽象方法 声明而未被实现的方法,抽象方法必须使用abstract关键词字声明 public abstract class People { ...

  2. 温故而知新 C++基本类型

    C++基本类型大小: 在32位计算机中测试得到:sizeof(bool) == 1sizeof(char) == 1 sizeof(short) == 2sizeof(int) == 4sizeof( ...

  3. 业内人士详述SIEM建设的演进过程

    http://www.verydemo.com/demo_c289_i22006.html 4A http://www.verydemo.com/demo_c281_i40888.html 从SIEM ...

  4. 程序自动生成Dump文件()

    前言:通过drwtsn32.NTSD.CDB等调试工具生成Dump文件, drwtsn32存在的缺点虽然NTSD.CDB可以完全解决,但并不是所有的操作系统中都安装了NTSD.CDB等调试工具.了解了 ...

  5. BZOJ1677: [Usaco2005 Jan]Sumsets 求和

    1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 570  Solved: 310[Submi ...

  6. 【转】在linux内核中读写文件 -- 不错

    原文网址:http://blog.csdn.net/tommy_wxie/article/details/8194276 1. 序曲 在用户态,读写文件可以通过read和write这两个系统调用来完成 ...

  7. jsoup web scraping

    jsoup简介 jsoup是一款HTML解析器,可用与解析URL地址.HTML文本内同等,操作类似于jQuery,可通过DOM查找数据,操作数据, 使用时需引入jsoup jar jsoup可以从包含 ...

  8. 64位CentOS安装32位开发环境编译Nachos

    参考http://stackoverflow.com/questions/7412548/gnu-stubs-32-h-no-such-file-or-directory 1.On CentOS 5. ...

  9. Java中Websocket使用实例解读

    介绍 现在很多网站为了实现即时通讯,所用的技术都是轮询(polling).轮询是在特定的的时间间隔(如每1秒),由浏览器对服务器发出HTTP request,然后由服务器返回最新的数据给客服端的浏览器 ...

  10. 如何计算Java对象占用了多少空间?

    在Java中没有sizeof运算符,所以没办法知道一个对象到底占用了多大的空间,但是在分配对象的时候会有一些基本的规则,我们根据这些规则大致能判断出来对象大小. 对象头 对象的头部至少有两个WORD, ...