Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7431489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:18:14+00:00 2026-05-29T09:18:14+00:00

I’m trying to set a variable that can be accessed from multiple children at

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:18:14+00:00Added an answer on May 29, 2026 at 9:18 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.