Mouse Pointer and Label Background Coloring in Qt Application with Multiple Labels
-
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 ); }
-
@Collaxd you better go with a good old fashioned cpp tutorial about that very subject.
-
@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. -
@Collaxd
No offence taken.In, say, your
HandLabel::enterEvent()
overridden method you callQLabel::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 callQLabel::mousePressEvent(event)
, as well as your code. Maybe also formouseMoveEvent()
. Does that help with yourWhen I click on a label, the mouse pointer becomes stuck on that label, and I am unable to click on other nearby labels.
?
-
@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 wayHere'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; }
-
@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.
-
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
-
@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 -
@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!
-
-
@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.