Qt 信号与 继承问题

发布于 2024-07-19 02:41:49 字数 1390 浏览 4 评论 0原文

我对 Qt 编程比较陌生,有一个问题。 简短版本:

如何继承超类中定义的信号?

我试图将其他人精心制作的 QTWidgets 子类化以更改一些默认行为:

//Plot3D is a QWidget that defines a signal "rotationChanged"
class matLinePlot : public QObject, public Plot3D {

    Q_OBJECT;
        //etc...
public:
       //etc...

        //Catch Plot3D's signal "rotationChanged" and do some magic with it:
    void initPlot(){
              QObject::connect(this, SIGNAL(rotationChanged( double , double , double )),
            this, SLOT(myRotationChanged(double, double, double)));
    }
};

问题出在 QObject::connect 行中。 我想做的是将rotationChanged SIGNAL(来自qwt3D_plot.h)连接到本地函数/SLOT -“myRotationChanged”。 然而,每当我这样做时,在运行时我都会得到:

Object::connect:没有这样的信号 matLinePlot::rotationChanged(double, double, double)

C:...\matrixVisualization.h 中 没有这样的信号 matLinePlot::rotationChanged(double, double, double)。 当然,我知道 rotationChanged 不在 matrixVisualization.h 中 - 它在 qwt_plot3D.h 中,但我认为因为我继承自Plot3D 一切都应该没问题。 但是,现在我想了一下,由于 SIGNAL 和 SLOT 是宏,我假设 MOC 不知道/关心继承。

这引出了我的问题 - 因为 MOC 和 SIGNALS / SLOTS 似乎不知道继承等:我如何子类化其他地方定义的小部件并访问小部件的信号?

我有很多关于如何使用封装来完成类似事情的示例,但恐怕我不明白如何通过继承来做到这一点。

抱歉,如果这是一个荒谬的问题 - 我觉得我错过了一些明显的东西。

I am relatively new to programming with Qt and had a question. Short version:

How do I inherit signals defined in superclasses?

I am trying to subclass someone else's nicely made QTWidgets to change some of the default behavior:


//Plot3D is a QWidget that defines a signal "rotationChanged"
class matLinePlot : public QObject, public Plot3D {

    Q_OBJECT;
        //etc...
public:
       //etc...

        //Catch Plot3D's signal "rotationChanged" and do some magic with it:
    void initPlot(){
              QObject::connect(this, SIGNAL(rotationChanged( double , double , double )),
            this, SLOT(myRotationChanged(double, double, double)));
    }
};

The problem is in the QObject::connect line. What I would like to do is connect the rotationChanged SIGNAL (from qwt3D_plot.h) to a local function/SLOT - "myRotationChanged". However whenever I do this, at run time I get:

Object::connect: No such signal matLinePlot::rotationChanged(double, double, double)

in C:...\matrixVisualization.h. Of course, I know that rotationChanged isn't in matrixVisualization.h - it's in qwt_plot3D.h, but I thought that since I inherit from Plot3D everything should be fine. But, now that I think about it, since SIGNAL and SLOT are macros, I assume MOC doesn't know/care about inheritance.

Which leads me to my question - since MOC and SIGNALS / SLOTS don't seem to know about inheritance etc: how can I subclass a widget defined somewhere else and get access to the widget's signals?

I have a lot of examples of how to use encapsulation to accomplish something like this, but I'm afraid I don't understand how to do this with inheritance.

Sorry if this is a ridiculous question - I feel like I'm missing something obvious.

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

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

发布评论

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

评论(4

兲鉂ぱ嘚淚 2024-07-26 02:41:49

我猜问题是多重继承:

class matLinePlot : public QObject, public Plot3D
...

我假设 Plot3DQObject 的子类? 在这种情况下,你应该

class matLinePlot : public Plot3D
...

这样做。

I guess the problem is the multiple inheritance:

class matLinePlot : public QObject, public Plot3D
...

I assume that Plot3D is a subclass of QObject? In this case, you should do

class matLinePlot : public Plot3D
...

instead.

酒与心事 2024-07-26 02:41:49

SIGNAL(x) 和 SLOT(x) 是生成字符串文字的宏。 在运行时,使用这些生成的文字的字符串比较来匹配槽和信号。

(我会在 mdec 的评论中添加评论,但我没有足够的代表)

SIGNAL(x) and SLOT(x) are macros that generate string literals. At runtime, slots and signals are matched up using string compares of those generated literals.

(I would have added a comment to mdec's comment, but I don't have enough rep)

远山浅 2024-07-26 02:41:49

我相信如果 Plot3D::rotationChanged 信号是公共的或受保护的,这将起作用。 您确定该信号不是私有的吗?

编辑:

虽然我找不到具体的参考资料,但我不得不得出结论,信号始终是公开的。 至少我在这里所做的测试似乎表明我可以连接到一个信号,即使它是在类的私有部分中声明的。

我还验证了 QObject 中声明的信号可以使用 connect 语句中的 QObject 子类进行连接,因此信号绝对是可继承的。 正如我在其他答案和评论中看到的那样,问题一定在其他地方。

I believe that will work if the Plot3D::rotationChanged signal is public or protected. Are you sure the signal is not private?

Edit:

Although I could not find a specific reference, I'll have to conclude that signals are always public. At least a test I did here seemed to indicate that I could connect to a signal even if it was declared in the private section of a class.

I also verified that a signal declared in QObject could be connected using a subclass of QObject in the connect statement so signals are definitely inheritable. As I see in other answers and comments here, the issue must be elsewhere.

囍笑 2024-07-26 02:41:49

不正确-> 看评论。

我在 Uni 使用 Qtopia,我相信我记得有人说过关于连接的 SIGNAL 和 SLOT 参数中的间距。

尝试使用

QObject::connect(this, SIGNAL(rotationChanged(double,double,double)),
            this, SLOT(myRotationChanged(double,double,double)));

我知道它看起来并不直观,因为 C++ 对空格不敏感,但我相信它与 Qtopia/QT 在连接信号和插槽时使用的一些魔法有关。 这可能只适用于Qtopia,或者我可能听错了,但是尝试一下。 此外,信号是公开的还是受保护的?您是否包含了适当的头文件?

Incorrect -> see comments.

I'm using Qtopia at Uni and I believe I recall someone saying something about spacing in the SIGNAL and SLOT parameters for connect.

Try using

QObject::connect(this, SIGNAL(rotationChanged(double,double,double)),
            this, SLOT(myRotationChanged(double,double,double)));

I know it doesn't seem intuitive, as C++ isn't sensitive to whitespace, however I believe it has something to do with some of the magic that Qtopia/QT uses when connecting signals and slots. This may only apply to Qtopia, or I may have heard wrong, but give it a try. Additionally are the signals public or protected and have you included the appropriate header files?

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