QT - 如何在QLineEdit上应用QToolTip [英] QT - How to apply a QToolTip on a QLineEdit

查看:1118
本文介绍了QT - 如何在QLineEdit上应用QToolTip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On a dialog I have a QLineEdit and a button. I want to enable a tool tip for the QLineEdit(in it or under it) when I press the button. Please give me a code snippet.

推荐答案

这里有一个简单的例子:

Here is a simple example:

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 提供全局坐标。

As you can see, there is no easy way to just enable the tool tip of some widget. You have to provide global coordinates to QToolTip::showText.

另一种方法是创建一个 QHelpEvent 自己并使用 QCoreApplication :: postEvent 发布此事件。这样,您可以使用 QWidget :: setToolTip 指定要在窗口小部件中显示的文本。你仍然必须提供全局坐标。

Another way to do this is to create a QHelpEvent yourself and post this event using QCoreApplication::postEvent. This way, you can specify the text to be shown in your widget using QWidget::setToolTip. You still have to provide global coordinates, though.

我真的很感兴趣,为什么你想这样做,因为工具提示只有当你鼠标悬停鼠标,当您要求这是什么信息。它看起来像不好的设计,使用它的东西。如果你想给用户一个消息,为什么不使用 QMessageBox

I am really interested in why you want to do this since tool tips are intended to be shown only when you hover your mouse or when you ask for the "What's this" information. It looks like bad design to use it for something else. If you want to give a message to the user, why don't you use QMessageBox?

这篇关于QT - 如何在QLineEdit上应用QToolTip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆