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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:44:27+00:00 2026-05-27T18:44:27+00:00

I’ve just registered here, so before anything, thanks for reading! I’m developing an application

  • 0

I’ve just registered here, so before anything, thanks for reading!

I’m developing an application using Qt, but I need some advices regarding sharing class members.
I am working with some collections (QHash) of project-specific structs and I created a polymorphic class to manage those collections. A derived class also manage some UI components (shared through pointers or references) so it can automatically represent those structs in the UI.. It’s something like this:

Main class

class ArmyEditor : public QMainWindow
{
//.... some specific functions ...

private:
    Ui::ArmyEditor *ui;

    // Specific structs
    QHash<QString, GameCategory> categories;
    QHash<QString, Column> columns;
    QHash<QString, GameProperty> properties;
    QHash<QString, UnitOption> commonOptions;
    QHash<QString, UnitOption> inheritedOptions;
    QHash<QString, GameItem> items;
    QHash<QString, GameItem> inheritedItems;
    QHash<QString, GlobalText> globalTexts;
    QHash<QString, GlobalText> inheritedGlobalTexts;
    QHash<QString, Unit> units;
};

Base class for collection managing..

class StructManager : public QObject {
    Q_OBJECT
public:

    explicit StructManager(QWidget* parent = 0);

    // ...Functions that perform actions in shared components...

protected:
    QWidget *parent;
    QHash<QString, GameCategory> *categories;
    QHash<QString, Column> *columns;
    QHash<QString, GameProperty> *properties;
    QHash<QString, UnitOption> *commonOptions;
    QHash<QString, GameItem> *commonItems;
    QHash<QString, GlobalText> *globalTexts;
    QHash<QString, Unit> *units;
};

Derived class for UI management and so on

class StructEditor : public StructManager
{
    Q_OBJECT
public:

    StructEditor(QWidget* parent = 0);

    // ...Overriden functions to automatically represent structs in the shared members..

protected:
    QTreeWidget *catList;
    QListWidget *colList;
    QTreeWidget *propList;
    QTreeWidget *optList;
    QListWidget *optActionList;
    QTreeWidget *itemList;
    QListWidget *itemActionList;
    QTableWidget *globalTextsGrid;
    QTreeWidget *unitTree;
    QComboBox *optCategory;
    QComboBox *itemCategory;
    QComboBox *unitCategory;
    QComboBox *optAmountColumn;
    QComboBox *optSetColumn;
};

And I share some UI members in the constructor of the MainWindow class..

ArmyEditor::ArmyEditor(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ArmyEditor)
{
    ui->setupUi(this);

    // Setup Army Struct Manager

    armyManager = new StructEditor(this);

    armyManager->setCatList(ui->catList);
    armyManager->setOptList(ui->optList);
    armyManager->setOptActionList(ui->optActionList);
    armyManager->setItemList(ui->itemList);
    armyManager->setItemActionList(ui->itemActionList);
    armyManager->setGlobalTextsGrid(ui->globalTextsGrid);
    armyManager->setUnitTree(ui->unitTree);
    armyManager->setOptCategory(ui->optCategory);
    armyManager->setItemCategory(ui->itemCategory);
    armyManager->setUnitCategory(ui->unitCategory);
    armyManager->setOptAmountColumn(ui->optAmountColumn);
    armyManager->setOptSetColumn(ui->optSetColumn);
    armyManager->setCategories(&categories);
    armyManager->setOptions(&commonOptions);
    armyManager->setItems(&items);
    armyManager->setGlobalTexts(&globalTexts);

    //.. some other code ..
};

I call the functions from the StructEditor class when I need to add a new Category or something like that..
My project consists of three applications, those of which use almost the same methods for managing these structs, so I decided to use a class with the methods to add, update, remove and represent the structs in the UI sharing some member pointers with the MainWindow class. But now I’m thinking it is a bit dirty and I should not share these members because the MainWindow loses control over them. I was thinking I could create the collections of my structs in the Base class and make a method so I can read (securely) members of those collections in the MainWindow class, but my problem is with UI members. I could use signals and manage those members directly in the MainWindow class, but then I would have to duplicate a lot of code and it would complicate (a bit) the code changes, which is the main reason I decided to unify those methods in a class.

So, my question is: Is there any way to ‘unify’ those methods without having to share members or using global variables (because would have the same effect)? I would like to have those methods in a separated file.

Thanks and greetings!

  • 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-27T18:44:28+00:00Added an answer on May 27, 2026 at 6:44 pm

    You should look at the inheritance you have provided and where your data resides.

    As AJG85 pointed out you should decouple your data from your manipulators and view so for example:

    class StructManager {
    private:
        typedef QHash<QString, GameCategory> CategoryT;
        CategoryT categories;
        ....
    public:
        CategoryT& getCategories() { return categories; }
    };
    
    class StructEditor : public QObject
    {
    Q_OBJECT
    protected:
        StructManager sm;
    ...
    };
    
    class ArmyEditor : public StructEditor
    {
    public:
        <manipulation methods>
    };
    
    class ArmyWindow : public MainWindow
    {
    private:
        ArmyEditor ae;
    ....
    };
    

    Once you have followed this structure you will find that all events from the UI can be passed on to your editors which manipulate data and display fields that read it.

    This way your have no sharing of data between object and no code duplication. If you want to be really fancy do this:

    class BaseEditor
    {
    protected:
        void ManipulateCategories() = 0;
    };
    
    class StructEditor : public QObject, public BaseEditor
    {
       ....
    };
    
    class ArmyEditor : public QObject, public BaseEditor
    {
     ....
    };
    

    I am sure you can figure out implementations for the individual editor classes.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need to clean up various Word 'smart' characters in user input, including but
I have thousands of HTML files to process using Groovy/Java and I need to
I am reading a book about Javascript and jQuery and using one of the
For some reason, after submitting a string like this Jack’s Spindle from a text
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
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into

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.