March 1, 2011

bundickt bundickt
Lab Rat
53 posts

QEvent in qml

 

Is there a way to pass a QEvent, QKeyEvent or QMouseEvent from c++ to qml without writing my own wrapper classes?

6 replies

March 1, 2011

mbrasser mbrasser
Ant Farmer
452 posts

You will need to wrap them in some fashion (you can see the wrappers used by the built-in elements at src/declarative/graphicsitems/qdeclarativeevents_p.h).

Regards,
Michael

March 2, 2011

bundickt bundickt
Lab Rat
53 posts

Thanks Michael

I implemented wrappers similar to the ones in qdeclarativeevents_p.h but when I try to use them in my qml I get this error:

QMetaProperty::read: Unable to handle unregistered datatype ‘QMLKeyEvent*’ for property ‘QDeclarativeBoundSignalParameters::event’

I register the QMLKeyEvent class with this call:

  1. qmlRegisterType<QMLKeyEvent>();

And at the end of my header file that contains the QMLKeyEvent class I put:

  1. QML_DECLARE_TYPE(QMLKeyEvent)

Is there something else I need to do to register this type?

March 2, 2011

mbrasser mbrasser
Ant Farmer
452 posts

I can’t think of anything else you’d need to do.

What version of Qt are you running (there have been a lot of bug fixes since 4.7.0)? Can you share what your QML looks like?

Regards,
Michael

March 2, 2011

bundickt bundickt
Lab Rat
53 posts

I believe I’m using Qt 4.7.1.

My QML code looks like this:

  1. UserEventFilter {
  2.   id: userEventFilter;
  3.  
  4.   onKeyPressed: {
  5.    console.log(event.text);
  6.   }
  7. }

UserEventFilter code looks like this:

  1. bool UserEventFilter::processKeyPress(QEvent *event) {
  2.  QMLKeyEvent ke(*static_cast<QKeyEvent *>(event));
  3.  ke.setAccepted(false);
  4.  
  5.  emit keyPressed(&ke);
  6.  emit userEventOccurred();
  7.  
  8.  return ke.isAccepted();
  9. }

I’m going to upgrade to Qt 4.7.2 and see what happens.

March 3, 2011

bundickt bundickt
Lab Rat
53 posts

Are there any examples out there that show how to emit a signal with an argument that is not a basic type?
Also, are there examples of emitting a signal with an argument that is not read only?

March 3, 2011

bundickt bundickt
Lab Rat
53 posts

It’s working… I think I had something wrong with my build.

 
  ‹‹ Qt programming language for andriod      chapter6-plugins - how are QML imports resolved? ››

You must log in to post a reply. Not a member yet? Register here!