[√]qdockerwidget自定义titlebar遇到的各种问题

110 阅读1分钟

这个需求来自我想自定义标题栏,我发现unity就实现了,早期版本也是基于qt,于是就顺着这个思路整合了下。

设置titlebar的高度

一般我们都是使用layout布局,我想要设置titlebar的高度,无论怎么搞发现都无法生效

this->setTitleBarWidget(new QWidget());
QWidget* titleBar = this->titleBarWidget();
QHBoxLayout* layout = new QHBoxLayout();
titleBar->setLayout(layout);
layout->setContentsMargins(0, 0, 0, 0); // 核心代码

QLabel* label = new QLabel();
label->setFixedHeight(20);
label->setContentsMargins(0, 0, 0, 0);
label->setText(this->windowTitle());
layout->addWidget(label);

原来是layout的margin导致的,如果设置titlebar->setFixedHeight(20)就会导致发生截断。

widget缩小,titlebar高度发生变化

dockwidget->setWidget(panel);

正常的

image.png

panel缩小后,titlebar变高了

image.png

dockwidget->setMinimumHeight(panel->minHeight);导致的,我只需要设置dockwidget.widget的minimumHeight即可。

如果有inspect工具就方便排查问题了。