1
2
3
|
QQuickView viwer;
//QQuickView继承自QWindow而不是QWidget
viwer.setFlags(Qt::FramelessWindowHint);
|
1
|
setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
|
1
2
3
|
//设置窗口颜色,以下为透明,在viwer.setSource()之前使用
viwer.setColor(QColor(Qt::transparent));
//QWidget用setAttribute(Qt::WA_TranslucentBackground,true)
|
1
|
viwer.rootContext()->setContextProperty(
"mainwindow"
,&viwer);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/*---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();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/*---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
viwer.setSource(QUrl(
"qrc:/qml/main.qml"
));
viwer.show();
//将viewer设置为main.qml属性
viwer.rootContext()->setContextProperty(
"mainwindow"
,&viwer);
return
app.exec();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/*--main.qml--*/
import
QtQuick
2.0
Rectangle {
width:
300
height:
200
//灰色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)
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import QtQuick 2.0
Rectangle {
width: 300
height: 200
//灰色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: 25
width: 25
anchors.right: parent.right
anchors.rightMargin: 5
anchors.top: parent.top
anchors.topMargin: 5
color:
"#aaff0000"
Text{
text:
"x"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked:
{
//Qt.quit()无法关闭窗口
mainwindow.close()
}
}
}
}
|