2016-03-14 133 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