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.9k 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.
  • SGaistS SGaist

    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 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
    0
    • C Collaxd

      @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 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
      0
      • SGaistS SGaist

        @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 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
        0
        • C Collaxd

          @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 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
          1
          • C Collaxd

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

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

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

                JonBJ 1 Reply Last reply
                0
                • C Collaxd

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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
                  1
                  • JonBJ JonB

                    @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 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
                    0
                    • C Collaxd

                      @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 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
                      0
                      • M mpergand

                        @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 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
                        0
                        • C Collaxd

                          @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 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
                          1
                          • M mpergand

                            @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 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
                            2
                            • C Collaxd has marked this topic as solved on
                            • C Collaxd

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

                              • Login

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