qt 自定义标题栏

在做产品的时候,往往为了美观自己定义标题栏.

自定义标题栏其实就两大部分:

1:重写基类的纯虚函数

1>mousePressEvent(QMouseEvent *event)

2>mouseReleaseEvent(QMouseEvent *event)

3>mouseMoveEvent(QMouseEvent *event)

2:编写标题栏对应 min,max,close,三个曹函数;

如下一个简单的demon:

如果需要在标题栏加入图片,文字说明,可以将其加入到水平布局器中.

hb=new QHBoxLayout;

hb->addWidget(button_min);

hb->addWidget(button_max);

hb->addWidget(button_close);

setLayout(hb);



代码如下:

头文件:

#include<QWidget>
#include<QMouseEvent>
#include<QPushButton>
#include<QHBoxLayout>
#include<QToolButton>
class my_title:public QWidget
{
    Q_OBJECT
public:
  explicit  my_title(QWidget *parent=0);
  bool is_press;
  QPoint pos;   //保存鼠标点击的坐标
  virtual void mousePressEvent(QMouseEvent *event);
  virtual void mouseMoveEvent(QMouseEvent *event);
  virtual void mouseReleaseEvent(QMouseEvent *event);
  QToolButton *button_min;
  QToolButton *button_close;
  QToolButton *button_max;
  QHBoxLayout *hb;
  void init();
public slots:
  void slot_close();  //关闭曹函数
  void slot_min();// 最小化
  void slot_max(); //最大话
};
 

CPP文件:

#include"my_title.h"
#include<QWidget.h>
#include<QDebug>
my_title::my_title(QWidget *parent):QWidget(parent)
{
    this->setFixedHeight(50);
    QPalette pal;
    pal.setColor(QPalette::Background, QColor(22, 164, 250));
    setAutoFillBackground(true);
    setPalette(pal);
    init();
    connect(button_close,SIGNAL(clicked()),this,SLOT(slot_close()));
    connect(button_max,SIGNAL(clicked()),this,SLOT(slot_max()));
    connect(button_min,SIGNAL(clicked()),this,SLOT(slot_min()));
}

/*************************************

*这里重点讲解三个虚函数

***************************************/

1:pressEvent函数主要是获取鼠标按下的坐标;

void my_title::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
       is_press=true;
       pos=event->globalPos(); //获取鼠标当前的坐标
    }
    event->ignore();//忽略事件,意思就是说鼠标点击了,但是不是左键,事件会一直执行寻找对应执行他的事件。所以这块可以忽略调事件.
}
void my_title::mouseReleaseEvent(QMouseEvent *event)
{
    is_press=false;
    event->ignore();
}
2:
move_pos=event->globalPos()-pos;

获得鼠标从点击到移动两者之间的坐标向量;

window()->move(window()->pos()+move_pos);

鼠标的坐标+移动的向量,就是最后的位置。

 pos = event->globalPos();

这一步必须属注意:因为鼠标移动是不断变化的,所以当前的坐标也要实时获取。

window()这个函数可以自行查找下,有好几种替代的方法;

void my_title::mouseMoveEvent(QMouseEvent *event)
{
    QPoint move_pos;
    if(is_press)
    {
        qDebug()<<"1111111111111";
        move_pos=event->globalPos()-pos;
        pos = event->globalPos();
        window()->move(window()->pos()+move_pos);
    }
    event->ignore();
}
void my_title::init()
{
    button_close=new QToolButton;
    button_close->setFixedSize(30,30);
 button_close->setStyleSheet("border-style:outset;background-color: rgb(22, 164, 250);image: url(:/new/ui_image/close.png);");
    button_max=new QToolButton;
    button_max->setStyleSheet("border-style:outset;background-color: rgb(22, 164, 250);image: url(:/new/ui_image/max.png);");
    button_max->setFixedSize(30,30);
    button_min=new QToolButton;
    button_min->setFixedSize(30,30);
    button_min->setStyleSheet(" border-style:outset;background-color: rgb(22, 164, 250);image: url(:/new/ui_image/min.png);");
    hb=new QHBoxLayout;
    hb->addWidget(button_min);
    hb->addWidget(button_max);
    hb->addWidget(button_close);
    setLayout(hb);
}
void my_title::slot_close()
{
    this->window()->close();
}
void my_title::slot_max()
{
    this->window()->showMaximized();
}
void my_title::slot_min()
{
    this->window()->showMinimized();
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值