示例#1
0
static int
qtuiloader_load(lua_State *L)
{
  // this
  QUiLoader *loader = luaQ_checkqobject<QUiLoader>(L, 1);
  // file
  QFile afile;
  QIODevice *file = qobject_cast<QIODevice*>(luaQ_toqobject(L, 2));
  if (!file && lua_isstring(L, 2))
    {
      file = &afile;
      const char *fn = lua_tostring(L, 2);
      afile.setFileName(QFile::decodeName(fn));
      if (! afile.open(QIODevice::ReadOnly))
        luaL_error(L,"cannot open file '%s' for reading (%s)", 
                   fn, afile.errorString().toLocal8Bit().constData() );
    }
  else if (!file)
    {
      file = &afile;
      void *udata = luaL_checkudata(L, 2, LUA_FILEHANDLE);
      if (! afile.open(*(FILE**)udata, QIODevice::ReadOnly))
        luaL_error(L,"cannot use stream for reading (%s)", 
                   afile.errorString().toLocal8Bit().constData() );
    }
  // parent
  QWidget *parent = luaQ_optqobject<QWidget>(L, 3);
  // load
  QWidget *w = loader->load(file, parent);
  luaQ_pushqt(L, w, !parent);
  return 1;
}
示例#2
0
文件: qtluautils.cpp 项目: elq/torch5
int 
luaQ_pcall(lua_State *L, int na, int nr, int eh, int oh)
{
  QtLuaEngine *engine = luaQ_engine(L);
  QObject *obj = engine;
  if (oh)
    obj = luaQ_toqobject(L, oh);
  if (! obj)
    luaL_error(L, "invalid qobject");
  return luaQ_pcall(L, na, nr, eh, obj);
}
示例#3
0
static int
qt_connect(lua_State *L)
{
  // LUA: "qt.connect(object signal closure)" 
  // Connects signal to closure.
  
  // LUA: "qt.connect(object signal object signal_or_slot)"
  // Connects signal to signal or slot.
  
  QObject *obj = luaQ_checkqobject<QObject>(L, 1);
  const char *sig = luaL_checkstring(L, 2);
  QObject *robj = luaQ_toqobject(L, 3);
  if (robj)
    {
      // search signal or slot
      QByteArray rsig = luaL_checkstring(L, 4);
      const QMetaObject *mo = robj->metaObject();
      int idx = mo->indexOfMethod(rsig.constData());
      if (idx < 0)
        {
          rsig = QMetaObject::normalizedSignature(rsig.constData());
          idx = mo->indexOfMethod(rsig.constData());
          if (idx < 0)
            luaL_error(L, "cannot find target slot or signal %s", 
                       rsig.constData());
        }
      // prepend signal or slot indicator
      QMetaMethod method = mo->method(idx);
      if (method.methodType() == QMetaMethod::Signal)
        rsig.prepend('0' + QSIGNAL_CODE);
      else if (method.methodType() == QMetaMethod::Slot)
        rsig.prepend('0' + QSLOT_CODE);
      else
        luaL_error(L, "target %s is not a slot or a signal",
                   rsig.constData());
      // connect
      QByteArray ssig = sig;
      ssig.prepend('0' + QSIGNAL_CODE);
      if (! QObject::connect(obj, ssig.constData(), robj, rsig.constData()))
        luaL_error(L, "cannot find source signal %s", sig);
    }
  else
    {
      luaL_checktype(L, 3, LUA_TFUNCTION);
      bool direct = lua_toboolean(L, 4);
      if (direct)
        luaL_checktype(L, 4, LUA_TBOOLEAN);
      if (! luaQ_connect(L, obj, sig, 3, direct))
        luaL_error(L, "cannot find source signal %s", sig);
    }
  return 0;
}