QLabel text not updating



  • Hi i want to update the text of QLabel when the key is pressed, i am unable to see the output on the UI when ever the Key is pressed.
    Here i s my code

    WIDGET.H

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include<QtGui>
    #include <QEvent>
    #include<QTextEdit>
    #include<QLabel>
    #include<QWidget>
    #include <QKeyEvent>
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
    
        Widget(QWidget *parent = 0);
       QLabel *lab;
        
    protected:
        bool event(QEvent *);
    };
    
    #endif // WIDGET_H
    
    

    WIDGET.CPP

    #include "widget.h"
    #include <QDebug>
    #include<QLabel>
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
            resize(1000,1000);
            qDebug() << "Constructor";
            QPalette palet=this->palette();
            QPixmap pix(":9.jpeg");
            QPixmap pix_scaled(pix.scaled(500,500,Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
            palet.setBrush(QPalette::Window, QBrush(pix_scaled));
            setPalette(palet);
            setAutoFillBackground(true);
    
            lab=new QLabel;
            lab->setParent(this);
            lab->setGeometry(30,30,310,60);
            lab->setText("SETTING TEXTTT");
    }
    
    bool Widget::event(QEvent *e)
    {
    if(e->type()==QEvent::KeyRelease)
     {
              QKeyEvent *ke = static_cast<QKeyEvent *>(e);
              int keyValR = ke->key();
              qDebug() << "Released key value is : " << keyValR;
    
             switch(keyValR)
             {
                 case Qt::Key_F1:
                      qDebug()<<"F1 Key Pressed";
                      lab->setText("KeyPressed is F1");
                      break;
                case Qt::Key_Q:
                      qDebug()<<"Q Key Pressed";
                      lab->setText("KeyPressed is Q");
                      break;
                default:
                       qDebug("No Key Pressed");
           
             }
        }
    }
    

    Here is the output image URL

    https://s17.postimg.org/z9uo6n2in/Screenshot_from_2017_02_13_17_09_31.png

    Please correct my Code if there are any mistakes



  • Code looks good to me at least. Can u try calling lab.update() function ? Also as a side note you can directly implement keyreleaseevent in widget class instead of going through event() function.



  • Hi,
    you can do such things in so-called event filter functions, similar to the function event, that you defined but with another signature, like:

    bool Widget::eventFilter(QObject *obj, QEvent *e)
    {
    if(e->type()==QEvent::KeyRelease)
     {
              QKeyEvent *ke = static_cast<QKeyEvent *>(e);
              int keyValR = ke->key();
              qDebug() << "Released key value is : " << keyValR;
    
             switch(keyValR)
             {
                 case Qt::Key_F1:
                      qDebug()<<"F1 Key Pressed";
                      lab->setText("KeyPressed is F1");
                      break;
                case Qt::Key_Q:
                      qDebug()<<"Q Key Pressed";
                      lab->setText("KeyPressed is Q");
                      break;
                default:
                       qDebug("No Key Pressed");
                }
           	// standard event processing
    	return QObject::eventFilter(obj, e);
    }
    

    And you will have to call something like:
    lab->installEventFilter(this) and
    lab->removeEventFilter(this)
    in contrctor and destructor of the Widget class.
    -Michael



  • @dheerendra

    I have tried update() too but of no use and i want to share something more

    If i exapnd the Widget size then i am able to see the updated text

    Image before Update of Label Text
    https://s17.postimg.org/z9uo6n2in/Screenshot_from_2017_02_13_17_09_31.png

    Image after Update of Label Text
    https://s14.postimg.org/aaamuwaqp/Screenshot_from_2017_02_13_17_34_50.png

    To say i have just exapnded the size of Widget a pinch that's all

    And i have already tried the approach of keyreleaseevent, but i want to find out why this approach is not working.



  • @m.sue

    thanks for replying me, i have tried what you have said but the control is not entering into the IF condition

    if(e->type()==QEvent::KeyRelease)
    {
      //Not entering into the condition
    }
    


  • Hi,
    I looked at the event function and it is probably the same as using an event filter. But in your function eventas in any virtual function you should use somewhere the base class implementation like
    return QWidget::event(e)
    at the end.
    -Michael.



  • @m.sue said in QLabel text not updating:

    Thanks Michael it worked and the text is updating.


Log in to reply
 

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