PDA

View Full Version : Changing cursor shape



Odyseusz
17th November 2011, 20:51
I have a problem with cursor (Windows XP SP3, Qt 4.7.0 Creator, both debug and release variants):

I don't understand behavior of cursor in this program! When i'm clicking on window (not QLabel area) cursor changes its shape as I expected. When I press mouse button cursor changes to "ClosedHandCursor" and when I released one cursor changes to "OpenHandCursor". But if I moved cursor over QLabel area cursor changes its shape ONLY on QLabel area despite the fact that events "mousePressEvent" and "mouseReleaseEvent" are served - see window title.

Do somebody knows how solve this problem? Thank you.

----------------------------------------------------------
main.cpp
----------------------------------------------------------

#include <QApplication>
#include "Problem.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Problem win;
win.show();
return app.exec();
}

----------------------------------------------------------
problem.h
----------------------------------------------------------

#include <QWidget>

class Problem : public QWidget
{
public:
Problem();
protected:
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
};

----------------------------------------------------------
problem.cpp
----------------------------------------------------------

#include <QtGui>
#include "Problem.h"

Problem::Problem()
{
setWindowTitle(" ? ");
setGeometry(50,50,500,300);
setCursor(Qt::OpenHandCursor);

QLabel *Label = new QLabel("Label area", this);
Label->setFrameStyle(1);
Label->setGeometry(100,100,200,20);
}
void Problem::mousePressEvent(QMouseEvent *event)
{
setCursor(Qt::ClosedHandCursor);
setWindowTitle(" ClosedHandCursor "));
}
void Problem::mouseReleaseEvent(QMouseEvent *event)
{
setCursor(Qt::OpenHandCursor);
setWindowTitle(" OpenHandCursor "));
}

MarekR22
17th November 2011, 21:36
try unset cursor for this label.

Odyseusz
18th November 2011, 09:03
I'm sorry, but it not makes any changes.
And even, then what to do if I would like another cursor for window
and another cursor for label (see below)?
Thank you for your help!


Problem::Problem()
{
setWindowTitle(" ? ");
setGeometry(50,50,500,300);
setCursor(Qt::OpenHandCursor); // <--- cursor for window

QLabel *Label = new QLabel("Label area", this);
Label->setFrameStyle(1);
Label->setGeometry(100,100,200,20);
Label->setCursor(Qt::IBeamCursor); // <---- cursor for label
}

void Problem::mousePressEvent(QMouseEvent *event)
{
setCursor(Qt::ClosedHandCursor);
setWindowTitle(" ClosedHandCursor "));
}

void Problem::mouseReleaseEvent(QMouseEvent *event)
{
setCursor(Qt::OpenHandCursor);
setWindowTitle(" OpenHandCursor "));
}

Lykurg
18th November 2011, 10:18
Hi Odyseusz,

I don't get your problem to be honest. The first program works as expected: Whether you click on the window or on the label, the shape changes (because the label adopt the behavior of its parent (the window)). On the second, over the label it changes to IBeamCursor and even if you press it still have that shape, because you change the cursor on the window not on the label. And since the press event was on the label, it stays "active" even if you move the cursor over the window.

If that does not answer your question please elaborate what exactly you expect (based on which actions).

Best

MarekR22
18th November 2011, 13:01
I meant: Lable->unsetCursor();

Odyseusz
18th November 2011, 17:59
------------------------------------------------------------
To MarekR22
------------------------------------------------------------
As I wrote in post #3 this instruction hasn't any effect!

Thank you for your help!

---------------------------------------------------------------------------
To Lykurg
---------------------------------------------------------------------------
First - I'm clicking on the window and the cursor changes as expected
(open hand - closed hand);
Next - I'm moving the cursor to the label and I'm clicking on this label:
in first example cursor changes as expected (open hand - closed hand),
in second example cursor changes to IBeam and of course not changes;
Then - I'm moving the cursor back to the window - in both cases the cursor
changes to open hand (as expected) - BUT, if now I'm clicking
on the window the cursor stays still open hand (no regard,
if I clicked on the label in step Next or only moved the cursor
over the label) - THIS IS MY PROBLEM!

Thank you for your help!

Sorry for my english!

Spitfire
22nd November 2011, 11:08
Yep, it's happening and it's odd.
QCursor::shape() returns correct value in mousePress/Release but the visible shape of the cursor doesn't change.

No idea why it's happening.

Odyseusz
22nd November 2011, 17:39
Spitfire, thank you for your attempt!