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 5949037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:11:35+00:00 2026-05-22T17:11:35+00:00

I have an application that opens a QDialog for some flow, and when it

  • 0

I have an application that opens a QDialog for some flow, and when it is closed, the QMainWindow is opened.

In the QDialog I create some objects that I’d like to pass as a pointer (or some other way) to the QMainWindow. For example I create a SysTray object that needs to change its state from the QMainWindow. What is the best way? Singletons?

update .
after implementing the sulotion another question was rises , does the QDialog is cleaned from memory that means , invoked its destructor ? i don’t think its the case . i have to do some object cleaning , and there destructor never invoked

  • 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-22T17:11:36+00:00Added an answer on May 22, 2026 at 5:11 pm

    One approach is to allocate on the heap with the QMainWindow as a parent. The QObject-hierarchy will take care of freeing the memory and you will access the object for the lifetime of the QMainWindow.

    If at any point you know for sure that you don’t need the QDialog or the shared object anymore, you can call deleteLater.

    Example:

    class MyDialog : public QDialog
    {
        Q_OBJECT
    
        QObject* _objToShare;
    
    public:    
        QObject* objToShare() const { return _objToShare; }
    
        MyDialog(QObject* parent = 0) : QDialog(parent)
        {
            _objToShare = new QObject(this); // either use this as the parent
                                             // or free by hand in the destructor
        }
    
        // ...
     };
    
     class MyWindow : public QMainWindow
     {
         Q_OBJECT
         // ...
    
         QObject* ptrToSharedObj; // alternatively you can hold a pointer
                                  // to your MyDialog instance
     };
    

    Where you use your QDialog:

     MyDialog* d = new MyDialog(this);
     d->exec();
     ptrToSharedObj = d->objToShare();
     // don't delete d explicitly, the QObject hierarchy will take care of that
    

    A better (and likely more memory-friendly) approach is to use a shared pointer implementation. Qt has a variety of smart pointer classes, one of which is QSharedPointer. It is basically a thread-safe, reference-counted pointer which means that if you grab one that points to the shared object of your QDialog, it will not get freed as long as there are any other QSharedPointers pointing to it. Keep in mind that this might cause circular references if you are not careful.

    Example:

    class MyDialog : public QDialog
    {
        Q_OBJECT
    
        QSharedPointer<QObject> _objToShare; 
                                     // ^ don't delete in the destructor
    public:
    
        QSharedPointer<QObject> objToShare() const { return _objToShare; }
    
        MyDialog(QObject* parent = 0) : QDialog(parent)
        {
            _objToShare = QSharedPointer<QObject>(new QObject); 
            // no parent to avoid destruction when the dialog is destructed
        }
    
        // ...
     };
    
     class MyWindow : public QMainWindow
     {
         Q_OBJECT
         // ...
    
         QSharedPointer<QObject> ptrToSharedObj;
     };
    

    Where you use your QDialog:

    MyDialog d;
    d.exec();
    ptrToSharedObj = d.objToShare(); 
    

    From this point on, even if d goes out of scope (i.e. destroyed), you will still have a valid pointer to the shared data (well, of course unless you do something explicitly to invalidate it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Windows shell script that opens an application. I'd like to modify
I have an application that IMPLICITLY opens a handle on a dll/file. At some
I have an application that opens another class using intent : private void createRepository(){
I have a Linux application that opens a UDP socket and binds it to
I have a VB.net test application that clicks a link that opens the Microsoft
We have a Win32 application that hosts the .NET runtime and opens up .NET
I have a WPF application that will always run on windows 7, it opens
I have a script that opens Powerpoint from my application and exports all slides.
I have an application right now that starts a process, then opens a file
I have an application that opens a div with several thumbnails of images. When

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.