qt自定义标题栏

1)创建一个标题的界面类,重新实现鼠标移动点击等事件

2)设置主界面无边框,创建一个垂直布局管理器,插入标题界面,在标题界面和主界面之间添加一个分割器

setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);

QtGuiDetailTitle *title = new QtGuiDetailTitle(this);

ui.verticalLayout->setContentsMargins(8, 8, 8, 8);//设置上下左右边距

ui.verticalLayout->insertWidget(0, title);

ui.verticalLayout->insertStretch(1);//插入一个分割器

3)重新实现主界面的nativeEvent时间,使界面可伸缩

#include <Windows.h>
#include <WindowsX.h>
bool QtGuiDirectDetail::nativeEvent(const QByteArray &eventType, void *message, long *result){
  MSG* msg = (MSG*)message;
  switch (msg->message){
  case WM_NCHITTEST:
    int xPos = GET_X_LPARAM(msg->lParam) - this->frameGeometry().x();
    int yPos = GET_Y_LPARAM(msg->lParam) - this->frameGeometry().y();
    if (this->childAt(xPos, yPos) == 0){
      *result = HTCAPTION;
    }
    else{
      return false;
    }
    if (xPos > 0 && xPos < 8){
      *result = HTLEFT;
    }
    if (xPos >(this->width() - 8) && xPos < (this->width() - 0)){
      *result = HTRIGHT;
    }
    if (yPos > 0 && yPos < 8){
      *result = HTTOP;
    }
    if (yPos >(this->height() - 8) && yPos < (this->height() - 0)){
      *result = HTBOTTOM;
    }
    if (xPos > 18 && xPos < 22 && yPos > 18 && yPos < 22){
      *result = HTTOPLEFT;
    }
    if (xPos >(width() - 22) && xPos < (width() - 18) && yPos > 18 && yPos < 22){
      *result = HTTOPRIGHT;
    }
    if (xPos > 18 && xPos < 22 && yPos >(height() - 22) && yPos < (height() - 18)){
      *result = HTBOTTOMLEFT;
    }
    if (xPos >(width() - 22) && xPos < (width() - 18) && yPos >(height() - 22) && yPos < (height() - 18)){
      *result = HTBOTTOMRIGHT;
    }
    return true;
  }
  return false;
}

已经工作的程序员朋友可以关注下我的gzh“程序员成长日志”,分享日常工作中解决的问题即可赚取稿费,大家一起成长~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容