专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > QT开发

怎么实现无窗体圆角边框

发布时间:2010-05-30 21:25:03 文章来源:www.iduyao.cn 采编人员:星星草
如何实现无窗体圆角边框
1.如何制作使得无边框窗体的边框为圆角
2.如何将无边框窗体中的关闭,最大,最小化按钮放置在最顶端,我现在做出的效果是离顶端还有一段距离。
以上两个问题麻烦大侠支个招,嘿嘿,谢谢啦,祝各位新年快乐。

------解决方案--------------------
圆角的我也想知道
至于第二个问题是否可以不用它自带的那三个按钮自己实现这三个按钮呢
------解决方案--------------------
QStyleSheet
------解决方案--------------------
来点代码,胜过千言万语:
C/C++ code

widget.h头文件
#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui/QWidget>
#include <qpoint.h>
class QPushButton;
class QPoint;
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
private:
    QPushButton *closeButton;
    bool isMouseDown;
    QPoint oldLocation;
    virtual void mouseMoveEvent(QMouseEvent *);
    virtual void mousePressEvent(QMouseEvent *);
    virtual void mouseReleaseEvent(QMouseEvent *);
private slots:
    void on_closeButton_clicked();
};
#endif // WIDGET_H


Widget.cpp源文件:
#include "widget.h"
#include <QPushButton>
#include <QBitmap>
#include <QPainter>
#include <QMouseEvent>
#include <qdebug.h>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);//无边框窗体
    closeButton = new QPushButton("X",this);
    closeButton->setObjectName("closeButton");
    closeButton->setGeometry(15,0,22,22);
    QMetaObject::connectSlotsByName(this);
    resize(300,300);

    QBitmap bmp(this->size());
    bmp.fill();
    QPainter p(&bmp);
    p.setPen(Qt::NoPen);
    p.setBrush(Qt::black);
    p.drawRoundedRect(bmp.rect(),10,10);
    setMask(bmp);//设置窗体遮罩

    isMouseDown = false;
}
void Widget::mousePressEvent(QMouseEvent *e)
{
    isMouseDown = true;
    oldLocation = this->mapToGlobal(e->pos());
    qDebug()<<oldLocation;
}

void Widget::mouseReleaseEvent(QMouseEvent *)
{
    isMouseDown = false;
}
void Widget::mouseMoveEvent(QMouseEvent *e)
{
    if(isMouseDown)//如果鼠标左键按下
    {
        QPoint p = this->mapToGlobal(e->pos());
        int x = p.x() - oldLocation.x();
        int y = p.y() - oldLocation.y();
        oldLocation = p;
        p = this->pos();
        p.setX(p.x() + x);
        p.setY(p.y() + y);
        this->move(p);//移动窗体
    }
}

Widget::~Widget()
{

}
void Widget::on_closeButton_clicked()
{
    this->close();
}

main.cpp文件:
#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

------解决方案--------------------
看范例文档
------解决方案--------------------
都可以通过QStyleSheet实现
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: