在一个对话框中,我有一个QLineEdit和一个按钮。当我按下按钮时,我想启用QLineEdit的工具提示(在它里面或它下面)。请给我一个代码片段。
发布于 2010-06-10 22:47:28
下面是一个简单的例子:
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget* parent = 0) : QWidget(parent)
{
QVBoxLayout* layout = new QVBoxLayout(this);
edit = new QLineEdit(this);
layout->addWidget(edit);
showButton = new QPushButton("Show tool tip", this);
layout->addWidget(showButton);
hideButton = new QPushButton("Hide tool tip", this);
layout->addWidget(hideButton);
connect(showButton, SIGNAL(clicked(bool)), this, SLOT(showToolTip()));
connect(hideButton, SIGNAL(clicked(bool)), this, SLOT(hideToolTip()));
}
public slots:
void showToolTip()
{
QToolTip::showText(edit->mapToGlobal(QPoint()), "A tool tip");
}
void hideToolTip()
{
QToolTip::hideText();
}
private:
QLineEdit* edit;
QPushButton* showButton;
QPushButton* hideButton;
};正如您所看到的,没有简单的方法来启用某些小部件的工具提示。您必须向QToolTip::showText提供全局坐标。
另一种方法是自己创建一个QHelpEvent并使用QCoreApplication::postEvent发布此事件。这样,您就可以使用QWidget::setToolTip指定要在小部件中显示的文本。不过,您仍然需要提供全局坐标。
我真的对你为什么要这样做很感兴趣,因为工具提示只在你悬停鼠标或询问“这是什么”信息时才会显示。把它用在别的地方看起来像是糟糕的设计。如果您想向用户发送消息,为什么不使用QMessageBox
发布于 2010-06-10 22:47:11
如果您需要QLineEdit的工具提示,那么问题出在哪里?只需设置:
myLineEdit->setToolTip("Here is my tool tip");但是,如果您只需要在按下某个button之后显示一些文本,这里有另一个解决方案:创建一个插槽,例如on_myBytton_clicked(),然后将其连接到您的按钮上。在slot中,使用位于表单上的QLabel、QTextEdit等小部件上的文本执行setText()函数。
希望能有所帮助。
https://stackoverflow.com/questions/3014879
复制相似问题