QML之窗口(无边框、透明及拖拽)
1.无边框
Qt Quick 2.0 中 QQuickView代替了1.0中的QDeclarativeView。
无边框窗口代码如下:
QQuickView viwer;//QQuickView继承自QWindow而不是QWidgetviwer.setFlags(Qt::FramelessWindowHint);
2.窗口透明
setOpacity可设置整个窗口(包括控件)的透明度,而背景透明则应使用setColor
//设置窗口颜色,以下为透明,在viwer.setSource()之前使用 viwer.setColor(QColor(Qt::transparent)); //QWidget用setAttribute(Qt::WA_TranslucentBackground,true)
3.拖拽窗口
拖拽窗口需要将窗口(viewer)设置为qml中的属性
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之窗口(无边框、透明及拖拽)的更多相关文章
- winform 窗体设置成无边框、可拖拽、四周圆角
最近做一个及时通讯系统的登录界面,现在将界面用到的无边框.可拖拽.四周圆角的方法分享如下: 1.无边框的窗体: 把FormBorderStyle的属性设置为none 2.可拖拽:private Poi ...
- [Winform]无边框窗口悬浮右下角并可以拖拽移动
摘要 简单实现了一个这样的功能,程序启动时,窗口悬固定在右下角,并可以通过鼠标拖拽移动. 核心代码块 无边框窗口并不出现在任务栏 //无边框 this.FormBorderStyle = System ...
- Qt5.3中qml ApplicationWindow设置窗口无边框问题
这个版本的qt在这里有点bug.. 设置ApplicationWindow的flags属性为Qt.FramelessWindowHint的确可以使程序无边框,但是同时程序在任务栏的图标也没了. 看文档 ...
- C# WinForm设置窗口无边框、窗口可移动、窗口显示在屏幕中央、控件去边框
1)窗口去除边框 在组件属性中FormBorderStyle设为None 2)窗口随着鼠标移动而动 添加引用using System.Runtime.InteropServices; 在初始化控件{I ...
- WPF 无边框透明按钮
在实际开发过程中,有时候要设置一个无边框的按钮,或者无边框的透明按钮. 按钮效果如下: 1.当你应用telerik组件中的Button时,这个直接就可以设置 telerik:StyleManager. ...
- WPF 无边框透明窗体
WindowStyle="None"--无边框,如果需要其它按钮,如缩小.放大.收缩.关闭按钮,可以自定义 AllowsTransparency="True"- ...
- 2017年11月20日 WinForm窗体 窗口无边框可移动&&窗口阴影 控制窗口关闭/最小化
弹框 MessageBox.Show(); 清空 clear() 字符串拼接 string 公共控件 button 按钮 checkbox 复选框 checklistbox 多个复选框 combobo ...
- C# 实现窗口无边框,可拖动效果
#region 无边框拖动效果 [DllImport("user32.dll")]//拖动无窗体的控件 public static extern bool ReleaseCaptu ...
- pyqt5 窗口无边框和透明
https://blog.csdn.net/FanMLei/article/details/79433229 按钮圆形方法属性border-radius:30px; QScrollArea 无法滚动用 ...
随机推荐
- asp.net(C#)写SQL语句技巧
/*添加SQL*/string fields = "";string values = "";fields += "xm"; values ...
- 转:CodeCube提供可共享、可运行的代码示例
CodeCube是一个新服务和开源项目,旨在让开发者能够通过浏览器以一种安全的方式分享并运行代码示例从而提升协作. 最初发布的服务可以从codecube.io上获取,支持Ruby.Python.Go及 ...
- WPS怎样设置多级标题(如四级标题)
WPS期初,乍一看最多只能设置三级标题. 怎样设置四级以上标题呢? 这里以设置四级标题为例: 点击‘视图’->'大纲', 假如,现在2.3.3这一行是三级标题: 在下拉框里选择‘4级’就可以了. ...
- C#程序设计基础——数据类型
C#是强类型语言,因此每个变量和对象都必须具有声明类型.数据类型可描述为内置数据类型(如int或char),用户定义数据类型(如class或interface).数据类型还可以定义为值类型(用于存储值 ...
- 【HDOJ】3587 NUDOTA
字符串模拟水题. /* 3587 */ #include <iostream> #include <cstdio> #include <cstring> #incl ...
- 【HDOJ】1063 Exponentiation
这道题目莫名其妙的wa,又莫名其妙的过了. import java.util.Scanner; import java.math.BigDecimal; public class Main { pub ...
- HBase HFileBlock
HFileBlock官方源码注释: Reading HFile version 1 and 2 blocks, and writing version 2 blocks. In version 1 ...
- HBase HFile
HFile index HFile index, which is proportional to the total number of Data Blocks. The total amount ...
- L1签证_百度百科
L1签证_百度百科 L1签证
- 安卓kernel自主唤醒系统方法—设置alarm
我们知道alarm一般是上层设置后.kernel驱动里面将set time写入alarm寄存器,到达时间后trigger,殊不知kernel里面也能够自主设置alarm. 尤其是系统在休眠时驱动nee ...