qtbase (version 1.1.0)

qsetClass: Define a Qt/C++ class in R

Description

C++ libraries like Qt often expect/require clients to extend classes in the API. qsetClass will define a class in terms of its name, parent (single inheritance) and constructor. Methods are added with with qsetMethod, qsetSlot, and qsetSignal. The qsetProperty function defines new properties. Calling qsetRefClass on an RQtClass creates a corresponding R5 reference class, thus unifying R/Qt classes with the methods package.

Usage

qsetClass(name, parent, constructor = function(...) parent(...),
          where = topenv(parent.frame()))
qsetMethod(name, class, FUN,
           access = c("public", "protected", "private"))
qsetSlot(signature, class, FUN,
         access = c("public", "protected", "private"))
qsetSignal(signature, class,
           access = c("public", "protected", "private"))
qsetProperty(name, class, type = NULL, read = function() this[[.name]], 
    write = function(val) this[[.name]] <- val, notify = NULL, 
    constant = FALSE, final = FALSE, stored = TRUE, user = FALSE)
qsetRefClass(Class, where = topenv(parent.frame()), ...)

Arguments

name
The name of the class or method.
parent
Object of RQtClass representing the parent. Only single inheritance is supported.
constructor
A function for constructing an instance. By default, arguments are passed to parent constructor. See details.
where
The environment in which to define the class. Behaves like setClass. Usually not specified.
class, Class
Object of RQtClass on which to define the method.
FUN
The function that implements the method.
access
The access modifier; same meaning as in C++. public methods may be invoked from any context, protected only by methods of this class or a subclass, and private only by methods of this class.
signature
The name and types of the arguments. If there are no arguments, and the no return value, this is just the name. Otherwise, provide the signature in C++ syntax, as in: int myMethod(int, const char*) for a method named myMethod that accepts two parameters, one an integer and one a string, and then returns an integer. We are essentially using C++ as a DSL for specifying signatures; the types must be C++ types, because this method is made available to external systems (like DBus and Javascript via QtWebKit).
type
The type name for the property
read
The function for reading (getting) the value of this property. By default, this assumes the property is stored as a field named of the form .name.
write
The function for writing (setting) the value of this property. By default, this assumes the property is stored as a field named of the form .name.
notify
The name of a previously defined signal that is emitted when the property is modified, or NULL for none.
constant
A hint to Qt that the property does not change; not actually enforced and rarely used.
final
A hint to Qt that the property is not to be overridden; not actually enforced and rarely used.
stored
A hint to Qt that the property is stored in the object, i.e., not dynamically computed. Could be helpful when serializing objects.
user
Whether the property is the primary user-facing property for this class. Like the current value for a scrollbar. Used when autowiring widgets to data models, as in QDataWidgetMapper.

Value

  • For qsetClass, the RQtClass object (supports chaining with qsetMethod).

    For qsetMethod, qsetSlot, qsetSignal, and qsetProperty, the name of the method/property (supports chaining).

    For qsetRefClass, the reference class generator object corresponding to the R/C++ class.

Details

The side-effect of qsetClass is that a RQtClass object for the new R class is assigned into the where argument. Within the scope of a method or constructor, the symbols are first resolved against the members of the class (including inherited members). The search then procedes to the enclosure of the R function, and on up the conventional search path.

For chaining up, there is a special function named super that is defined differently for methods and constructors. Within a constructor, super will invoke the constructor of the super class, as in Java. For a method, the first argument passed to super should be the name of the method in the parent class to invoke (also similar to Java).

Examples

Run this code
e <- Qt$QLineEdit()

qsetClass("positiveValidator", Qt$QValidator)

qsetMethod("validate", positiveValidator, function(input, pos) {
  val <- suppressWarnings(as.integer(input))
  if (!is.na(val)) {
    if (val > 0)
      Qt$QValidator$Acceptable
    else Qt$QValidator$Invalid
  } else {
    if (input == "")
      Qt$QValidator$Acceptable
    else Qt$QValidator$Invalid
  }
})

v <- positiveValidator(e)
e$setValidator(v)
e$show()

Run the code above in your browser using DataLab