2015-09-07 33 views

回答

0

懸停QWidget時顯示幫助的最簡單方法是:setToolTip(QString)和setToolTipDuration(int)。 如果你想要一個「?」按鈕,只需實現你自己的QWidget。然後通過UI設計器或直接在您的代碼中添加QPushButton和QLabel佈局,並顯示您的QLabel單擊()時光標位置的幫助文本。這樣的事情:

{ 
// Constructor 
... 
    m_mainLabel = new QLabel("Main text"); 
    m_button = new QPushButton("?"); 
    m_helpLabel = new QLabel("Help text"); 
    connect(m_button, SIGNAL(clicked(bool)), 
      this, SLOT(slotShowOrHideHelpLabel(bool))); 
    QHBoxLayout *hBoxLayout = new QHBoxLayout; 
    hBoxLayout->addWidget(m_mainLabel); 
    hBoxLayout->addWidget(m_button); 
    setLayout(hBoxLayout); 
} 

void slotShowOrHideHelpLabel(bool showHelpLabel) 
{ 
    if (showHelpLabel) 
    { 
     m_helpLabel->show(); 
     m_helpLabel->move(QCursor::pos()); 
    } 
    else 
    { 
     m_helpLabel->hide(); 
    } 
} 
+0

感謝您的建議。而不是創建一個helpLabel,在「?」按鈕單擊,我正在爲主標籤執行setToolTip()和setWhatsThis()。但不知何故,這個工具提示和什麼是不會出現的。如何在點擊事件中啓用它們? – user2653062

+0

您可以使用eventFilter處理主標籤上的單擊事件,而無需實現自定義類。並在單擊事件的處理程序中創建並顯示您在toolTip或whatsThis中設置的文本的helpLabel。據我所知,toolTip,WhatsThis,statusTip是持有信息的屬性。您可以將其用於您的目的,例如,單擊時顯示在標籤中)) – genesis32

0

此外,您可以使用QMenu,而不是QPushButton + QLabel。

// Constructor 
setContextMenuPolicy(Qt::CustomContextMenu); 
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotCustomMenu(QPoint))); 

// slotCustomMenu(QPoint) 
QMenu menu(this); 
menu.addAction(this->toolTip()); 
menu.addAction(this->whatsThis()); 
menu.exec(QCursor::pos());