views:

41

answers:

2

Hi,

I use this code to validate some properties of a set of Qt objects in ChaiScript:

    /// initialize the engine
    boost::shared_ptr<chaiscript::ChaiScript> chai;
    chai.reset(new chaiscript::ChaiScript());
    std::cout << "ChaiScript engine created!!!" << std::endl;


    ///
    /// register elements
    //
    assert(ui->checkBox);
    chai->add(chaiscript::var(ui->checkBox), "checkBox");

    ///
    /// adapt elements

    /// QCheckBox
    chai->add(chaiscript::fun(&QCheckBox::isTristate), "isTristate");
    chai->add(chaiscript::fun(&QCheckBox::isChecked), "isChecked");


    /// validate some properties
    try
    {
        chai->eval("print(\"Starting evaluation...\")");
        int answer_i = 0;
        bool answer_b = false;
        //answer_b = chai->eval<bool>("checkBox.isTristate()");
        answer_b = chai->eval<bool>("checkBox.isChecked()");

        std::cout << "ChaiScript::: " << answer_b << " :: " << answer_i << std::endl;
    }
    catch(std::exception e)
    {
        std::cout << "ChaiScript@Exception::: " << e.what() << std::endl;
    }

The problem is that the "isTristate()" call works, because this is a native method of QCheckBox. On the other hand, the "isChecked()" call fails, i think because this is an inherited method from the QAbstractButton class.

What i am doing wrong?

Thanks!!!

A: 

Hi again,

The problem has been solved in the following way:

/// method binding
chai->add(chaiscript::fun<bool (QCheckBox *)>(&QCheckBox::isChecked), "isChecked");

It is necessary to specify that the bound method belongs to the QCheckBox class, in order not to bind the reference to the parent method.

Cheers!!!

pedromateo
A: 

The latest SVN version of ChaiScript solves this problem by allowing the user to add base class relationships.

chai.add(base_class<Base, Derived>());

After which inherited function calls work as expected, without the need to clarify the base class method pointer type, as your answer shows.

lefticus