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. how to change the mouse cursor that shows QLable is selectable
Forum Updated to NodeBB v4.3 + New Features

how to change the mouse cursor that shows QLable is selectable

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 9.3k Views 3 Watching
  • 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.
  • Q Qt Enthusiast

    how to change the mouse cursor that shows QLable is selectable

    I am using setTextInteractionFlags (Qt::TextSelectableByMouse) for a the QLable . When I select the QLabel I can select the text and copy to clipboard . But when my mouse comes near to Qlabel it does not change the currsor that the QLabel is selectable .

    How can I change the mouse cursor to show it user that QLable is selectable

    joeQJ Offline
    joeQJ Offline
    joeQ
    wrote on last edited by
    #2

    @Qt-Enthusiast Hi, friend, welcome.

    Text is selectable is different with QLabel is selectable. They are two conpect.

    You can reimplement mouseMoveEvent of QLabel, in function, you can changed some state of QLable when use move the mouse hover QLabel.

    Note: setMouseTracking(true) for QLabel.

    Just do it!

    Q 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #3

      Wouldn't it be better to use QLineEdit with setReadOnly(true)? selecting Labels is not expected behaviour (and I'm not talking just in Qt but in any application)

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      4
      • joeQJ joeQ

        @Qt-Enthusiast Hi, friend, welcome.

        Text is selectable is different with QLabel is selectable. They are two conpect.

        You can reimplement mouseMoveEvent of QLabel, in function, you can changed some state of QLable when use move the mouse hover QLabel.

        Note: setMouseTracking(true) for QLabel.

        Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #4

        @joeQ

        I have done following code

        class myLabel: public QLabel
        {

        public:
        myLabel(QString string)
        {
        this->setText(string);
        this->setTextInteractionFlags (Qt::TextSelectableByMouse);

          //set MouseTracking true to capture mouse event even its key is not pressed
          this->setMouseTracking(true);
        };
        ~ myLabel(){};
        
        void mouseMoveEvent ( QMouseEvent * event )
        {
              setCursor(Qt::IBeamCursor);
          //Show x and y coordinate values of mouse cursor here
          //this->setText("Select the text");
        };
        
            void mouseReleaseEvent(QMouseEvent * event)
            {
              //setCursor(Qt::ArrowCursor);
        
            }  
        

        };

        but I am still getting issue , I want when the mouse goes away from text of QLabel then The mouse cursor should change back to Arrow

        mrjjM 1 Reply Last reply
        0
        • Q Qt Enthusiast

          @joeQ

          I have done following code

          class myLabel: public QLabel
          {

          public:
          myLabel(QString string)
          {
          this->setText(string);
          this->setTextInteractionFlags (Qt::TextSelectableByMouse);

            //set MouseTracking true to capture mouse event even its key is not pressed
            this->setMouseTracking(true);
          };
          ~ myLabel(){};
          
          void mouseMoveEvent ( QMouseEvent * event )
          {
                setCursor(Qt::IBeamCursor);
            //Show x and y coordinate values of mouse cursor here
            //this->setText("Select the text");
          };
          
              void mouseReleaseEvent(QMouseEvent * event)
              {
                //setCursor(Qt::ArrowCursor);
          
              }  
          

          };

          but I am still getting issue , I want when the mouse goes away from text of QLabel then The mouse cursor should change back to Arrow

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @Qt-Enthusiast
          Hi you can use
          virtual void leaveEvent(QEvent * event)
          to reset cursor.

          1 Reply Last reply
          1
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #6

            @mrjj
            Here is following code

            class myLabel: public QLabel
            {

            public:
            myLabel(QString string)
            {
            this->setText(string);
            this->setTextInteractionFlags (Qt::TextSelectableByMouse);

              //set MouseTracking true to capture mouse event even its key is not pressed
              this->setMouseTracking(true);
            };
            ~ myLabel(){};
            
            void enterEvent ( QEvent * event )
            {
                  setCursor(Qt::IBeamCursor);
                  QLabel::enterEvent(event);
              //Show x and y coordinate values of mouse cursor here
              //this->setText("Select the text");
            };
            
                void leaveEvent(QEvent * event)
                {
                  setCursor(Qt::ArrowCursor);
                  QLabel::leaveEvent(event);
            
                }  
            

            In this I am getting Qt::IBeamCursor on whole QLabel and I want the Qt::IBeamCursor should only on text of QLabel and then when I leave the text of I should get (Qt::ArrowCursor
            but how can Qt::IBeamCursor only for text portion of QLabel not the whoe QLabel

            mrjjM 1 Reply Last reply
            1
            • Q Qt Enthusiast

              @mrjj
              Here is following code

              class myLabel: public QLabel
              {

              public:
              myLabel(QString string)
              {
              this->setText(string);
              this->setTextInteractionFlags (Qt::TextSelectableByMouse);

                //set MouseTracking true to capture mouse event even its key is not pressed
                this->setMouseTracking(true);
              };
              ~ myLabel(){};
              
              void enterEvent ( QEvent * event )
              {
                    setCursor(Qt::IBeamCursor);
                    QLabel::enterEvent(event);
                //Show x and y coordinate values of mouse cursor here
                //this->setText("Select the text");
              };
              
                  void leaveEvent(QEvent * event)
                  {
                    setCursor(Qt::ArrowCursor);
                    QLabel::leaveEvent(event);
              
                  }  
              

              In this I am getting Qt::IBeamCursor on whole QLabel and I want the Qt::IBeamCursor should only on text of QLabel and then when I leave the text of I should get (Qt::ArrowCursor
              but how can Qt::IBeamCursor only for text portion of QLabel not the whoe QLabel

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #7

              @Qt-Enthusiast
              setCursor works on whole widget so unless u know where text is draw and only change when over the text it will change when over the whole label.
              You could use http://doc.qt.io/qt-5/qfontmetrics.html
              and
              http://doc.qt.io/qt-5/qfontmetrics.html#boundingRect-1
              to know the text area and store and only setCursor when inside that and reset when outside.
              (use mousemove)

              Since there is no signal for user finishing edit in a label as far as i know then you might get issues
              knowing when u re-calculate the text rect.
              But you could override paint and call QLabel::paint(xxx); at the top and then
              store the text rect. That should work.

              1 Reply Last reply
              1
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by
                #8

                A small example could be helpful for me

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #9

                  Hi
                  Text has to be top aligned but otherwise it works ok

                  #include<QLabel>
                  #include<QFontMetrics>
                  
                  class myLabel: public QLabel {
                    QRect textArea;
                   public:
                    myLabel(QString string) {
                      this->setText(string);
                      this->setTextInteractionFlags (Qt::TextSelectableByMouse);
                      setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignTop); // WONT WORK IF CENTERED
                      //set MouseTracking true to capture mouse event even its key is not pressed
                      this->setMouseTracking(true);
                    };
                    ~ myLabel() {}
                  
                   protected:
                    virtual void paintEvent(QPaintEvent* event) {
                      QLabel::paintEvent(event); // call base class
                      QFontMetrics fm(font()); // give it the font used
                      auto r = fm.boundingRect(text()); // get the area of the text. 
                      textArea.setCoords(0, 0, r.width(), r.height()); // set variable
                      qDebug() << "text:" << textArea;
                    }
                  
                    virtual void mouseMoveEvent(QMouseEvent* event)  {
                      auto t = mapFromGlobal( QCursor::pos() ); // get mouse pos, translate to x,y usefull inside widget
                      qDebug() << "MOUSEPOS:" << t << " area:" << textArea ;
                      if ( textArea.contains( t ) ) // is the point inside the rect
                      { setCursor(Qt::IBeamCursor); }
                      else
                      { setCursor(Qt::ArrowCursor); }
                    }
                  };
                  
                  
                  1 Reply Last reply
                  1
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by
                    #10

                    I tried this I am noy able to select the text am I missing some thing

                    mrjjM 1 Reply Last reply
                    0
                    • Q Qt Enthusiast

                      I tried this I am noy able to select the text am I missing some thing

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @Qt-Enthusiast
                      I can only select by clicking word. Not by dragging.
                      Dont know why.

                      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