Qt4父指针用法

发布于 2024-08-22 11:11:41 字数 726 浏览 2 评论 0原文

我在理解 QT4 中 parent 指针的用法时遇到一些问题。

class firstClass : public QWidget
{
    Q_OBJECT

public:
     firstClass(QWidget *parent = 0);
    ~firstClass();

    void doSomething();

private:
    secondClass * myClass;
};

class secondClass : public QWidget
{
    Q_OBJECT

public:
    secondClass(QWidget *parent = 0);
    void doSomethingElse();
};

我想在运行 doSomethingElse() 时调用 doSomething() 方法。有没有办法使用 parent 指针来做到这一点?

我尝试了 parent->doSomething() 但它不起作用。看起来 Qt Creator 只建议在 parent-> 之后使用 QObject 类的方法。

另一方面,我不能像 secondClass(firstClass *parent = 0); 这样写 - 编译器返回错误:

感谢您的任何建议。

I'm having some problem with understanding usage of parent pointer in QT4.

class firstClass : public QWidget
{
    Q_OBJECT

public:
     firstClass(QWidget *parent = 0);
    ~firstClass();

    void doSomething();

private:
    secondClass * myClass;
};

class secondClass : public QWidget
{
    Q_OBJECT

public:
    secondClass(QWidget *parent = 0);
    void doSomethingElse();
};

I want to call doSomething() method while running doSomethingElse(). Is there any way to do it using parent pointer?

I tried parent->doSomething() but it doesn't work. It seems that Qt Creator is suggesting only methods from QObject class after parent->.

On the other hand I can't write it like secondClass(firstClass *parent = 0); - compilator returns error:

Thanks for any suggestions.

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

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

发布评论

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

评论(2

禾厶谷欠 2024-08-29 11:11:41

如果您确信 secondClass 的父级始终是 firstClass 那么您可以这样做:

static_cast<firstClass *>(parent)->doSomething();

或者您可以使用 qobject_cast 并检查确保 parent 实际上是 firstClass 的实例:

firstClass *myParent = qobject_cast<firstClass *>(parent);
if(myParent){
    myParent->doSomething();
}

If you are positive that the parent of secondClass is always going to be firstClass then you can do this:

static_cast<firstClass *>(parent)->doSomething();

Alternatively you can use qobject_cast and check to make sure that parent is actually an instance of firstClass:

firstClass *myParent = qobject_cast<firstClass *>(parent);
if(myParent){
    myParent->doSomething();
}
再浓的妆也掩不了殇 2024-08-29 11:11:41

更类似于 Qt 的方法是使用信号和槽,而不是尝试直接调用不同的函数。

class firstClass : public QWidget
{
    Q_OBJECT

public:
     firstClass(QWidget *parent = 0);
    ~firstClass();

public slot:
    void doSomething();

private:
    secondClass * myClass;
};

class secondClass : public QWidget
{
    Q_OBJECT

public:
    secondClass(QWidget *parent = 0);
    void doSomethingElse()
    {
        // ...
        emit ( triggerDoSomething() );
        // ...
    }

signal:
    void triggerDoSomething();
};

firstClass::firstClass(QWidget *parent) : 
    QWidget(parent), myClass(new secondClass(this))
{
    // ...
    bool connected = connect(myClass, SIGNAL(triggerDoSomething()),
        SLOT(doSomething()));
    assert( connected );
}

The more Qt-ish way to do this would be to use signals and slots, instead of trying to directly call a different function.

class firstClass : public QWidget
{
    Q_OBJECT

public:
     firstClass(QWidget *parent = 0);
    ~firstClass();

public slot:
    void doSomething();

private:
    secondClass * myClass;
};

class secondClass : public QWidget
{
    Q_OBJECT

public:
    secondClass(QWidget *parent = 0);
    void doSomethingElse()
    {
        // ...
        emit ( triggerDoSomething() );
        // ...
    }

signal:
    void triggerDoSomething();
};

firstClass::firstClass(QWidget *parent) : 
    QWidget(parent), myClass(new secondClass(this))
{
    // ...
    bool connected = connect(myClass, SIGNAL(triggerDoSomething()),
        SLOT(doSomething()));
    assert( connected );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文