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. Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels
Forum Updated to NodeBB v4.3 + New Features

Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 6 Posters 1.0k Views 2 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.
  • C Offline
    C Offline
    Collaxd
    wrote on 21 Mar 2023, 17:43 last edited by
    #1

    I am working on a Qt application that contains multiple labels, and upon clicking on a label, its background color should change to a previously selected color. However, I am facing two issues:

    When I click on a label, the mouse pointer becomes stuck on that label, and I am unable to click on other nearby labels.

    The labels surrounding the clicked label do not change their background color, only the clicked label changes its color.

    The code is based on the HandLabel class, which inherits from QLabel. The mousePressEvent() method is responsible for changing the color of the label upon clicking it, and the onbgColorChanged() method is called to change the color when a new color is selected. Additionally, the enterEvent() and leaveEvent() methods enable and disable mouse tracking, and the mouseMoveEvent() method is for debugging purposes only.

    I would appreciate any help in resolving these issues.

    #include "HandLabel.h"
    
    HandLabel::HandLabel(const QString &text, QWidget *parent, Qt::WindowFlags f)
        : QLabel(text, parent, f)
    {
        connect(parent, SIGNAL(bgColorChanged(QColor)), this, SLOT(onbgColorChanged(QColor)));
        setAlignment(Qt::AlignCenter);
        setFrameStyle(QFrame::Panel | QFrame::Sunken);
        setAutoFillBackground(true);
    }
    
    
    void HandLabel::onbgColorChanged(QColor newColor)
    {
        setBgColor(newColor);
    }
    
    void HandLabel::mousePressEvent(QMouseEvent *event)
    {
        Q_UNUSED(event);
    
        QPalette pal = palette();
        QColor selectedColor = pal.color(QPalette::Window);
    
        if (m_bgColor == selectedColor) {
            pal.setColor(QPalette::Window, Qt::transparent);
        } else {
            pal.setColor(QPalette::Window, m_bgColor);
        }
        setPalette(pal);
    }
    
    void HandLabel::mouseMoveEvent(QMouseEvent *event)
    {
        qDebug() << event;
    }
    
    void HandLabel::enterEvent(QEnterEvent *event)
    {
        QLabel::enterEvent(event); // chama o enterEvent da classe base
        setMouseTracking(true); // habilita o rastreamento do mouse
        if (event->buttons() & Qt::LeftButton) {
            QPalette pal = palette();
            QColor selectedColor = pal.color(QPalette::Window);
            if (m_bgColor != selectedColor) {
                pal.setColor(QPalette::Window, m_bgColor);
                setPalette(pal);
            }
        }
    }
    
    void HandLabel::leaveEvent(QEvent *event)
    {
        QLabel::leaveEvent(event); // chama o leaveEvent da classe base
        setMouseTracking(false); // desabilita o rastreamento do mouse
    }
    
    QColor HandLabel::bgColor() const
    {
        return m_bgColor;
    }
    
    void HandLabel::setBgColor(const QColor &newBgColor)
    {
        m_bgColor = newBgColor;
    }
    
    

    example1

    J 1 Reply Last reply 21 Mar 2023, 18:34
    0
    • C Collaxd
      22 Mar 2023, 01:01

      @mpergand

      void HandLabel::dragEnterEvent(QDragEnterEvent *event)
      {
          qDebug() << event;
          QLabel::dragEnterEvent(event);
      }
      
      void HandLabel::dragMoveEvent(QDragMoveEvent *event)
      {
          qDebug() << event;
          QLabel::dragMoveEvent(event);
      }
      

      Thanks for your help, but it doesn't appear in the debug when I add the two functions :(

      I managed to do something similar in React + JS but I put the lstenner in the parent layout which is not possible here since the parent layout is a grid and has no mouse properties

      M Offline
      M Offline
      mpergand
      wrote on 22 Mar 2023, 10:35 last edited by mpergand
      #16

      @Collaxd
      Seems to work:

      class Label : public QLabel
      {
          public:
          Label(const QString& text, QWidget* parent=nullptr) : QLabel(text,parent)
          {
          setFrameStyle(QFrame::Panel | QFrame::Sunken);
          setAcceptDrops(true);
          }
      
          void mousePressEvent(QMouseEvent *ev) override
          {
               qDebug()<<Q_FUNC_INFO;
               QLabel::mousePressEvent(ev);
          }
      
          void mouseMoveEvent(QMouseEvent *ev) override
          {
               qDebug()<<Q_FUNC_INFO;
              QDrag *drag = new QDrag(this);
              QMimeData *mimeData = new QMimeData;
      
              mimeData->setText("");
              drag->setMimeData(mimeData);
      
              Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
              if(  dropAction == Qt::IgnoreAction )
              {
                  qDebug()<<"end drag";
                  drag->deleteLater();
              }
          }
      
      
          void dragEnterEvent(QDragEnterEvent* ev ) override
          {
              qDebug()<<Q_FUNC_INFO;
               ev->acceptProposedAction();
          }
      
          void dragMoveEvent(QDragMoveEvent* ev ) override
          {
              qDebug()<<Q_FUNC_INFO<<ev->source()<<this;
      
          }
      };
      

      Each Label receives dragMoveEvent when the mouse is over.
      Looks promising, have a look at the doc about drag&drop:
      https://doc.qt.io/qt-5/dnd.html

      C 1 Reply Last reply 23 Mar 2023, 01:56
      1
      • C Collaxd
        21 Mar 2023, 17:43

        I am working on a Qt application that contains multiple labels, and upon clicking on a label, its background color should change to a previously selected color. However, I am facing two issues:

        When I click on a label, the mouse pointer becomes stuck on that label, and I am unable to click on other nearby labels.

        The labels surrounding the clicked label do not change their background color, only the clicked label changes its color.

        The code is based on the HandLabel class, which inherits from QLabel. The mousePressEvent() method is responsible for changing the color of the label upon clicking it, and the onbgColorChanged() method is called to change the color when a new color is selected. Additionally, the enterEvent() and leaveEvent() methods enable and disable mouse tracking, and the mouseMoveEvent() method is for debugging purposes only.

        I would appreciate any help in resolving these issues.

        #include "HandLabel.h"
        
        HandLabel::HandLabel(const QString &text, QWidget *parent, Qt::WindowFlags f)
            : QLabel(text, parent, f)
        {
            connect(parent, SIGNAL(bgColorChanged(QColor)), this, SLOT(onbgColorChanged(QColor)));
            setAlignment(Qt::AlignCenter);
            setFrameStyle(QFrame::Panel | QFrame::Sunken);
            setAutoFillBackground(true);
        }
        
        
        void HandLabel::onbgColorChanged(QColor newColor)
        {
            setBgColor(newColor);
        }
        
        void HandLabel::mousePressEvent(QMouseEvent *event)
        {
            Q_UNUSED(event);
        
            QPalette pal = palette();
            QColor selectedColor = pal.color(QPalette::Window);
        
            if (m_bgColor == selectedColor) {
                pal.setColor(QPalette::Window, Qt::transparent);
            } else {
                pal.setColor(QPalette::Window, m_bgColor);
            }
            setPalette(pal);
        }
        
        void HandLabel::mouseMoveEvent(QMouseEvent *event)
        {
            qDebug() << event;
        }
        
        void HandLabel::enterEvent(QEnterEvent *event)
        {
            QLabel::enterEvent(event); // chama o enterEvent da classe base
            setMouseTracking(true); // habilita o rastreamento do mouse
            if (event->buttons() & Qt::LeftButton) {
                QPalette pal = palette();
                QColor selectedColor = pal.color(QPalette::Window);
                if (m_bgColor != selectedColor) {
                    pal.setColor(QPalette::Window, m_bgColor);
                    setPalette(pal);
                }
            }
        }
        
        void HandLabel::leaveEvent(QEvent *event)
        {
            QLabel::leaveEvent(event); // chama o leaveEvent da classe base
            setMouseTracking(false); // desabilita o rastreamento do mouse
        }
        
        QColor HandLabel::bgColor() const
        {
            return m_bgColor;
        }
        
        void HandLabel::setBgColor(const QColor &newBgColor)
        {
            m_bgColor = newBgColor;
        }
        
        

        example1

        J Offline
        J Offline
        JonB
        wrote on 21 Mar 2023, 18:34 last edited by
        #2

        @Collaxd said in Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels:

        When I click on a label, the mouse pointer becomes stuck on that label, and I am unable to click on other nearby labels.

        Maybe because you do not call the base class implementation for your mouse...Event() overrides?
        Separately I don't know whether qDebug() output on every mouse move makes it sluggish.

        C 1 Reply Last reply 21 Mar 2023, 19:56
        0
        • J JonB
          21 Mar 2023, 18:34

          @Collaxd said in Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels:

          When I click on a label, the mouse pointer becomes stuck on that label, and I am unable to click on other nearby labels.

          Maybe because you do not call the base class implementation for your mouse...Event() overrides?
          Separately I don't know whether qDebug() output on every mouse move makes it sluggish.

          C Offline
          C Offline
          Collaxd
          wrote on 21 Mar 2023, 19:56 last edited by
          #3

          @JonB

          protected:
              void mousePressEvent(QMouseEvent *event) override;
              void mouseMoveEvent(QMouseEvent *event) override;
              void enterEvent(QEnterEvent *event) override;
              void leaveEvent(QEvent *event) override;
          

          thanks for your answer yes and it does everything just using override, any suggestions?

          SGaistS 1 Reply Last reply 21 Mar 2023, 19:59
          0
          • C Collaxd
            21 Mar 2023, 19:56

            @JonB

            protected:
                void mousePressEvent(QMouseEvent *event) override;
                void mouseMoveEvent(QMouseEvent *event) override;
                void enterEvent(QEnterEvent *event) override;
                void leaveEvent(QEvent *event) override;
            

            thanks for your answer yes and it does everything just using override, any suggestions?

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 21 Mar 2023, 19:59 last edited by
            #4

            Hi,

            @JonB was not talking about the declaration of the functions but calling the base class implementation in your reimplementation.

            On an unrelated note, your use of parent to do the connection in the constructor is bad. That connection should happen in the parent. A child widget should know nothing about its parent.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            C 1 Reply Last reply 21 Mar 2023, 20:07
            0
            • SGaistS SGaist
              21 Mar 2023, 19:59

              Hi,

              @JonB was not talking about the declaration of the functions but calling the base class implementation in your reimplementation.

              On an unrelated note, your use of parent to do the connection in the constructor is bad. That connection should happen in the parent. A child widget should know nothing about its parent.

              C Offline
              C Offline
              Collaxd
              wrote on 21 Mar 2023, 20:07 last edited by Collaxd
              #5

              @SGaist
              thanks for your answer. I understand better now, I'm learning and I don't know how the best declaration was, so I chose to do it inside the child, but now I've implemented it in the creation of the labels
              but the problem persists

              Sorry but I still can't understand the implementation part, I'm a little new to cpp sorry

              
              MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent)
                  , ui(new Ui::MainWindow)
                  , m_hands (readHands())
              {
                  ui->setupUi(this);
              
                  int row=0;
                  int col=0;
                  foreach (QString hand, m_hands) {
                      m_label = new HandLabel(hand, this);
                      connect(this, SIGNAL(bgColorChanged(QColor)), m_label, SLOT(onbgColorChanged(QColor)));
              
                      ui->handsContainer->addWidget(m_label, row, col);
                      
                      if (col < 12) col++;
                      else {
                          col=0;
                          row++;
                      }
                  }
              
                  emit bgColorChanged(Qt::yellow);
              }```
              SGaistS 1 Reply Last reply 21 Mar 2023, 20:13
              0
              • C Collaxd
                21 Mar 2023, 20:07

                @SGaist
                thanks for your answer. I understand better now, I'm learning and I don't know how the best declaration was, so I chose to do it inside the child, but now I've implemented it in the creation of the labels
                but the problem persists

                Sorry but I still can't understand the implementation part, I'm a little new to cpp sorry

                
                MainWindow::MainWindow(QWidget *parent)
                    : QMainWindow(parent)
                    , ui(new Ui::MainWindow)
                    , m_hands (readHands())
                {
                    ui->setupUi(this);
                
                    int row=0;
                    int col=0;
                    foreach (QString hand, m_hands) {
                        m_label = new HandLabel(hand, this);
                        connect(this, SIGNAL(bgColorChanged(QColor)), m_label, SLOT(onbgColorChanged(QColor)));
                
                        ui->handsContainer->addWidget(m_label, row, col);
                        
                        if (col < 12) col++;
                        else {
                            col=0;
                            row++;
                        }
                    }
                
                    emit bgColorChanged(Qt::yellow);
                }```
                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 21 Mar 2023, 20:13 last edited by
                #6

                @Collaxd as I said, it was an unrelated note.

                For your issue: do you know what "calling the base class implementation" means ? That's key to answer @JonB's question.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                C 1 Reply Last reply 21 Mar 2023, 20:17
                0
                • SGaistS SGaist
                  21 Mar 2023, 20:13

                  @Collaxd as I said, it was an unrelated note.

                  For your issue: do you know what "calling the base class implementation" means ? That's key to answer @JonB's question.

                  C Offline
                  C Offline
                  Collaxd
                  wrote on 21 Mar 2023, 20:17 last edited by
                  #7

                  @SGaist said in Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels:

                  For your issue: do you know what "calling the base class implementation" means ? That's key to answer @JonB's question.

                  no, i even asked gpt chat and tried to learn but i don't understand what jon is getting at, could you simplify the question so i can improve my example?

                  Christian EhrlicherC JoeCFDJ SGaistS 3 Replies Last reply 21 Mar 2023, 20:21
                  0
                  • C Collaxd
                    21 Mar 2023, 20:17

                    @SGaist said in Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels:

                    For your issue: do you know what "calling the base class implementation" means ? That's key to answer @JonB's question.

                    no, i even asked gpt chat and tried to learn but i don't understand what jon is getting at, could you simplify the question so i can improve my example?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 21 Mar 2023, 20:21 last edited by
                    #8

                    @Collaxd said in Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels:

                    no, i even asked gpt chat

                    I really wonder where such stuff will end... very staggering.

                    When you don't want to read a good c++ book then you can ask google for 'calling the base class implementation'.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    C 1 Reply Last reply 21 Mar 2023, 20:30
                    1
                    • C Collaxd
                      21 Mar 2023, 20:17

                      @SGaist said in Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels:

                      For your issue: do you know what "calling the base class implementation" means ? That's key to answer @JonB's question.

                      no, i even asked gpt chat and tried to learn but i don't understand what jon is getting at, could you simplify the question so i can improve my example?

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on 21 Mar 2023, 20:22 last edited by
                      #9

                      @Collaxd

                      void HandLabel::mousePressEvent(QMouseEvent *event)
                      {
                          Q_UNUSED(event);
                      
                          QPalette pal = palette();
                          QColor selectedColor = pal.color(QPalette::Window);
                      
                          if (m_bgColor == selectedColor) {
                              pal.setColor(QPalette::Window, Qt::transparent);
                          } else {
                              pal.setColor(QPalette::Window, m_bgColor);
                          }
                          setPalette(pal);
                      
                         /* something like this */
                         QLabel::mousePressEvent( event );
                      }
                      
                      1 Reply Last reply
                      0
                      • C Collaxd
                        21 Mar 2023, 20:17

                        @SGaist said in Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels:

                        For your issue: do you know what "calling the base class implementation" means ? That's key to answer @JonB's question.

                        no, i even asked gpt chat and tried to learn but i don't understand what jon is getting at, could you simplify the question so i can improve my example?

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 21 Mar 2023, 20:23 last edited by
                        #10

                        @Collaxd you better go with a good old fashioned cpp tutorial about that very subject.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • Christian EhrlicherC Christian Ehrlicher
                          21 Mar 2023, 20:21

                          @Collaxd said in Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels:

                          no, i even asked gpt chat

                          I really wonder where such stuff will end... very staggering.

                          When you don't want to read a good c++ book then you can ask google for 'calling the base class implementation'.

                          C Offline
                          C Offline
                          Collaxd
                          wrote on 21 Mar 2023, 20:30 last edited by Collaxd
                          #11

                          @Christian-Ehrlicher
                          Thank you, friends. I really didn't mean to offend when I mentioned the chat. In fact, I'm just a veterinarian enthusiast in programming trying to learn some things. I'll do more research.

                          J 1 Reply Last reply 21 Mar 2023, 21:27
                          0
                          • C Collaxd
                            21 Mar 2023, 20:30

                            @Christian-Ehrlicher
                            Thank you, friends. I really didn't mean to offend when I mentioned the chat. In fact, I'm just a veterinarian enthusiast in programming trying to learn some things. I'll do more research.

                            J Offline
                            J Offline
                            JonB
                            wrote on 21 Mar 2023, 21:27 last edited by JonB
                            #12

                            @Collaxd
                            No offence taken.

                            In, say, your HandLabel::enterEvent() overridden method you call

                            QLabel::enterEvent(event); // chama o enterEvent da classe base

                            Same for leaveEvent(). It allows whatever the class you derive from to see these events and act on them, as well as your own code.

                            I would guess you need to do the same for your HandLabel::mousePressEvent(). I would let it call QLabel::mousePressEvent(event), as well as your code. Maybe also for mouseMoveEvent(). Does that help with your

                            When I click on a label, the mouse pointer becomes stuck on that label, and I am unable to click on other nearby labels.

                            ?

                            C 1 Reply Last reply 21 Mar 2023, 22:14
                            1
                            • J JonB
                              21 Mar 2023, 21:27

                              @Collaxd
                              No offence taken.

                              In, say, your HandLabel::enterEvent() overridden method you call

                              QLabel::enterEvent(event); // chama o enterEvent da classe base

                              Same for leaveEvent(). It allows whatever the class you derive from to see these events and act on them, as well as your own code.

                              I would guess you need to do the same for your HandLabel::mousePressEvent(). I would let it call QLabel::mousePressEvent(event), as well as your code. Maybe also for mouseMoveEvent(). Does that help with your

                              When I click on a label, the mouse pointer becomes stuck on that label, and I am unable to click on other nearby labels.

                              ?

                              C Offline
                              C Offline
                              Collaxd
                              wrote on 21 Mar 2023, 22:14 last edited by Collaxd
                              #13

                              @JonB
                              Perfect, thanks for your understanding and help, but even when calling the base event, the mouse is still 'stuck' on the initial click label, I can do something just with enter, it also serves my purpose, but it doesn't work in the same way

                              Here's an example of how my code is and an app I want to 'imitate' with it working

                              #include "HandLabel.h"
                              
                              #include <qapplication.h>
                              
                              HandLabel::HandLabel(const QString &text, QWidget *parent, Qt::WindowFlags f)
                                  : QLabel(text, parent, f)
                              {
                                  setAlignment(Qt::AlignCenter);
                                  setFrameStyle(QFrame::Panel | QFrame::Sunken);
                                  setAutoFillBackground(true);
                              }
                              
                              
                              void HandLabel::onbgColorChanged(QColor newColor)
                              {
                                  setBgColor(newColor);
                              }
                              
                              void HandLabel::mousePressEvent(QMouseEvent *event)
                              {
                                  QLabel::mousePressEvent(event); // chama o mousePressEvent da classe base
                              
                                  if (event->buttons() & Qt::LeftButton)
                                      updateBackground();
                              
                              }
                              
                              void HandLabel::mouseMoveEvent(QMouseEvent *event)
                              {
                                  QLabel::mouseMoveEvent(event);
                              
                                  qDebug() << event;
                              //    updateBackground();
                              
                              }
                              
                              void HandLabel::enterEvent(QEnterEvent *event)
                              {
                                  QLabel::enterEvent(event); // chama o enterEvent da classe base
                                  updateBackground();
                                  setMouseTracking(true);
                              }
                              
                              void HandLabel::leaveEvent(QEvent *event)
                              {
                                  QLabel::leaveEvent(event); // chama o leaveEvent da classe base
                                  // emite um evento mouseReleaseEvent
                                  setMouseTracking(false);
                              }
                              
                              void HandLabel::updateBackground()
                              {
                                  QPalette pal = palette();
                                  QColor selectedColor = pal.color(QPalette::Window);
                                  if (m_bgColor == selectedColor)
                                      pal.setColor(QPalette::Window, Qt::transparent);
                                  else
                                      pal.setColor(QPalette::Window, m_bgColor);
                              
                                  setPalette(pal);
                              }
                              
                              
                              QColor HandLabel::bgColor() const
                              {
                                  return m_bgColor;
                              }
                              
                              void HandLabel::setBgColor(const QColor &newBgColor)
                              {
                                  m_bgColor = newBgColor;
                              }
                              
                              

                              alt text

                              M 1 Reply Last reply 21 Mar 2023, 22:41
                              0
                              • C Collaxd
                                21 Mar 2023, 22:14

                                @JonB
                                Perfect, thanks for your understanding and help, but even when calling the base event, the mouse is still 'stuck' on the initial click label, I can do something just with enter, it also serves my purpose, but it doesn't work in the same way

                                Here's an example of how my code is and an app I want to 'imitate' with it working

                                #include "HandLabel.h"
                                
                                #include <qapplication.h>
                                
                                HandLabel::HandLabel(const QString &text, QWidget *parent, Qt::WindowFlags f)
                                    : QLabel(text, parent, f)
                                {
                                    setAlignment(Qt::AlignCenter);
                                    setFrameStyle(QFrame::Panel | QFrame::Sunken);
                                    setAutoFillBackground(true);
                                }
                                
                                
                                void HandLabel::onbgColorChanged(QColor newColor)
                                {
                                    setBgColor(newColor);
                                }
                                
                                void HandLabel::mousePressEvent(QMouseEvent *event)
                                {
                                    QLabel::mousePressEvent(event); // chama o mousePressEvent da classe base
                                
                                    if (event->buttons() & Qt::LeftButton)
                                        updateBackground();
                                
                                }
                                
                                void HandLabel::mouseMoveEvent(QMouseEvent *event)
                                {
                                    QLabel::mouseMoveEvent(event);
                                
                                    qDebug() << event;
                                //    updateBackground();
                                
                                }
                                
                                void HandLabel::enterEvent(QEnterEvent *event)
                                {
                                    QLabel::enterEvent(event); // chama o enterEvent da classe base
                                    updateBackground();
                                    setMouseTracking(true);
                                }
                                
                                void HandLabel::leaveEvent(QEvent *event)
                                {
                                    QLabel::leaveEvent(event); // chama o leaveEvent da classe base
                                    // emite um evento mouseReleaseEvent
                                    setMouseTracking(false);
                                }
                                
                                void HandLabel::updateBackground()
                                {
                                    QPalette pal = palette();
                                    QColor selectedColor = pal.color(QPalette::Window);
                                    if (m_bgColor == selectedColor)
                                        pal.setColor(QPalette::Window, Qt::transparent);
                                    else
                                        pal.setColor(QPalette::Window, m_bgColor);
                                
                                    setPalette(pal);
                                }
                                
                                
                                QColor HandLabel::bgColor() const
                                {
                                    return m_bgColor;
                                }
                                
                                void HandLabel::setBgColor(const QColor &newBgColor)
                                {
                                    m_bgColor = newBgColor;
                                }
                                
                                

                                alt text

                                M Offline
                                M Offline
                                mpergand
                                wrote on 21 Mar 2023, 22:41 last edited by mpergand
                                #14

                                @Collaxd said in Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels:

                                he mouse is still 'stuck' on the initial click label,

                                I think you should use dragEnterEvent & dragMoveEvent instead of enterEvent, if you want to select muti objects while mouse press.

                                C 1 Reply Last reply 22 Mar 2023, 01:01
                                0
                                • M mpergand
                                  21 Mar 2023, 22:41

                                  @Collaxd said in Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels:

                                  he mouse is still 'stuck' on the initial click label,

                                  I think you should use dragEnterEvent & dragMoveEvent instead of enterEvent, if you want to select muti objects while mouse press.

                                  C Offline
                                  C Offline
                                  Collaxd
                                  wrote on 22 Mar 2023, 01:01 last edited by Collaxd
                                  #15

                                  @mpergand

                                  void HandLabel::dragEnterEvent(QDragEnterEvent *event)
                                  {
                                      qDebug() << event;
                                      QLabel::dragEnterEvent(event);
                                  }
                                  
                                  void HandLabel::dragMoveEvent(QDragMoveEvent *event)
                                  {
                                      qDebug() << event;
                                      QLabel::dragMoveEvent(event);
                                  }
                                  

                                  Thanks for your help, but it doesn't appear in the debug when I add the two functions :(

                                  I managed to do something similar in React + JS but I put the lstenner in the parent layout which is not possible here since the parent layout is a grid and has no mouse properties

                                  M 1 Reply Last reply 22 Mar 2023, 10:35
                                  0
                                  • C Collaxd
                                    22 Mar 2023, 01:01

                                    @mpergand

                                    void HandLabel::dragEnterEvent(QDragEnterEvent *event)
                                    {
                                        qDebug() << event;
                                        QLabel::dragEnterEvent(event);
                                    }
                                    
                                    void HandLabel::dragMoveEvent(QDragMoveEvent *event)
                                    {
                                        qDebug() << event;
                                        QLabel::dragMoveEvent(event);
                                    }
                                    

                                    Thanks for your help, but it doesn't appear in the debug when I add the two functions :(

                                    I managed to do something similar in React + JS but I put the lstenner in the parent layout which is not possible here since the parent layout is a grid and has no mouse properties

                                    M Offline
                                    M Offline
                                    mpergand
                                    wrote on 22 Mar 2023, 10:35 last edited by mpergand
                                    #16

                                    @Collaxd
                                    Seems to work:

                                    class Label : public QLabel
                                    {
                                        public:
                                        Label(const QString& text, QWidget* parent=nullptr) : QLabel(text,parent)
                                        {
                                        setFrameStyle(QFrame::Panel | QFrame::Sunken);
                                        setAcceptDrops(true);
                                        }
                                    
                                        void mousePressEvent(QMouseEvent *ev) override
                                        {
                                             qDebug()<<Q_FUNC_INFO;
                                             QLabel::mousePressEvent(ev);
                                        }
                                    
                                        void mouseMoveEvent(QMouseEvent *ev) override
                                        {
                                             qDebug()<<Q_FUNC_INFO;
                                            QDrag *drag = new QDrag(this);
                                            QMimeData *mimeData = new QMimeData;
                                    
                                            mimeData->setText("");
                                            drag->setMimeData(mimeData);
                                    
                                            Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
                                            if(  dropAction == Qt::IgnoreAction )
                                            {
                                                qDebug()<<"end drag";
                                                drag->deleteLater();
                                            }
                                        }
                                    
                                    
                                        void dragEnterEvent(QDragEnterEvent* ev ) override
                                        {
                                            qDebug()<<Q_FUNC_INFO;
                                             ev->acceptProposedAction();
                                        }
                                    
                                        void dragMoveEvent(QDragMoveEvent* ev ) override
                                        {
                                            qDebug()<<Q_FUNC_INFO<<ev->source()<<this;
                                    
                                        }
                                    };
                                    

                                    Each Label receives dragMoveEvent when the mouse is over.
                                    Looks promising, have a look at the doc about drag&drop:
                                    https://doc.qt.io/qt-5/dnd.html

                                    C 1 Reply Last reply 23 Mar 2023, 01:56
                                    1
                                    • M mpergand
                                      22 Mar 2023, 10:35

                                      @Collaxd
                                      Seems to work:

                                      class Label : public QLabel
                                      {
                                          public:
                                          Label(const QString& text, QWidget* parent=nullptr) : QLabel(text,parent)
                                          {
                                          setFrameStyle(QFrame::Panel | QFrame::Sunken);
                                          setAcceptDrops(true);
                                          }
                                      
                                          void mousePressEvent(QMouseEvent *ev) override
                                          {
                                               qDebug()<<Q_FUNC_INFO;
                                               QLabel::mousePressEvent(ev);
                                          }
                                      
                                          void mouseMoveEvent(QMouseEvent *ev) override
                                          {
                                               qDebug()<<Q_FUNC_INFO;
                                              QDrag *drag = new QDrag(this);
                                              QMimeData *mimeData = new QMimeData;
                                      
                                              mimeData->setText("");
                                              drag->setMimeData(mimeData);
                                      
                                              Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
                                              if(  dropAction == Qt::IgnoreAction )
                                              {
                                                  qDebug()<<"end drag";
                                                  drag->deleteLater();
                                              }
                                          }
                                      
                                      
                                          void dragEnterEvent(QDragEnterEvent* ev ) override
                                          {
                                              qDebug()<<Q_FUNC_INFO;
                                               ev->acceptProposedAction();
                                          }
                                      
                                          void dragMoveEvent(QDragMoveEvent* ev ) override
                                          {
                                              qDebug()<<Q_FUNC_INFO<<ev->source()<<this;
                                      
                                          }
                                      };
                                      

                                      Each Label receives dragMoveEvent when the mouse is over.
                                      Looks promising, have a look at the doc about drag&drop:
                                      https://doc.qt.io/qt-5/dnd.html

                                      C Offline
                                      C Offline
                                      Collaxd
                                      wrote on 23 Mar 2023, 01:56 last edited by
                                      #17

                                      @mpergand
                                      I would like to thank you very much for your solution, it was a success! I've been searching for days and couldn't find anything similar, which generated a lot of learning and made me realize that I still have a lot to study!

                                      For future readers, here is the solution:

                                      void HandLabel::mouseMoveEvent(QMouseEvent *event)
                                      {
                                          Q_UNUSED(event);
                                      
                                          QDrag *drag = new QDrag(this);
                                      
                                          QMimeData *mimeData = new QMimeData;
                                          drag->setMimeData(mimeData);
                                      
                                          Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
                                          if(  dropAction == Qt::IgnoreAction ) {
                                              drag->deleteLater();
                                          }
                                      }
                                      
                                      void HandLabel::dragEnterEvent(QDragEnterEvent *event)
                                      {
                                          Q_UNUSED(event);
                                          updateColorScheme();
                                      }
                                      

                                      Despite the mouse having a 'block' symbol, the colors are working perfectly!

                                      alt text

                                      M 1 Reply Last reply 23 Mar 2023, 11:16
                                      2
                                      • C Collaxd has marked this topic as solved on 23 Mar 2023, 01:56
                                      • C Collaxd
                                        23 Mar 2023, 01:56

                                        @mpergand
                                        I would like to thank you very much for your solution, it was a success! I've been searching for days and couldn't find anything similar, which generated a lot of learning and made me realize that I still have a lot to study!

                                        For future readers, here is the solution:

                                        void HandLabel::mouseMoveEvent(QMouseEvent *event)
                                        {
                                            Q_UNUSED(event);
                                        
                                            QDrag *drag = new QDrag(this);
                                        
                                            QMimeData *mimeData = new QMimeData;
                                            drag->setMimeData(mimeData);
                                        
                                            Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
                                            if(  dropAction == Qt::IgnoreAction ) {
                                                drag->deleteLater();
                                            }
                                        }
                                        
                                        void HandLabel::dragEnterEvent(QDragEnterEvent *event)
                                        {
                                            Q_UNUSED(event);
                                            updateColorScheme();
                                        }
                                        

                                        Despite the mouse having a 'block' symbol, the colors are working perfectly!

                                        alt text

                                        M Offline
                                        M Offline
                                        mpergand
                                        wrote on 23 Mar 2023, 11:16 last edited by mpergand
                                        #18

                                        @Collaxd said in Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels:

                                        void HandLabel::dragEnterEvent(QDragEnterEvent *event)
                                        {
                                        Q_UNUSED(event);
                                        updateColorScheme();
                                        }

                                        Oh, miss that drag enter is call for each label !
                                        Even easier that I imagined.

                                        Despite the mouse having a 'block' symbol, the colors are working perfectly!

                                        You need to call ev->acceptProposedAction();
                                        for that symbol to not appear.

                                        1 Reply Last reply
                                        1

                                        1/18

                                        21 Mar 2023, 17:43

                                        • Login

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