2.3. Handlers

A simple example using Handlers is as follows:

module Main where

import Qtc.Classes.Qccs
import Qtc.Classes.Qccs_h
import Qtc.Classes.Gui
import Qtc.ClassTypes.Gui
import Qtc.Gui.Base
import Qtc.Enums.Base
import Qtc.Enums.Classes.Core
import Qtc.Enums.Core.Qt
import Qtc.Gui.QApplication
import Qtc.Gui.QMessageBox
import Qtc.Gui.QLabel
import Qtc.Gui.QLabel_h
import Qtc.Gui.QKeyEvent

main :: IO Int
main = do
  qApplication ()
  tl <- qLabel "press key 'A'"
  setAlignment tl (fAlignCenter::Alignment)
  resize tl (200::Int, 60::Int)
  mb <- qMessageBox tl
  setHandler tl "keyPressEvent(QKeyEvent*)" $ tlkp mb
  qshow tl ()
  qApplicationExec ()

tlkp :: QMessageBox () -> QLabel () -> QKeyEvent () -> IO ()
tlkp mb this ke
  = do
    k <- key ke ()
    if (k == qEnum_toInt eKey_A)
     then
      do
      setText mb $ "You have pressed the 'A' key!"
      qshow mb ()
     else
      return ()
    keyPressEvent_h this ke

The setHandler function has the general format:

setHandler handler_object "handler_signature" handler_function

The handler_object must be a previously declared Qt object.

The "handler_signature" string describes the name and parameter types handler to be set.

The handler_function is a partial application of a Haskell function of type:

global_Haskell_parameters -> handler_object_type -> handler_parameters

Note that the handler function in the above examples terminates with the function call keyPressEvent_h this ke. This is a call to the base class handler function which is often required in event handling, to "pass on" the event to lower levels for normal processing. This is available for all qtHaskell handler functions. The name of the function to call is the same as in the handler_string followed by _h. The required parameters are handler_object followed by handler_parameters.

There is no need to subclass an object in order to set a handler on it. Subclassing is only required for objects that have custom signals or slots.