这个需求来自我想自定义标题栏,我发现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);
正常的
panel缩小后,titlebar变高了
dockwidget->setMinimumHeight(panel->minHeight);
导致的,我只需要设置dockwidget.widget的minimumHeight即可。
如果有inspect工具就方便排查问题了。