2016-03-14 212 views
0

我希望能够检测作为主窗口一部分的工具栏的位置和方向的任何变化。该工具栏有一个QPushButton,称为nodes_button并且是主窗口的一部分。我正在使用默认的QMainWindowQToolBarnodes_button包含一个QMenu,整个练习的目的是将nodes_button的菜单指示符以可视化方式放置在工具栏中。QMainWindow中的QToolbar - 关于其位置变化的信号工具栏

默认主窗口中的默认工具栏有5个位置,它们可以放置在顶部,底部,左侧,右侧和浮动位置。根据工具栏所在的区域,它还将其方向更改为水平(浮动,顶部和底部)或垂直(左或右)。

确定取向不是困难的,因为一个工具栏实际上提供QToolBar::orientation()(用于检索的方位)和QToolBar::orientationChanged(Qt::Orientation)(一个信号发射的每一次的工具栏的取向改变)。利用信号我能象这样连接插槽:

connect(toolbar, SIGNAL(orientationChanged(Qt::Orientation)), this, SLOT(toolbarAdjust())); 

目前toolbarAdjust()只触发时工具栏的方向改变,但是我也想用它时,工具栏的位置发生变化:

void MainWindow::toolbarAdjust() 
{ 
    Qt::Orientation toolbarOrientation = toolbar->orientation(); 
    Qt::ToolBarArea toolbarPosition = this->toolBarArea(toolbar); 

    if(toolbarOrientation == Qt::Horizontal) { 
    if(toolbarPosition == Qt::NoToolBarArea || toolbarPosition == Qt::TopToolBarArea) { 
     nodes->setStyleSheet("QPushButton#nodes_button::menu-indicator {image: url(:node_menu_top); subcontrol-position: bottom center; subcontrol-origin: padding; bottom: -7px}"); 
    } 
    else if(toolbarPosition == Qt::BottomToolBarArea) { 
     nodes->setStyleSheet("QPushButton#nodes_button::menu-indicator {image: url(:node_menu_bottom); subcontrol-position: top center; subcontrol-origin: padding; top: -7px}"); 
    } 
    } 
    else if (toolbarOrientation == Qt::Vertical) { 
    if(toolbarPosition == Qt::LeftToolBarArea) { 
     nodes->setStyleSheet("QPushButton#nodes_button::menu-indicator {image: url(:node_menu_right); subcontrol-position: left center; subcontrol-origin: padding; right: -6px;}"); 
    } 
    else if(toolbarPosition == Qt::RightToolBarArea) { 
     nodes->setStyleSheet("QPushButton#nodes_button::menu-indicator {image: url(:node_menu_left); subcontrol-position: right center; subcontrol-origin: padding; left: -6px;}"); 
    } 
    } 
} 

由于许多原因,这种情况并不总是如此,因为对于左右和上下工具栏区域,工具栏的方向是相同的 - 垂直/水平。

所以我需要这样的东西

connect(this, SIGNAL(toolbarLocationChanged()), this, SLOT(toolbarAdjust())); 

使用工具,我可以把我的菜单指示在适合的工具栏的当前位置的方式的方向和位置。正如你所看到的方向之下的组合样式表的相应变化

QPushButton#nodes_button::menu-indicator { 
    image: url(:node_menu_top);   // bottom, right, left 
    subcontrol-position: bottom center; // top, right, left 
    subcontrol-origin: padding; 
    bottom: -7px      // top, left, right 
} 

导致

enter image description here enter image description here enter image description here enter image description here

我其实可以省略全方向部分和请执行以下操作:

void MainWindow::toolbarAdjust() 
{ 
    Qt::ToolBarArea toolbarPosition = this->toolBarArea(drawingToolBar); 

    switch(toolbarPosition) { 
    case Qt::NoToolBarArea:; 
    case Qt::TopToolBarArea: 
     nodes->setStyleSheet("QPushButton#nodes_button::menu-indicator {image: url(:node_menu_top); subcontrol-position: bottom center; subcontrol-origin: padding; bottom: -7px}"); 
     break; 
    case Qt::BottomToolBarArea: 
     nodes->setStyleSheet("QPushButton#nodes_button::menu-indicator {image: url(:node_menu_bottom); subcontrol-position: top center; subcontrol-origin: padding; top: -7px}"); 
     break; 
    case Qt::LeftToolBarArea: 
     nodes->setStyleSheet("QPushButton#nodes_button::menu-indicator {image: url(:node_menu_right); subcontrol-position: left center; subcontrol-origin: padding; right: -6px;}"); 
     break; 
    case Qt::RightToolBarArea: 
     nodes->setStyleSheet("QPushButton#nodes_button::menu-indicator {image: url(:node_menu_left); subcontrol-position: right center; subcontrol-origin: padding; left: -6px;}"); 
     break; 
    } 
} 

由于工具栏在停靠/浮动时的行为方式。

编辑: 我发现this post其中一个用户告诉OP约QDockWidget::dockLocationChanged()和,也许这样的功能已经被添加到QToolBar了。最后,OP发布一封电子邮件,其中显示已请求此功能。该帖子是从2007年...

回答

0

我发现QToolBar::topLevelChanged(bool)信号帮助我解决问题。每当用户想要更改工具栏的工具栏区域时,他/她必须单击工具栏上的工具栏,将其拖至该区域然后放下。基于Qt文档:

当浮动属性发生变化时,会发出此信号。如果工具栏现在处于浮动状态,则 topLevel参数为true;否则为 这是错误的。

因此,无论何时用户拖动工具栏,它显然也是浮动的。工具栏停靠后,它不会浮动。检测工具栏浮动属性中的这一更改可用于调整其内容。

下面是结果,如果任何人的兴趣:

(指标朝下)

enter image description here

底部位置(指标朝上)

enter image description here

顶部位置

左放置(指标指向右侧)

enter image description here

放置的位置(指标指向左边)

enter image description here