How to compare Qt objects?

24 views
Skip to first unread message

Polos Ruetz

unread,
Mar 18, 2014, 6:06:38 AM3/18/14
to eql-user
(This is a forwarded question from "Orm")

Hi,

I'm trying to take some action on mouse over a label. The following
code installs an event handler for entering and leaving. Unfortunately
the object submitted to the event handler functions can't be
determined by comparing it to the lisp representation of the created
label widget (see the format command in the handler functions). I
guess I'm doing something wrong. Can somebody give me a hint on the
canonical way to accomplish this task (and apologies, in case that
shouldn't be a strictly eql-related question)?

I'm aware that in principle I could subclass QLabel and add a slot
with id information to query in the event handler, but I'm at loss how
to subclass qt classes in the framework of eql.

Yours,
Orm

;;; ---------- start code example -------------

(defparameter *labels*
(loop for count from 1 to 4
collect (qnew "QLabel"
"text" (format nil "Label ~d" count))))

(defun enter-event (obj event)
(format t "enter-event: ~a, ~a~%" obj (find obj *labels*))
t)

(defun leave-event (obj event)
(format t "leave-event: ~a, ~a~%" obj (find obj *labels*))
t)

(let ((main (qnew "QWidget"
"windowTitle" "hover-test"
"size" '(200 200)
"pos" '(50 50)))
(layout (qnew "QVBoxLayout")))
(dolist (w *labels*)
(format t "~a~%" w) ; print internal object ref to stdout
(! "addWidget" layout w))
(! "setLayout" main layout)
(qadd-event-filter nil |QEvent.Enter| 'enter-event)
(qadd-event-filter nil |QEvent.Leave| 'leave-event)
(x:do-with main "show" "raise"))

;;; ---------- end code example -------------

polos

unread,
Mar 18, 2014, 6:09:13 AM3/18/14
to eql-...@googlegroups.com
Your functions only need a small modification. The internally used
QT-OBJECTs can be compared with QEQL, so this will work:

(defun enter-event (obj event)
  (format t "enter-event: ~a, ~a~%" obj (find obj *labels* :test 'qeql))

  t)

(defun leave-event (obj event)
  (format t "leave-event: ~a, ~a~%" obj (find obj *labels* :test 'qeql))
  t)

Subclassing is not directly supported, but QOVERRIDE and encapsulation
(see example "X-extras/CLOS-encapsulation") should be sufficient anyway.

Paul
Reply all
Reply to author
Forward
0 new messages