Qt 無邊框視窗自定義標題欄

2021-06-26 13:55:11 字數 2779 閱讀 1130

實現了自定義標題欄的最小化,最大化(自適應工作列),關閉,拉伸,拖曳。

因為不想引入其他資源,所以按鈕,背景都是系統提供的。

效果圖:

直接上**。

用法:

#include "widget.h"

int main(int argc, char *argv)

#pragma once

#include class qtoolbutton;

class customframe : public qframe

;

#include "customframe.h"

#include static const int title_height = 30;

static const int frame_border = 2;

customframe::customframe(qwidget *contentwidget, const qstring &title)

: contentwidget_(contentwidget)

void customframe::slotshowsmall()

void customframe::slotshowmaxrestore()

else

ismax_ = !ismax_;

}void customframe::mousepressevent(qmouseevent *e)

else if (e->type() == qevent::mousebuttondblclick && e->pos().y() <= title_height) }}

void customframe::mousemoveevent(qmouseevent *e)

this->move(e->globalpos() - clickpos_);

}void customframe::mousereleaseevent(qmouseevent *)

bool customframe::nativeevent(const qbytearray & eventtype, void * message, long * result)

else

if(xpos > 0 && xpos < hit_border)

if(xpos > (this->width() - hit_border) && xpos < (this->width() - 0))

if(ypos > 0 && ypos < hit_border)

if(ypos > (this->height() - hit_border) && ypos < (this->height() - 0))

if(xpos > 0 && xpos < hit_border && ypos > 0 && ypos < hit_border)

if(xpos > (this->width() - hit_border) && xpos < (this->width() - 0) && ypos > 0 && ypos < hit_border)

if(xpos > 0 && xpos < hit_border && ypos > (this->height() - hit_border) && ypos < (this->height() - 0))

if(xpos > (this->width() - hit_border) && xpos < (this->width() - 0) && ypos > (this->height() - hit_border) && ypos < (this->height() - 0))

return true;

}return false;

}void customframe::paintevent(qpaintevent *e)

qpainter painter(this);

qpainterpath painterpath;

painterpath.setfillrule(qt::windingfill);

painterpath.addrect(border, border, this->width()-2*border, this->height()-2*border);

painter.setrenderhint(qpainter::antialiasing, true);

painter.fillpath(painterpath, qbrush(qt::white));

qcolor color(200, 200, 200);

for (int i=0; iwidth()-(border-i)*2, this->height()-(border-i)*2);

} painter.setpen(qt::nopen);

painter.setbrush(qt::white);

// 這裡可以在資源中指定一張標題背景

border, this->width()-2*border, this->height()-2*border), qpixmap(default_skin));

painter.drawrect(qrect(border, title_height, this->width()-2*border, this->height()-title_height-border));

qframe::paintevent(e);

}

QT 自定義標題欄

1 去除舊的標題欄 去除qdialog對話方塊有上角問號 qt windowflags flags qt dialog flags qt windowclosebuttonhint flags qt framelesswindowhint setwindowflags flags flags qt ...

QT自定義標題欄

ifndef dlgtitle h define dlgtitle h include enum buttontype class dlgtitle public qwidget endif dlgtitle h自定義標題欄類dlgtitle.cpp如下 include dlgtitle.h def...

Qt 個性化標題欄,自定義標題欄

目前還沒有達到自己滿意的地步,魔方別人寫的的,先提供參考,後面在加入新的東西 標頭檔案 ifndef titlebar h define titlebar h include class qlabel class qpushbutton class titlebar public qwidget e...