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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:22:35+00:00 2026-06-09T19:22:35+00:00

I have the following layout: MainWindow <——— Settings \ \ V PerformOps MainWindow takes

  • 0

I have the following layout:

MainWindow  <--------- Settings
    \
     \
      V
   PerformOps

MainWindow takes in variables passed from Settings, and passes them on to PerformOps.
If the Settings class is not used, MainWindow passed on defaults to PerformOps.

Currently I do:

class Settings{

public: 
   Settings(*parent);
   var1, var2, var3
   .
   .
   void exec();
}

class PerformOps{

public: 
   PerformOps();
   var1, var2, var3;
   .
   .
   void start();
}


class MainWindow{

private:
  Settings *set;    //share pointers over all methods
  PerformOps *op;

  bool settings_not_clicked = false;

  void saveChanges()
  {
     QSettings settings("my","app");
     settings.setValue("value1", op->var1);
     settings.setValue("value2", op->var2);
     settings.setValue("value3", op->var3);
  }

  void loadChanges()
  {
     QSettings settings("my","app");
     op->var1 = settings.value("value1");
     op->var2 = settings.value("value2");
     op->var3 = settings.value("value3");

  }

  void closeEvent(QCloseEvent *event)
  {
       event->ignore();
       saveChanges();
       event->accept();
  }

  void takeDataAndDoStuff()   //This is always called
  {
     op = new PerformOps;

     if(settings_not_clicked) {
        loadChanges()
     }
     else {
         op->var1 = 33;
         op->var2 = "Geronimo!";
         op->var3 = true;              
     }

     op->start();
  }


  void getStuff_maybe_if_clicked()   //This might not be always called
  {
     Settings *set = new Settings(this);
     set->exec()      //Pause parent, run Settings

     settings_not_clicked = false;
  }

Question: Is there a cleaner way to share data across classes without resorting to the dirty method of : op->var1 = set->var1;, taking into account that the set pointer might not always be initialised?

  • 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-06-09T19:22:37+00:00Added an answer on June 9, 2026 at 7:22 pm

    Well, the approach itself is not that bad, however there are some things you can improve.

    First of all, if I understood you correctly, you want to pass the settings if they exist, and pass the default values if they don’t. In this case you can utilize the constructor:

    class PerformOps
    {
    public:
        PerformOps( int v1 = 33, string v2 = "Geronimo!", bool v3 = true ): var1(v1), var2(v2), var3(v3)
        {
        }
        <...>
    }
    

    Now, if you call the constructor as PerformOps(), the default values will be set. If you call it and feed it some of the values, it will use them:

    <...>
    PerformOps *op;
    
    if(settings_not_clicked) {
        op = new PerformOps( set->var1, set->var2, set->var3 );
    }
    else {
        op = new PerformOps();              
    }
     <...>
    

    of course, if you don’t want to do it via the cunstructor, you could just make a function and call it like “setData()” and use the same technique with default function parameters.


    Now, as for the pointers. It is a good idea to always initialize pointers with NULL, or nullptr if you have c++0x. Also, when you delete the memory, assign the pointer to NULL or nullptr again. This way you will be able to always see if the pointer is valid by a simple check.

    someClass * ptr = nullptr;
    <...>
    ptr = new someClass();
    <...>
    delete ptr;
    ptr = nullptr;
    

    UPD

    I would suggest you to get rid of your Settings class, and just use QSettings directly. You will not need to mess with the pointers, and reading/writing to QSettings is very fast.

    Now, also don’t forget that you can use QSettings from the heap:

    QSettings * settings = new QSettings();
    

    If you want each settings set to have a “parent”, you could just derive your Settings class from QSettings, and just add one parent field into it. This will keep all the functionality of QSettings, which is very convenient.

    Actually, your approach is fine too, all you need to do is just to check if the pointers are valid.

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

Sidebar

Related Questions

I have the following layout : And following code : public class MailSenderActivity extends
I have the following Layout which does not work: <LinearLayout android:orientation=horizontal android:layout_width=match_parent android:id=@+id/experienceLayout android:background=#ffffff
I have a window with this basic layout: <Window x:Class=GridStuffs.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350
I have the following layout in a sidebar: <div class=sidebar> <div class=center> <div>button one</div>
I have the following layout in my page (simplified) <h:form> <h:commandButton action=#{bean.save} value=Save/> <rich:tabPanel
I have the following layout for my mvc project: /Controllers /Demo /Demo/DemoArea1Controller /Demo/DemoArea2Controller etc...
I have the following layout defined for one of my Activities: <?xml version=1.0 encoding=utf-8?>
I have the following layout: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent android:background=@drawable/aussie_bg_big > <ImageView android:id=@+id/titleImage android:layout_width=wrap_content
I have the following XML layout for a ListActivity. <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
I have the following XML layout file, HELPME.XML <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent

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.