tags:

views:

152

answers:

2

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.

A: 

If you need tooltip for QLineEdit, so what is the problem? Just set:

myLineEdit->setToolTip("Here is my tool tip");

But if you need just to show some text after some button was pressed, here the another solution: create a slot, for example on_myBytton_clicked() and connect it to your button. In slot do the setText() function with your text on QLabel, QTextEdit and etc widgets located on your form.

Hope it help.

mosg
This is not what the OP asked. But you're right: I also think its a better idea to use a `QLabel` instead of a tool tip.
Job
Your answer does not have any connection to my question. I ask how to use toolTip!!!
Narek
So you better read about how to ask correct question, before posting them...
mosg
Seems you misunderstood. Does this code enable myLineEdit->setToolTip("Here is my tool tip"); a tooltip when I press the button???
Narek
+1  A: 

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;
};

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.

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.

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?

Job
Thanks. I want to use this in order to tell user that the QLineEdit is going to be filled with CapsLock ON state (for password field).There are two questions: 1) Why if I don't separate this class definition in .cpp and .h files it gives linker error (some vtable problem)? And how I can have more then one QToolTips separately invoked in one class? I see you have used static functions you used.
Narek
1: Probably because you didn't `#include` the moc file. See http://stackoverflow.com/questions/3001615/qt-moc-with-implementations-inside-of-header-files/ 2: I don't really understand your question. Do you mean show more than one tool tip at once? This is impossible.
Job