Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1037220
  • 博文数量: 155
  • 博客积分: 5339
  • 博客等级: 大校
  • 技术积分: 1436
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-10 21:41
文章分类

全部博文(155)

文章存档

2016年(3)

2015年(7)

2014年(3)

2013年(1)

2012年(8)

2011年(5)

2010年(1)

2009年(5)

2008年(4)

2007年(26)

2006年(46)

2005年(46)

分类: C/C++

2014-11-04 10:21:31

开发环境:Win8.1  Qt5.2.1
1.无边框  
   Qt Quick 2.0 中 QQuickView代替了1.0中的QDeclarativeView。
无边框窗口代码如下:
点击(此处)折叠或打开
  1. QQuickView viwer;
  2. //QQuickView继承自QWindow而不是QWidget
  3. viwer.setFlags(Qt::FramelessWindowHint);

   这样窗口实现了无边框,但是程序将不会依附在任务栏,如果想同时无边框且图标依附到任务栏,则应该如下设置:
点击(此处)折叠或打开
  1. setWindowFlags(Qt::Window|Qt::FramelessWindowHint);

2.窗口透明
   setOpacity可设置整个窗口(包括控件)的透明度,而背景透明则应使用setColor
点击(此处)折叠或打开
  1. //设置窗口颜色,以下为透明,在viwer.setSource()之前使用
  2. viwer.setColor(QColor(Qt::transparent));
  3. //QWidget用setAttribute(Qt::WA_TranslucentBackground,true)

3.拖拽窗口
   拖拽窗口需要将窗口(viewer)设置为qml中的属性
点击(此处)折叠或打开
  1. viwer.rootContext()->setContextProperty("mainwindow",&viwer);

   main.cpp如下
点击(此处)折叠或打开
  1. /*---main.cpp---*/
  2. #include<QApplication>
  3. #include<QQuickView>
  4. #include<QColor>
  5. #include<QQmlContext>
  6. int main(int argc,char* argv[])
  7. {
  8.     QApplication app(argc,argv);
  9.     QQuickView viwer;
  10.     //无边框,背景透明
  11.     viwer.setFlags(Qt::FramelessWindowHint);
  12.     viwer.setColor(QColor(Qt::transparent));
  13.     //加载qml,qml添加到资源文件中可避免qml暴露
  14.     viwer.setSource(QUrl("qrc:/qml/main.qml"));
  15.     viwer.show();
  16.     //将viewer设置为main.qml属性
  17.     viwer.rootContext()->setContextProperty("mainwindow",&viwer);
  18.     return app.exec();
  19. }

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

点击(此处)折叠或打开
  1. /*--main.qml--*/
  2. import QtQuick 2.0
  3. Rectangle {
  4.     width: 300
  5.     height: 200
  6.                                                                                                                                                               
  7.     //灰色0.9透明度
  8.     color:Qt.rgba(0.5,0.5,0.5,0.9)
  9.     MouseArea {
  10.         id: dragRegion
  11.         anchors.fill: parent
  12.         property point clickPos: "0,0"
  13.         onPressed: {
  14.             clickPos = Qt.point(mouse.x,mouse.y)
  15.             }
  16.         onPositionChanged: {
  17.         //鼠标偏移量
  18.         var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
  19.         //如果mainwindow继承自QWidget,用setPos
  20.         mainwindow.setX(mainwindow.x+delta.x)
  21.         mainwindow.setY(mainwindow.y+delta.y)
  22.         }
  23.     }
  24. }

   效果如下:
添加关闭按钮

点击(此处)折叠或打开
  1. import QtQuick 2.0
  2. Rectangle {
  3.     width: 300
  4.     height: 200
  5.     //灰色0.9透明度
  6.     color:Qt.rgba(0.5,0.5,0.5,0.9)
  7.     MouseArea {
  8.         id: dragRegion
  9.         anchors.fill: parent
  10.         property point clickPos: "0,0"
  11.         onPressed: {
  12.             clickPos = Qt.point(mouse.x,mouse.y)
  13.             }
  14.         onPositionChanged: {
  15.         //鼠标偏移量
  16.         var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
  17.         //如果mainwindow继承自QWidget,用setPos
  18.         mainwindow.setX(mainwindow.x+delta.x)
  19.         mainwindow.setY(mainwindow.y+delta.y)
  20.         }
  21.     }
  22.     //要置于MouseArea之后,否则无法响应鼠标点击
  23.     Rectangle{
  24.         id:closeBtn
  25.         height: 25
  26.         width: 25
  27.         anchors.right: parent.right
  28.         anchors.rightMargin: 5
  29.         anchors.top: parent.top
  30.         anchors.topMargin: 5
  31.         color:"#aaff0000"
  32.         Text{
  33.             text:"x"
  34.             anchors.centerIn: parent
  35.         }
  36.         MouseArea{
  37.             anchors.fill: parent
  38.             onClicked:
  39.             {
  40.                 //Qt.quit()无法关闭窗口
  41.                 mainwindow.close()
  42.             }
  43.         }
  44.     }
  45. }


运行效果如图:

本文出自 “大耳” 博客,请务必保留此出处http://cpp51.blog.51cto.com/5346598/1408999
阅读(12092) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~