Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, May 2008
How to scroll with arrow keys in a graphicsview


Message 1 in thread

Hello,

I have a graphicsview  V  and install an eventfilter on it.
In the event filter I  check for key events and the event filter
has a pointer to V and now calls Vptr->translate(10,0)
for translating to the right, etc.

With print statements I see that I get at the function call
when I hit the arrow key, but visually nothing happens,
that is the displayed graphicsitems just stay where they are.

I must misunderstand something very basic.

Does anybody have a minimum example that works?

best regards,

andreas

--
 [ signature omitted ] 

Message 2 in thread

Hi,

Andreas Fabri wrote:
> 
> Hello,
> 
> I have a graphicsview  V  and install an eventfilter on it.
> In the event filter I  check for key events and the event filter
> has a pointer to V and now calls Vptr->translate(10,0)
> for translating to the right, etc.
> 
> With print statements I see that I get at the function call
> when I hit the arrow key, but visually nothing happens,
> that is the displayed graphicsitems just stay where they are.
> 
> I must misunderstand something very basic.
> 
> Does anybody have a minimum example that works?
> 
> best regards,
> 
> andreas
> 
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 

Check out the property of the view called 'transformationAnchor'. The 
default setting makes translations do nothing.

Cheers,
Ian.

This email and any files transmitted with it are confidential and are intended solely for the use of the individual or entity to whom they are addressed.  If you are not the original recipient or the person responsible for delivering the email to the intended recipient, be advised that you have received this email in error, and that any use, dissemination, forwarding, printing, or copying of this email is strictly prohibited. If you received this email in error, please immediately notify the sender and delete the original.


--
 [ signature omitted ] 

Message 3 in thread

Ian Thomson wrote:
> Hi,
> 
> Please respond to the list.

Sorry, but I am used to lists where the reply-to is the list and not the sender.

> 
> The solution is to turn off the anchor (QGraphicsView::NoAnchor) before 
> you use translate(x, y), and then to turn the anchor back to 
> QGraphicsView::AnchorUnderMouse afterwards.

Didn't help. I fleshed out a minimal example which doesn't do what I expect,
namely to display the graphics item slighly moved.

andreas

#include <QApplication>
#include <QtGui/QGraphicsView>
#include <QtGui/QGraphicsScene>
#include <QKeyEvent>
#include <iostream>

struct  Up : public QObject {

   Q_OBJECT

public:
   Up(QGraphicsView* v_)
     : v(v_)
   {}

   bool eventFilter(QObject *obj, QEvent *event)
   {
     if (event->type() == QEvent::KeyPress) {
       QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
       if (keyEvent->key() == Qt::Key_Up){
	std::cout << "up" << std::endl;
	v->translate(0, -10);
       }
     }
     return true;
   }

   QGraphicsView* v;

};

int main(int argc, char **argv)
{
   QApplication app(argc, argv);


   QGraphicsScene scene;
   scene.setSceneRect(0,0, 100, 100);
   scene.addRect(20, 20, 30, 5);

   QGraphicsView* view = new QGraphicsView(&scene);
   view->installEventFilter(new Up(view));
   view->setTransformationAnchor(QGraphicsView::NoAnchor);

   view->show();
   return app.exec();
}

#include "min.moc"


> 
> Cheers,
> Ian.
> 
> Andreas Fabri wrote:
>> Ian Thomson wrote:
>>> Hi,
>>>
>>> Andreas Fabri wrote:
>>>>
>>>> Hello,
>>>>
>>>> I have a graphicsview  V  and install an eventfilter on it.
>>>> In the event filter I  check for key events and the event filter
>>>> has a pointer to V and now calls Vptr->translate(10,0)
>>>> for translating to the right, etc.
>>>>
>>>> With print statements I see that I get at the function call
>>>> when I hit the arrow key, but visually nothing happens,
>>>> that is the displayed graphicsitems just stay where they are.
>>>>
>>>> I must misunderstand something very basic.
>>>>
>>>> Does anybody have a minimum example that works?
>>>>
>>>> best regards,
>>>>
>>>> andreas
>>>>
>>>> -- 
>>>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx 
>>>> with "unsubscribe" in the subject or the body.
>>>> List archive and information: http://lists.trolltech.com/qt-interest/
>>>>
>>>
>>> Check out the property of the view called 'transformationAnchor'. The 
>>> default setting makes translations do nothing.
>>
>> It's
>>
>> graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
>>
>> andreas
>>
>>>
>>> Cheers,
>>> Ian.
>>>
>>> This email and any files transmitted with it are confidential and are 
>>> intended solely for the use of the individual or entity to whom they 
>>> are addressed.  If you are not the original recipient or the person 
>>> responsible for delivering the email to the intended recipient, be 
>>> advised that you have received this email in error, and that any use, 
>>> dissemination, forwarding, printing, or copying of this email is 
>>> strictly prohibited. If you received this email in error, please 
>>> immediately notify the sender and delete the original.
>>>
>>>
>>> -- 
>>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx 
>>> with "unsubscribe" in the subject or the body.
>>> List archive and information: http://lists.trolltech.com/qt-interest/
>>>
>>>
>>>
>>
> 
> 
> 

--
 [ signature omitted ]