我使用 Qt Creator 创建了一个应用程序。因为我希望它看起来与普通的 Windows 应用程序不同,所以我删除了 Windows 标题栏。但是,如果没有标题栏,我将无法移动、最小化和最大化应用程序。如果可能的话,我希望通过按住菜单栏来移动我的应用程序,并将 OS X 应用程序中使用的那三个彩色圆形按钮放在菜单栏的右上角。这在 Qt 中可行吗?
最佳答案
我将解释如何获得它的结构,但我会留给您来设计所有内容并连接信号。
最好的办法是创建您自己的 QWidget
类,名称类似于“TitleBar”,并为其指定一个 QHBoxLayout
。在“TitleBar”类中,创建另一个 QWidget
和 QHBoxLayout
来包含按钮。创建 3 个 QPushButtons
并将它们添加到其中。接下来,您需要创建自己的 QMenuBar
类,名称类似于“MenuBar”,这样您就可以获取鼠标事件并构建拖动机制。以下是您如何完成此操作的一些示例代码。
标题栏
TitleBar::TitleBar() :
QWidget()
{
QHBoxLayout *l = new QHBoxLayout();
l->setContentsMargins(0, 0, 0, 0);
setLayout(l);
QWidget *buttonGroup = new QWidget();
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->setContentsMargins(0, 0, 0, 0);
QPushButton *minButton = new QPushButton("");
QPushButton *maxButton = new QPushButton("");
QPushButton *closeButton = new QPushButton("");
buttonLayout->addWidget(minButton, Qt::AlignRight);
buttonLayout->addWidget(maxButton, Qt::AlignRight);
buttonLayout->addWidget(closeButton, Qt::AlignRight);
buttonGroup->setLayout(buttonLayout);
buttonGroup->setFixedSize(buttonGroup->sizeHint());
MenuBar *bar = new MenuBar();
bar->addMenu("&File");
l->addWidget(bar);
l->addWidget(buttonGroup);
}
菜单栏
void MenuBar::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() == Qt::LeftButton)
{
QPointF newPos = (event->localPos() - dragPos) + window()->pos();
window()->move(newPos.x(), newPos.y());
}
else
QMenuBar::mouseMoveEvent(event);
}
void MenuBar::mousePressEvent(QMouseEvent *event)
{
dragPos = event->localPos();
QMenuBar::mousePressEvent(event);
}
然后,您只需将“TitleBar”类添加到顶部对齐的小部件的主布局中,就可以了!现在这并不完美,但我认为它可以完成工作。
这是一个使用此方法的示例项目:http://www.filedropper.com/menubartest
关于c++ - 从 Qt 应用程序中删除标题栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27847229/