继承QWidget的Javascript对象

发布于 2024-10-16 03:49:50 字数 997 浏览 1 评论 0 原文

我正在 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();

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

错误:运行/评估中:类型错误: Function.prototype.connect:目标是 不是一个函数

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

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

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

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

I am doing something in Javascript and Qt, and for my purpose, I need a javascript object that inherits QWidget. So far, I have tried the following:

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();

I instantiate object from the class "Test" and with the call of the show() method, I get the widget:

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

However, I get the following interpreter error:

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

If I change the line this.button.clicked.connect(this.slot); to call a static method defined like this:

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

the program runs fine, but static method is a no-no. I don't want anyone to call slot() except the instantiated object.

What is wrong with this picture? Has anyone had experience with Javascript objects inheriting Qt objects?
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2024-10-23 03:49:50

好吧,我想我可能会明白这一点。所以这里的神奇之处在于:

Test.prototype = new QWidget();

需要在构造函数之前,构造函数也必须采用 “parent”参数也是如此。最后但并非最不重要的一点是,在 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");
}

这是它可能关注的人。

Ok I think I might figured this out. So the magic here is:

Test.prototype = new QWidget();

needs to be before the constructor, also the constructor has to take "parent" argument too. And last but not least, in connect() there are two arguments: first is which class contains the slot (in my case it is this) and the name of the slot with this pointer.

So, having this in mind, the above code will look like 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");
}

This is to whom it may concern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文