社区应用 最新帖子 精华区 社区服务 会员列表 统计排行
主题 : Qt中不规则窗体和部件的实现
XChinux 离线
级别: 总版主
显示用户信息 
0  发表于: 2007-08-06  
来源于 原创 分类

Qt中不规则窗体和部件的实现

管理提醒: 本帖被 XChinux 执行加亮操作(2007-08-06)
Qt中不规则窗体和部件的实现,不管是窗体还是部件,都是从QWidget派生而来的,所以它们的原理是一样的,都是设置部件的背景色或者按钮图标等为一个图像A,然后设置此部件的掩码为另一个图像B,这个图像B经过位与运算后,得到不规则的图像,这就行了下面,是一个简单的示例:
form.h:
引用

#ifndef  FORM_H
#define  FORM_H

#include <QtGui/QWidget>

class Form : public QWidget
{
    Q_OBJECT
public:
    Form();
    ~Form();
};

#endif


form.cpp:
引用

#include <QtGui/QPixmap>
#include <QtGui/QBitmap>
#include "toolbutton.h"
#include "form.h"

Form::Form()
{
    QPalette p = palette();
    QPixmap img("a.png");
    QBitmap mask("mask.png");
    p.setBrush(QPalette::Window, QBrush(img));
    setPalette(p);
    setMask(mask);
    setWindowFlags(Qt::FramelessWindowHint);

    resize(600, 400);
   
    ToolButton *button = new ToolButton(this);
    button->move(200, 200);
}

Form::~Form()
{
}


toolbutton.h:
引用

#ifndef  TOOLBUTTON_H
#define  TOOLBUTTON_H

#include <QtGui/QToolButton>

class ToolButton : public QToolButton
{
    Q_OBJECT
public:
    ToolButton(QWidget *parent=0);
    ~ToolButton();
};

#endif


toolbutton.cpp:
引用

#include <QtGui/QPixmap>
#include <QtGui/QBitmap>
#include "toolbutton.h"

ToolButton::ToolButton(QWidget *parent)
    : QToolButton(parent)
{
    /*
    // 如果是非按钮类部件,则使用此段代码
    QPalette p = palette();
    p.setBrush(QPalette::Button, QBrush(QPixmap("toolbutton.png")));
    setPalette(p);
    /*/
    // 如果是按钮类部件,则使用此段代码
    setIcon(QIcon("toolbutton.png"));
    setIconSize(QSize(48, 48));
    //*********************************/
    setMask(QBitmap("toolbutton_mask.png"));

    setFixedSize(48, 48);
}

ToolButton::~ToolButton()
{
}


main.cpp:
引用

#include <QtGui/QApplication>
#include "form.h"

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    Form form;
    form.show();
    return app.exec();
}
[ 此贴被XChinux在2007-08-07 00:25重新编辑 ]
描述:a.png    窗口背景图
图片:a.png
描述:mask.png    窗口掩码图
图片:mask.png
描述:toolbutton.png    按钮背景图
图片:toolbutton.png
描述:toolbutton_mask.png    按钮掩码图
图片:toolbutton_mask.png
描述:runtime.png    示例运行时图
图片:runtime.png
有PHP、Qt、用友相关开发业务相商的朋友请发电子邮件到XChinux@163.com
有技术问题的朋友请在论坛里发贴提问,不要通过私人短信或者QQ/MSN提问讨论,谢谢合作

邮件:  XChinux@163.com
网址: http://www.qtcn.org
博客: http://hi.baidu.com/xchinux
操作系统: 红旗桌面版6, 红旗工作站5, Mandriva 2009
主要开发方向: C++/Qt程序开发、PHP/Apache/MySQL/PostgreSQL程序开发、用友相关二次开发
经常使用的工具: Opera/Firefox, Vim, OpenOffice/RedOffice, 二笔输入法
jzj139 离线
级别: 侠客
显示用户信息 
1  发表于: 2007-08-06  
真实的开发中的其实有着很多需要解决的难题的
qt
XChinux 离线
级别: 总版主
显示用户信息 
2  发表于: 2007-08-06  
其它的问题用到的朋友自己解决去吧,哈哈,要休息了。
有PHP、Qt、用友相关开发业务相商的朋友请发电子邮件到XChinux@163.com
有技术问题的朋友请在论坛里发贴提问,不要通过私人短信或者QQ/MSN提问讨论,谢谢合作

邮件:  XChinux@163.com
网址: http://www.qtcn.org
博客: http://hi.baidu.com/xchinux
操作系统: 红旗桌面版6, 红旗工作站5, Mandriva 2009
主要开发方向: C++/Qt程序开发、PHP/Apache/MySQL/PostgreSQL程序开发、用友相关二次开发
经常使用的工具: Opera/Firefox, Vim, OpenOffice/RedOffice, 二笔输入法
mxw5495 离线
级别: 新手上路
显示用户信息 
3  发表于: 2007-08-07  
多谢老大指点,以前只知道窗体能用setmask,原来按钮控件也可以,呵呵
jzj139 离线
级别: 侠客
显示用户信息 
4  发表于: 2007-08-07  
我这段时间做的就是这方面的代码编写工作
有空也可以写点东西  不过现在太累
还是交给老大写吧  呵呵
qt
幻想天空 离线
级别: 新手上路
显示用户信息 
5  发表于: 2007-08-19  
请问老大,在qt中怎么实现窗口的透明度调节啊?十万火急!!!相当感谢!!
浪漫天使 离线
级别: 圣骑士
显示用户信息 
6  发表于: 2007-08-19  
void setWindowOpacity ( qreal level )


windowOpacity : double
This property holds the level of opacity for the window.
The valid range of opacity is from 1.0 (completely opaque) to 0.0 (completely transparent).
By default the value of this property is 1.0.
This feature is available on Mac OS X, X11 platforms that support the Composite extension, and Windows 2000 and later.
Note that under X11 you need to have a composite manager running, and the X11 specific _NET_WM_WINDOW_OPACITY atom needs to be supported by the window manager you are using.
Warning: Changing this property from opaque to transparent might issue a paint event that needs to be processed before the window is displayed correctly. This affects mainly the use of QPixmap::grabWindow(). Also note that semi-transparent windows update and resize significantly slower than opaque windows.
sunnylemon 离线
级别: 新手上路
显示用户信息 
7  发表于: 2008-11-11  
楼上的你傻吗?把帮助文档的COPY出来有用吗?
--  --#
vrcats 离线
级别: 侠客
显示用户信息 
8  发表于: 2008-11-12  
楼上不要犯了嗔戒嘛,看帮助是最好的学习Qt方法
yurenjimi 离线
级别: 新手上路
显示用户信息 
9  发表于: 2008-12-15  
QWidget::setWindowOpacity ( qreal level )

十试验证明是可行的。。。。楼上的楼上尝试一下再说……
hello world!!
描述
快速回复

认证码:

验证问题:
100 + 2 = ? 正确答案:102
按"Ctrl+Enter"直接提交