Set consistent mouse cursor for QWidget - qt

I have sub-classed QWidget to draw on it using mouse.
I use setCursor to change its cursor to cross shape.
It is working fine but as soon as I press the mouse button on it (for example to draw freehand line), the cursor changes back to application cursor.
Note that I do not want to use setOverrideCursor on mouseenter event for example because I want to change cursor only for this widget and not for entire application, and I have a better workaround (as follows) anyways.
My current solution is to use
setCursor(cursor());
in my overridden mousePressEvent(QMouseEvent * event) and mouseDoubleClickEvent(QMouseEvent * event)
The latter is because for some reason double click also changes the cursor to the application cursor for a moment!
The workaround works :) but I would like to see if there is any better solution, that asks QT not to change the cursor at all.
I should add that drag/drop is not activated.
Here is some source code snippet as requested:
class MyWidget : public QWidget
{
void paintEvent( QPaintEvent * /*event*/ );
void resizeEvent( QResizeEvent * event );
void mouseDoubleClickEvent ( QMouseEvent * event );
void mousePressEvent( QMouseEvent* event );
void mouseReleaseEvent( QMouseEvent* event );
void mouseMoveEvent( QMouseEvent* event );
void wheelEvent( QWheelEvent* event );
}
Then I override the following (for the workaround)
void MyWidget::mouseDoubleClickEvent(QMouseEvent * event)
{
// ... do some other stuff ...
// This is a workaround to prevent the cursor from changing
setCursor(cursor());
event->accept();
}
void MyWidget::mousePressEvent(QMouseEvent * event)
{
// ... do some other stuff ...
// This is a workaround to prevent the cursor from changing
setCursor(cursor());
event->accept();
}
To change cursor assuming that mywidget is instantiated with my class, I do this: mywidget->setCursor(Qt::CrossCursor)
Again, it changes the cursor as expected when I hover over my control, but it changes back to the application cursor once I press the mouse button (thus the need for the above workaround)

QApplication.setOverrideCursor(QtGui.QCursor(Qt.CrossCursor))
and when the QWidget closed, set back to the original cursor

Ok I still have not found any answer for this, so here is the workaround:
void MyWidget::mouseDoubleClickEvent(QMouseEvent * event)
{
// ... do some other stuff ...
// This is a workaround to prevent the cursor from changing
setCursor(cursor());
event->accept();
}
void MyWidget::mousePressEvent(QMouseEvent * event)
{
// ... do some other stuff ...
// This is a workaround to prevent the cursor from changing
setCursor(cursor());
event->accept();
}

Related

QRubberBand doesn't work

I am using QRubberBand to draw a selection box on my QWidget that displays a QImage.
I am using the code exactly as it is shown in the documentation, but it doesn't work. I get no errors, but it does act strange. Instead of displaying a selection box when I hold my left button down and drag it across my QWidget which is painted with the QImage it just makes one of my buttons on my interface disappear and reappear based on my left button click. I should also add that the button that disappears isn't apart of the QWidget, or even apart of the parent QObject that creates the QWidget.
void Widget::mousePressEvent(QMouseEvent *event)
{
origin = event->pos();
if (!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
rubberBand->hide();
// determine selection, for example using QRect::intersects()
// and QRect::contains().
}
Any help is appreciated.
Thanks
It is totally my fault. I failed to set my pointer as NULL in my header.
Instead of this :
QRubberBand *rubberBand {rubberBand = NULL};
I just did :
QRubberBand *rubberBand;

how to use mouse move event for QGraphicsScene?

hey want to drag this bezier curve when mouse button is pressed and moved..
I did this:
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
qDebug()<<"in mouse move - outside if";
if((e->buttons() & Qt::RightButton) && isStart && enableDrag)
{
qDebug()<<"mouse dragging happening";
xc2=e->pos().x();
yc2=e->pos().y();
drawDragBezier(xc2,yc2);
}
}
this starts dragging when i press right button and start moving mouse in whole main window..but I want to start dragging only when I press mouse button and move mouse inside the QGraphicsScene.
how to solve this?
EDIT:
void mySubClass1::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
qDebug()<<"in musubclass mouse press event: "<<event->pos().x()<<" "
<<event- >pos().y();
if(shape().contains(event->pos()))
{
currPosX=event->pos().x();
currPosY=event->pos().y();
qDebug()<<"currPosX currPosY: "<<currPosX<<" "<<currPosY;
}
}
}
And the mainwindow class is:
{
myGPath=new mySubClass1();
myScene=new QGraphicsScene;
myScene->addItem(myGPath);
ui->graphicsView->setScene(myScene);
QPointF *startPoint=new QPointF(50,50);
myPaintPath=new QPainterPath(*startPoint);
myPaintPath->quadTo(100,25,200,200);
myGPath->setPath(*myPaintPath);
}
is this the right way?
Personally, to solve this issue I'd take a different approach.
Create a class inherited from QGraphicsItem (or QGraphicsObject if you want signals and slots) to represent the bezier curve. Then implement the mouseMoveEvent of the object in this class.
class MyBezierCurve : public QGraphicsItem
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent*);
void mouseMoveEvent(QGraphicsSceneMouseEvent*);
void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
};
This way, the object can detect in its mousePressEvent when the mouse is directly over one of its control points and update the control points with mouse move events until the release event occurs.
Handling the mouse events in the QGraphicsView will work, but if you introduce more bezier curves, or other objects you'll find you'll need to check which of them you need to be interacting with. Handling it in the object itself will take care of that for you.
You should subclass QGraphicsView and detect mouseMoveEvent over there.
class MyGraphicsView : public QGraphicsView
{
Q_OBJECT
...
protected:
void mouseMoveEvent(QMouseEvent *event);
...
};

KeyPress Event waits for MousePress Event qt

So I'm writing a qtwidget that requires keyboard and mouse input by overriding the respective functions. However the keypress event doesnt happen until a mousepress event occurs. I've played around with the focus a bit and that doesnt seem to help. I feel like I'm missing something regarding how qt processes events. Can any of you fill me in. Thank you
Here are what i think are the relevant bits of code. Let me know if you need more
MeshTest::MeshTest(QWidget *parent) :
QGLWidget(parent)
{
setFocusPolicy(Qt::StrongFocus);
/* Other stuff */
}
void MeshTest::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
/* Some math/ray casting here */
hasHit = MeshTest::findIntersections(x,y,z,dirx,diry,dirz);
}
}
void MeshTest::mouseReleaseEvent(QMouseEvent *event){
if (event->button() == Qt::LeftButton) {
/* Some ray casting stuff here */
}
if(hasHit)
updateGL();
}
void MeshTest::keyPressEvent(QKeyEvent* event){
printf("Key Pressed\n"); //Just for debugging this particular bug
}
Okay so i fixed this a few days ago. The solution is to overide QWidget::event(QEvent *event)
and do all event handling in the function itself

Signal click on QSpinBox Qt

I would like to open a window when I click on a QSpinBox. The problem is that there is no such signal "clicked" for this widget.
Does someone has an idea how to do that?
A QSpinBox is just a QLineEdit with two buttons, input validation and event handling. It doesn't have clicked signal because it's supposed to handle the mouse even itself.
The problem is that even making a custom widget derived from QSpinBox won't be enough since it doesn't receive the mouse events itself, they are handled by its children widgets. You could install an event filter on the QSpinBox children in order to catch the click event, but that's not the neatest way.
If you just want to display a numpad when the user select the box, you can use directly a QLineEdit. You will lose the QSpinBox buttons (but you can add your own ones if you need them) and the validation (but you can add you own using QValidator).
Then you just have to derive it in order to catch the focus event, trigger a custom signal which would show your keyboard :
class MySpinBox: public QLineEdit
{
Q_OBJECT
public:
MySpinBox(QWidget *parent = 0);
~MySpinBox();
signals:
needNumpad(bool hasFocus);
protected:
virtual void focusInEvent(QFocusEvent *e) {
QLineEdit::focusInEvent(e);
emit(needNumpad(true));
}
virtual void focusOutEvent(QFocusEvent *e) {
QLineEdit::focusInEvent(e);
emit(needNumpad(false));
}
}
You can use an event filter and do something like this:
ui->spinBox->installEventFilter(this);
QObjectList o_list = ui->spinBox->children();
for(int i = 0; i < o_list.length(); i++)
{
QLineEdit *cast = qobject_cast<QLineEdit*>(o_list[i]);
if(cast)
cast->installEventFilter(this);
}
And in the event filter you check for a mouse click (in this example its triggered by all mouse buttons, left click, right click, scroll wheel click etc.).
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::MouseButtonPress)
{
showNumpadDialog();
}
return false;
}
You do not need to create your own QSpinBox with QLineEdit and two buttons.
Since QLineEdit is the child of QSpinBox. You can create an event filter for QLineEdit and check whether its parent is a spinbox. Then so, you would get a click event for spin box.
if(event->type() == QEvent::MouseButtonPress && dynamic_cast<QSpinBox *>(dynamic_cast<QLineEdit *>(obj)->parent()) )

Self-expanding Widget, on mouse over

I want to implement a widget (with some edit boxes and sliders) that would open beneath or next to a button ("Opener") when I hover it. The key is it's a temporary widget - as soon as it loses focus, I want it gone. Also, I want it to pop up right next to the Opener, ideally pointing an arrow to Opener.
So, it's basically a tooltip. But it needs to be a widget with buttons and sliders and stuff like that. Is there a clever way to implement it without making a custom widget and writing handlers for all the mouse and focus events and recomputing its ideal position every time I open it or the Opener moves?
class OpenerButton : public QPushButton
{
public:
OpenerButton(QWidget * parent = 0);
protected:
void enterEvent(QEvent *e);
void leaveEvent(QEvent *e);
};
OpenerButton::OpenerButton(QWidget * parent)
: QPushButton(parent)
{
//Do necessary initializations For ex:set a menu for opener button
}
void OpenerButton::leaveEvent(QEvent * e)
{
//hide the popup_menu
}
void OpenerButton::enterEvent(QEvent * e)
{
//Show the menu
//You can use animation for ex:
Popup_menu=new Popup_Dialog(this);//Popup_Dialog is a dialog containing all your widgets
QPropertyAnimation *animation = new QPropertyAnimation(Popup_menu,"geometry");
animation->setDuration(500);
animation->setDirection(QAbstractAnimation::Forward);
QRect startRect(Rect_Relative_to_Opener_Button);
QRect endRect(Shifted_Rect_Relative_to_Opener_Button);
animation->setStartValue(startRect);
animation->setEndValue(endRect);
animation->start();
}
Enterevent is called when the mouse cursor enters the widget. Similarly leaveevent when mouse cursor leaves the widget.

Resources