Qub1
Qub1

Reputation: 1244

How to use a class inside another class?

I'm developing a Qt application and I'm really new to C++. What I'm trying to do, is create a class as a variable and then use its contents from another class.

My structure and what I'm trying to do, indicated by --> and <--:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "settings.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT

    public:
        -->BHSettings settings(qApp->applicationDirPath() + "/settings.ini");--<

        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();

    private:
        Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::doSomething() {
    -->settings.loadSettings();<--
}

settings.h

#ifndef BHSETTINGS_H
#define BHSETTINGS_H

#include <QSettings>

class BHSettings : public QSettings {
    public:
        QString theme;

        BHSettings(QString settingsFilePath);
        void loadSettings();
        void saveSettings();
        void saveSettings();
};

#endif // BHSETTINGS_H

settings.cpp

#include "settings.h"

BHSettings::BHSettings(QString settingsFilePath) : QSettings(settingsFilePath, QSettings::IniFormat) {
    loadSettings();
    saveSettings();
}

void BHSettings::loadSettings() {
    theme = getTheme();
}

void BHSettings::saveSettings() {
    setValue("General/Theme", theme);
}

QString BHSettings::getTheme() {
    return value("General/Theme", "default").toString();
}

I'm completely lost as how to do this. Some guidance as how to define another class to use its methods would be great.

Upvotes: 1

Views: 142

Answers (3)

Christophe
Christophe

Reputation: 73376

In your MainWindow class, define the variable:

public:
    BHSettings settings; 

In the constructor initialise this memeber:

MainWindow::MainWindow(QWidget *parent) : ... , settings(qApp->applicationDirPath() + "/settings.ini") 
{
...
}

Upvotes: 1

R Sahu
R Sahu

Reputation: 206607

Declare the member in the class definition without the initialization code.

   BHSettings settings;

Add the initialization code in the constructor of the class.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent), ui(new Ui::MainWindow),
    settings(qApp->applicationDirPath() + "/settings.ini")
{
    ui->setupUi(this);
}

You can use:

void MainWindow::doSomething() {
    settings.loadSettings();
}

if you don't need the settings before that function. You may also call it in the constructor if that makes sense.

Upvotes: 1

Marco A.
Marco A.

Reputation: 43662

You had a great start but since your BHSettings class has a non-default constructor, in order to have it as a member variable you should initialize it into your constructor's initialization list

MainWindow::MainWindow(QWidget *parent) : 
  QMainWindow(parent), 
  ui(new Ui::MainWindow), 
  settings(qApp->applicationDirPath() + "/settings.ini")  <--
{
  ui->setupUi(this);
}

you can't initialize it in the class declaration as an inline-declaration or something like you were doing.

Also notice that this will cause your settings object to be initialized (i.e. BHSettings's object constructed) each time you instantiate the MainWindow class

Upvotes: 1

Related Questions