gpt4 book ai didi

继承QWidget的Javascript对象

转载 作者:行者123 更新时间:2023-12-02 20:25:16 24 4
gpt4 key购买 nike

我正在 Javascript 和 Qt 中做一些事情,出于我的目的,我需要一个继承 QWidget 的 javascript 对象。到目前为止,我已经尝试过以下方法:

function Test()
{
QWidget.call(this);
this.button = new QPushButton("test");
this.layout().insertWidget(1,this.button);
this.button.clicked.connect(this.slot);
}

Test.prototype.slot = function()
{
print("Test button clicked");
}

Test.prototype = new QWidget();

我从“Test”类实例化对象,并通过调用 show() 方法,获得小部件:

var testVariable = new Test();
testVariable.show();

但是,我收到以下解释器错误:

Error: In run/evaluate: TypeError: Function.prototype.connect: target is not a function

如果我更改行 this.button.clicked.connect(this.slot); 来调用如下定义的静态方法:

    this.button.clicked.connect(Test.slot);
...
...
Test.slot = function () { /* code */ }

程序运行良好,但静态方法是禁忌。除了实例化对象之外,我不希望任何人调用 slot()

这张图有什么问题吗?有人有过 Javascript 对象继承 Qt 对象的经验吗?提前致谢

最佳答案

好吧,我想我可能明白了。所以这里的魔力是:

Test.prototype = new QWidget();

需要在构造函数之前,构造函数也必须采用 "parent" argument也。最后但并非最不重要的一点是,在 connect() 中,有两个参数:第一个是哪个类包含插槽(在我的例子中是 this)和插槽的名称与 this 指针。

因此,考虑到这一点,上面的代码将如下所示:

Test.prototype = new QWidget();
function Test(parent)
{
QWidget.call(this, parent);
this.button = new QPushButton("test");
this.layout().insertWidget(1, this.button);
this.button.clicked.connect(this, this.slot);
}

Test.prototype.slot = function()
{
print("Test button clicked");
}

这是可能涉及到的人。

关于继承QWidget的Javascript对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4932004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com