how to change the mouse cursor that shows QLable is selectable
-
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
-
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
@Qt-Enthusiast Hi, friend, welcome.
Text
is selectable is different withQLabel
is selectable. They are two conpect.You can reimplement
mouseMoveEvent of QLabel
, in function, you can changed some state ofQLable
when use move the mouse hoverQLabel
.Note:
setMouseTracking(true) for QLabel
. -
@Qt-Enthusiast Hi, friend, welcome.
Text
is selectable is different withQLabel
is selectable. They are two conpect.You can reimplement
mouseMoveEvent of QLabel
, in function, you can changed some state ofQLable
when use move the mouse hoverQLabel
.Note:
setMouseTracking(true) for QLabel
.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
-
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
@Qt-Enthusiast
Hi you can use
virtual void leaveEvent(QEvent * event)
to reset cursor. -
@mrjj
Here is following codeclass 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 -
@mrjj
Here is following codeclass 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@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. -
A small example could be helpful for me
-
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); } } };
-
I tried this I am noy able to select the text am I missing some thing
-
I tried this I am noy able to select the text am I missing some thing
@Qt-Enthusiast
I can only select by clicking word. Not by dragging.
Dont know why.