在做产品的时候,往往为了美观自己定义标题栏.
自定义标题栏其实就两大部分:
1>mousePressEvent(QMouseEvent *event)
2>mouseReleaseEvent(QMouseEvent *event)
3>mouseMoveEvent(QMouseEvent *event)
如下一个简单的demon:
如果需要在标题栏加入图片,文字说明,可以将其加入到水平布局器中.
hb=new QHBoxLayout;
hb->addWidget(button_min);
hb->addWidget(button_max);
hb->addWidget(button_close);
setLayout(hb);
#include
#include
#include
#include
#include
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(); //最大话
};
#include"my_title.h" #include
#include
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();
}