无法从子类访问 QMainWindow 变量

发布于 2025-01-03 16:38:37 字数 820 浏览 1 评论 0原文

我正在尝试设置一个可以从主窗口(QMainWindow)级别的多个子项访问的变量。问题是每当我尝试从孩子(或孙子)访问它时,我都会遇到分段错误。

这是所涉及代码的模型:

//MainWindow.h
...
public:
    int getVariable();
    void setVariable(int i);
...
private:
    int globalInt;
    SomeWidget *myWidget;


//MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ...
    this->globalInt = 10;
    myWidget = new SomeWidget();
    myWidget->setParent(this);
    ....
}
int getVariable()
{
    return this->globalInt;
}
void setVariable(int i)
{
    this->globalInt = i;
}
...



//SomeWidget.cpp
...
int x = (static_cast<MainWindow*>(this->parent()))->getVariable(); //Causes Segfault
...

老实说,我不知道我做错了什么。我尝试创建一个新的 MainWindow* 指向父级的指针并对其进行强制转换,我尝试将“global”int 公开并直接访问它,等等。我需要做什么?

I'm trying to set a variable that can be accessed from multiple children at the MainWindow (QMainWindow) level. The issue is whenever I try to access it from a child (or grandchild), I get a segmentation fault.

Here's a mock up of the involved code:

//MainWindow.h
...
public:
    int getVariable();
    void setVariable(int i);
...
private:
    int globalInt;
    SomeWidget *myWidget;


//MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ...
    this->globalInt = 10;
    myWidget = new SomeWidget();
    myWidget->setParent(this);
    ....
}
int getVariable()
{
    return this->globalInt;
}
void setVariable(int i)
{
    this->globalInt = i;
}
...



//SomeWidget.cpp
...
int x = (static_cast<MainWindow*>(this->parent()))->getVariable(); //Causes Segfault
...

I honestly have no idea what I'm doing wrong. I've tried creating a new MainWindow* pointer to the parent and casting it, I've tried making the "global" int public and directly accessing it, etc. etc. Any ideas what I need to do?

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

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

发布评论

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

评论(2

扮仙女 2025-01-10 16:38:37

只需将 *this 作为构造函数参数传递给 SomeWidget 即可:

myWidget = new SomeWidget(this);

在稍后实现 SomeWidget 时,您可以通过以下方式访问 MainWindow 的成员:

void SomeWidget::someFunc() {
    MainWindow *w = qobject_cast<MainWindow*>(parent());
    //cphecing the pointer
    if(w != 0) {
         //accessing MainWindow functionality
         int myInt = w->globalInt;
    }

Simply pass *this to SomeWidget as a constructor parameter:

myWidget = new SomeWidget(this);

And than later in implementation of the SomeWidget you can access members of MainWindow following way:

void SomeWidget::someFunc() {
    MainWindow *w = qobject_cast<MainWindow*>(parent());
    //cphecing the pointer
    if(w != 0) {
         //accessing MainWindow functionality
         int myInt = w->globalInt;
    }
抽个烟儿 2025-01-10 16:38:37

您可以使用reinterpret_cast。请注意,这不是一种安全的方法,因为您可以使指针“指向不兼容类的对象,因此取消引用它是不安全的。”(Info

我刚刚上班,所以我只有时间做一个小(而且丑陋的)例子,一个小的控制台程序。

class some_widget
{
public:
    some_widget(){ m_parent = 0;}

    void set_parent( void* p_parent ){m_parent= p_parent;}
    void* get_parent(){return m_parent;}

    void do_something();

private:
    void* m_parent;
};

class main_window
{
public:
    main_window(void);
    ~main_window(void){ delete myWidget; myWidget = 0;}

    int getVariable(){return global_int;}
    void setVariable(int i){global_int = i;}

    some_widget* get_widget(){return myWidget;}

private:
    int global_int;
    some_widget *myWidget;
};

main_window::main_window(void)
{
    global_int = 10;
    myWidget = new some_widget();
    myWidget->set_parent(this);
}

void some_widget::do_something()
{
    if( this->get_parent() != 0 )
    {
        int x = reinterpret_cast<main_window*>(this->get_parent())->getVariable();
    }
}

int main( int argc, char* argv[] )
{
    main_window* mw = new main_window();

    mw->get_widget()->do_something();

    return 0;
}

You could use reinterpret_cast. Note that this is not a secure way, because you can make pointers "that point to an object of an incompatible class, and thus dereferencing it is unsafe." (Info)

I just arrived at work so I only had time for a little (and ugly) example, a small console program.

class some_widget
{
public:
    some_widget(){ m_parent = 0;}

    void set_parent( void* p_parent ){m_parent= p_parent;}
    void* get_parent(){return m_parent;}

    void do_something();

private:
    void* m_parent;
};

class main_window
{
public:
    main_window(void);
    ~main_window(void){ delete myWidget; myWidget = 0;}

    int getVariable(){return global_int;}
    void setVariable(int i){global_int = i;}

    some_widget* get_widget(){return myWidget;}

private:
    int global_int;
    some_widget *myWidget;
};

main_window::main_window(void)
{
    global_int = 10;
    myWidget = new some_widget();
    myWidget->set_parent(this);
}

void some_widget::do_something()
{
    if( this->get_parent() != 0 )
    {
        int x = reinterpret_cast<main_window*>(this->get_parent())->getVariable();
    }
}

int main( int argc, char* argv[] )
{
    main_window* mw = new main_window();

    mw->get_widget()->do_something();

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