首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QT - How to apply a QToolTip on a QLineEdit

QT - How to apply a QToolTip on a QLineEdit
EN

Stack Overflow用户
提问于 2010-06-10 21:38:12
回答 2查看 16.8K关注 0票数 4

在一个对话框中,我有一个QLineEdit和一个按钮。当我按下按钮时,我想启用QLineEdit的工具提示(在它里面或它下面)。请给我一个代码片段。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-06-10 22:47:28

下面是一个简单的例子:

代码语言:javascript
复制
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

票数 7
EN

Stack Overflow用户

发布于 2010-06-10 22:47:11

如果您需要QLineEdit的工具提示,那么问题出在哪里?只需设置:

代码语言:javascript
复制
myLineEdit->setToolTip("Here is my tool tip");

但是,如果您只需要在按下某个button之后显示一些文本,这里有另一个解决方案:创建一个插槽,例如on_myBytton_clicked(),然后将其连接到您的按钮上。在slot中,使用位于表单上的QLabelQTextEdit等小部件上的文本执行setText()函数。

希望能有所帮助。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3014879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档