【问题标题】:Cannot access QMainWindow variables from child class无法从子类访问 QMainWindow 变量
【发布时间】:2012-02-08 05:29:21
【问题描述】:

我正在尝试设置一个可以从 MainWindow (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* 指针并将其转换,我尝试将“全局”int 公开并直接访问它等等。有什么想法我需要做什么?

【问题讨论】:

  • 尝试使用qobject_cast,并尝试使用qDebug() &lt;&lt; this-&gt;parent()。这将有助于弄清楚发生了什么。
  • 代码中最后一行来自哪里?它会在演员中崩溃,因为父母没有设置它。我建议您像这样访问您的父母,而是让父小部件将值传递给孩子。
  • 最后一行在代码的更下方。我确实确保设置了父级 - new SomeWidget(this).
  • 此外,这里关于从父级传递给子级的问题是我有一个作为 QMainWindow 父级的扩展坞,并单独的“项目”子级。我需要孩子们能够看到存储在父级中的变量的当前状态。另一种方法是将每个子节点更新为变量的当前状态,但我只想将其作为最后的手段。

标签: c++ qt segmentation-fault parent global


【解决方案1】:

只需将 *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;
    }

【讨论】:

  • 我也试过了,使用 qpbject_cast 而不是 dynamic_cast(也是 static_cast),但是当我尝试检索 globalInt 时仍然会出现段错误。
【解决方案2】:

您可以使用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;
}

【讨论】:

  • 显然我累了就不能编码了。我实际上是在 MainWindow 的两个父母,duh.. reinterpret_cast(this->parent()->parent()->parent()->parent()->variable) 成功了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2011-09-05
  • 1970-01-01
  • 2014-06-04
  • 2013-01-16
  • 2016-12-27
相关资源
最近更新 更多