2015-09-07 85 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());