Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel text not updating
Forum Updated to NodeBB v4.3 + New Features

QLabel text not updating

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mounipanditi
    wrote on last edited by mounipanditi
    #1

    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

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by dheerendra
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      M 1 Reply Last reply
      7
      • m.sueM Offline
        m.sueM Offline
        m.sue
        wrote on last edited by m.sue
        #3

        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

        M 1 Reply Last reply
        0
        • dheerendraD dheerendra

          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.

          M Offline
          M Offline
          mounipanditi
          wrote on last edited by mounipanditi
          #4

          @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.

          1 Reply Last reply
          0
          • m.sueM m.sue

            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

            M Offline
            M Offline
            mounipanditi
            wrote on last edited by
            #5

            @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
            }
            
            1 Reply Last reply
            0
            • m.sueM Offline
              m.sueM Offline
              m.sue
              wrote on last edited by
              #6

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

              M 1 Reply Last reply
              2
              • m.sueM m.sue

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

                M Offline
                M Offline
                mounipanditi
                wrote on last edited by
                #7

                @m.sue said in QLabel text not updating:

                Thanks Michael it worked and the text is updating.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved