Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to access QStringList present in 1 class in all other the remaning classes?



  • i have a QStringList in 1 class

    completer.cpp

    #include "completer.h"
    
    Completer::Completer(QObject *parent)
        : QObject(parent)
    {
    }
    Completer::~Completer()
    {
    }
    

    completer.h

    #pragma once
    #include <QObject>
    
    class Completer : public QObject
    {
        Q_OBJECT
    public:
        Completer(QObject *parent);
        ~Completer();
        QStringList global_completer_list;
    };
    

    i also have 3 other Dialog classes, all of the dialogs contain QLineedits , one of the class is named as car.h and car.cpp, so , my question is how do i create an object of completer class and access the QStringlist of completer class in all another classes, i am having syntax problems.



  • @learnist
    Not sure what your problem is, as this is just C++, and has nothing to do with QStringList, or even Qt:

    #include "completer.h"
    ...
    Completer *completer = new Completer;
    ...
    qDebug() << completer->global_completer_list;
    


  • @JonB Agreed , not Qt , but more of OOPS concepts problem, i want to know how can i access the QStringList in my new class (a.cpp and a.h), where do i place your code, in my .h or .cpp ?


  • Moderators

    @learnist the fact that you have a public member variable named global_completer_list screams spaghetti code with global variables to me!

    You should probably rethink and redesign your application concept!



  • @JonB i get the error no default constructor exists for class "Completer" in

    Completer *completer = new Completer;
    

  • Moderators

    @learnist
    either

    Completer(QObject *parent = nullptr);
    

    or

    Completer *completer = new Completer(nullptr);