• 12214阅读
  • 6回复

[提问]QtScript [复制链接]

上一主题 下一主题
离线kym2005
 
只看楼主 倒序阅读 楼主  发表于: 2010-04-09
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
大家帮我看看,为什么我的脚本不能执行
#include <QtGui>
#include <QtUiTools>
#include <QtScript>
#include <QMessageBox>
class Testss {
public:    
    Testss(){};
    ~Testss(){};
    void start()
        {
            Q_INIT_RESOURCE(ddd);
            QScriptEngine engine;

            QFile scriptFile(":/teste.js");
            if(!scriptFile.open(QIODevice::ReadOnly)){
                 QMessageBox::warning(NULL,QObject::tr("open"),QObject::tr("open failed"));
            }
            engine.evaluate(scriptFile.readAll());
            scriptFile.close();

            QUiLoader loader;
            QFile uiFile(":/teste.ui");
            if(!uiFile.open(QIODevice::ReadOnly)){
                QMessageBox::warning(0,QObject::tr("open"),QObject::tr("open failed"));
            }
            QWidget *ui = loader.load(&uiFile);
            uiFile.close();

            QScriptValue func = engine.evaluate("nextClass");
            QScriptValue scriptUi = engine.newQObject(ui);
            QScriptValue table = func.construct(QScriptValueList() << scriptUi);
            if(engine.hasUncaughtException()) {
                QScriptValue value = engine.uncaughtException();
                QString lineNumber = QString("\nLine Number:%1\n").arg(engine.uncaughtExceptionLineNumber());
                QStringList btList = engine.uncaughtExceptionBacktrace();
                QString trace;
                for(short i=0; i<btList.size(); ++i)
                    trace += btList.at(i);
                QMessageBox::information(NULL, QObject::tr("Exception"), value.toString() + lineNumber + trace );
            }
            
            ui->show();
    }
    
};
int main(int argc,char **argv)
{    
    
    QApplication app(argc, argv);
    Testss w;
    w.start();
    return app.exec();
}


下面是js

function nextClass(ui)
{
    this.ui = ui;
    ui.pushButton.clicked.connect(this,this.showtext);
    ui.pushButton1.clicked.connect(this,ui.close);    
    
}
nextClass.prototype.showtext = function()
{        
    this.ui.textEdit.setText(this.ui.lineEdit.text);
}

下面是.ui

<ui version="4.0" >
<class>nextClass</class>
<widget class="QWidget" name="nextClass" >
  <property name="geometry" >
   <rect>
    <x>0</x>
    <y>0</y>
    <width>502</width>
    <height>288</height>
   </rect>
  </property>
  <property name="windowTitle" >
   <string>twoform</string>
  </property>
  <widget class="QPushButton" name="pushButton" >
   <property name="geometry" >
    <rect>
     <x>110</x>
     <y>200</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text" >
    <string>PushButton</string>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton1" >
   <property name="geometry" >
    <rect>
     <x>250</x>
     <y>200</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text" >
    <string>PushButton</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit" >
   <property name="geometry" >
    <rect>
     <x>140</x>
     <y>40</y>
     <width>113</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QTextEdit" name="textEdit" >
   <property name="geometry" >
    <rect>
     <x>140</x>
     <y>90</y>
     <width>191</width>
     <height>64</height>
    </rect>
   </property>
  </widget>
  <action name="actionOpen" >
   <property name="text" >
    <string>open</string>
   </property>
  </action>
  <action name="actionSend" >
   <property name="text" >
    <string>send</string>
   </property>
  </action>
  <action name="actionQuit" >
   <property name="text" >
    <string>quit</string>
   </property>
  </action>
</widget>
<resources/>
<connections/>
<slots>
  <slot>readyreader()</slot>
</slots>
</ui>


为什么我把类里Start函数里的代码放到main函数中脚本就能执行,而在放到函数中就不能呢


离线kym2005
只看该作者 1楼 发表于: 2010-04-09
希望大家能够帮助呀,很着急,先谢谢了
离线浪漫天使
只看该作者 2楼 发表于: 2010-04-09

Q_INIT_RESOURCE(ddd);
移到
QApplcation
那行后面看看
离线kym2005
只看该作者 3楼 发表于: 2010-04-09
使过了,还是不行呀
离线kym2005
只看该作者 4楼 发表于: 2010-04-09
那个问题解决了,只要把QScriptEngine engine;放到主函数中就行了。但是现在还有个问题,就是假设我有这几个文件,
main.cpp,test.cpp,test.h和一个.ui 文件,如果我在test.cpp 的构造函数中使用了setupUi(this),然后在动态加载时js和ui,操作控件只能一个能用。
离线kym2005
只看该作者 5楼 发表于: 2010-04-12
问题解决了
离线huangweisxl

只看该作者 6楼 发表于: 2011-04-22
我也遇到这个问题,请问怎么解决的?麻烦指点一二
快速回复
限100 字节
 
上一个 下一个