How to show ToolTip for QLineEdit with out mousehovering ..!



  • I am trying to show the tooltip for a LineEdit without the mousehovering and by reading some examples and answers in Internet i have tried an example to make it work. But the example is compiling and executing fine but i am unable to achieve my target.

    I know i did some thing wrong, i request your help to do so. Please correct bugs in my code.

    MyLineEdit.h

    #ifndef LINEEDITTOOLTIP_H
    #define LINEEDITTOOLTIP_H
    
    #include <QWidget>
    #include <QLineEdit>
    
    class MyLineEdit : public QLineEdit
    {
      Q_OBJECT
    
    public:
      MyLineEdit(QWidget *parent = 0);
      ~MyLineEdit();
    
    signals:
      void focussed(bool hasFocus);
    
    protected:
      virtual void focusInEvent(QFocusEvent *e);
      virtual void focusOutEvent(QFocusEvent *e);
    };
    
    #endif // LINEEDITTOOLTIP_H
    

    MyLineEdit.cpp

    #include "MyLineEdit.h"
    
    
    MyLineEdit::MyLineEdit(QWidget *parent)
     : QLineEdit(parent)
    {}
    
    MyLineEdit::~MyLineEdit()
    {}
    
    void MyLineEdit::focusInEvent(QFocusEvent *e)
    {
      QLineEdit::focusInEvent(e);
      emit(focussed(true));
    }
    
    void MyLineEdit::focusOutEvent(QFocusEvent *e)
    {
      QLineEdit::focusOutEvent(e);
      emit(focussed(false));
    }
    

    Widget.h

    #include <QWidget>
    #include <QLineEdit>
    #include <QPushButton>
    #include <QDebug>
    #include <QToolTip>
    #include "MyLineEdit.h"
    
    class Widget : public QWidget
    {
        Q_OBJECT
        
    public:
        /*!
         * This is Constructor
         */
        explicit Widget(QWidget *parent = 0);
        MyLineEdit *ledt;
    
        ~Widget();
    private:
        QPushButton *button;
        QLineEdit *cmConfLineEdit;
    
    private slots:
    void showLineEditToolTip(bool);
    void buttonSlot();
    };
    #endif
    

    Widget.cpp

    Widget::Widget(QWidget *parent) :
        QWidget(parent),
    {
        ledt = new MyLineEdit;
       
        button = new QPushButton(this);
        button->setText("Press");
        button->setGeometry(100,50,40,40);
    
        cmConfLineEdit = new QLineEdit;
        cmConfLineEdit->setParent(ledt);
        cmConfLineEdit->setGeometry(100,120,60,30);
    
        connect(button,SIGNAL(clicked()),this,SLOT(buttonSlot()));
        connect(ledt,SIGNAL(focussed(bool)),this,SLOT(showLineEditToolTip(bool)));
    
    void Widget::buttonSlot()
    {
        cmConfLineEdit->setFocusPolicy(Qt::StrongFocus);
    } 
    void Widget::showLineEditToolTip(bool value)
    {
        qDebug()<<"Bool Value is"<<value;
        if(value ==  true)
        {
            QToolTip::showText(QPoint(110,130),"Enter Conf Number",this);
        }
    }
    

    Thanks in Advance,
    Mounika.P


  • Moderators

    @mounipanditi said in How to show ToolTip for QLineEdit with out mousehovering ..!:
    are you sure it is not working?
    QToolTip::showText() expects the position to be in global coordinates on the screen. Maybe you are just overseeing the tooltip?



  • @raven-worx

    Thanks for the reply,

    I am sure after clicking on the PushButton i am unable to see the qDebug which i have written in the showLineEditToolTip SLOT.

    What i have written is it correct..?


  • Moderators

    @mounipanditi said in How to show ToolTip for QLineEdit with out mousehovering ..!:

    What i have written is it correct..?

    seems so yes.
    Any other output on the console regarding an unsuccessful connection to the slot?



  • @raven-worx

    Nope except the following statement on console i am unable to see anything

    Qt: Session management error: None of the authentication protocols specified are supported



  • @mounipanditi
    I don't think what you've written is correct.

    Let me see, you have a subclass of QLineEdit that has a custom focussed signal,
    that seems ok.

    You create such a custom object:

    ledt = new MyLineEdit;
    //and connect to it
    connect(ledt,SIGNAL(focussed(bool)),this,SLOT(showLineEditToolTip(bool)));
    

    also ok,
    but your buttonSlot connects to this one:

    void Widget::buttonSlot()
    {
        cmConfLineEdit->setFocusPolicy(Qt::StrongFocus);
    } 
    

    which is a normal QLineEdit so you of course won't get Signal from ledt

    in fact your ledt is doing nothing, but leaking memory x)


  • Moderators

    @mounipanditi
    what @J.Hilk wrote is only partially correct, but it lead me to the actual issue.
    The code so far is syntactically correct. Though there is also non-sense contained.

    1. it doesn't make sense to set a focus policy in a slot connected to a button. What should actually happen in this slot? If you want to set the focus to the line edit you should use setFocus() instead
    2. I have no idea why you place a QLineEdit into your custom MyLineEdit widget. Thus the MyLineEdit widget never receives the focus, because the child (plain QLineEdit gets it first)


  • @raven-worx
    you're right, it seemes, that I missed the cmConfLineEdit->setParent(ledt); part. Only noticed it, when you wrote it down!



  • @raven-worx said in How to show ToolTip for QLineEdit with out mousehovering ..!:

    policy in a slot connected to a butto

    I understand your point, can you please guide me how to achieve my target to show the ToolTip for a lineEdit with out mouse hovering either by a code snippet or a documented guide.



  • @mounipanditi

    your example modified:

    Widget::Widget(QWidget *parent) :
        QWidget(parent),
    {
        ledt = new MyLineEdit;
        ledt->setGeometry(100,120,60,30);
       
        button = new QPushButton(this);
        button->setText("Press");
        button->setGeometry(100,50,40,40);
    
        connect(button,SIGNAL(clicked()),this,SLOT(buttonSlot()));
        connect(ledt,SIGNAL(focussed(bool)),this,SLOT(showLineEditToolTip(bool)));
    
    void Widget::buttonSlot()
    {
        ledt->setFocus();
    } 
    


  • @J.Hilk

    Thanks for the reply i was unable to see the Tooltip after modifying the code as you have posted.

    Please go through the below screen shots

    https://unsee.cc/dunepima/


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.