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

How to avoid a circular dependency in Qt creator



  • Hello. I want to avoid a circular dependency. I currently think about a sample project like avoiding a circu![alt text](image url)lar dependency.

    Below is my sample project.

    main.qml

    ApplicationWindow  {
    
    	Button{
    		・・・
    		onClicked: QmlAccessSlot.fnConnBtn()
    	}
    
    	
    	Text {
    		id: dispText
    		・・・
    	}
    
    	Connections {
    		target:QmlAccessSignal
    		onDispText: {
    			dispText.text = dispChar
    		}
    	}
    
    }
    

    main.cpp

    int main(int argc, char *argv[])
    {
    	//QmlAccessSignal
    	qmlRegisterSingletonType<QmlAccessSignal>("QmlAccessSignal", 1, 0,"QmlAccessSignal")
    
    	//QmlAccessSlot
    	qmlRegisterSingletonType<QmlAccessSlot>("QmlAccessSlot", 1, 0,"QmlAccessSlot")
    	・・・・・・
    }
    

    QmlAccessSlot

    class QmlAccessSlot : public QObject
    {
    	Q_OBJECT
    public:
    	explicit QmlAccessSlot(QObject *parent = nullptr ){
    		m_Process = new Process();
    	}
    
    public slots:
    	void fnConnBtn() {
    		m_Process->fnProcess();
    	}
    
    private:
    	Process* m_Process;
    }
    

    Process

    class Process
    {
    public:
    	Process() {
    		//I can't make an instance here because I've already made the instance in main.cpp.
    		//m_QmlAccessSignal = new QmlAccessSignal();
    	}
    
    	QmlAccessSignal* m_QmlAccessSignal;
    
    	void fnProcess() {
    		//I want t use fnDispText and emit signal in  QmlAccessSignal
    		//m_QmlAccessSignal->fnDispText(dispChar);
    	}
    };
    

    QmlAccessSignal

    class QmlAccessSignal : public QObject
    {
    	Q_OBJECT
    public:
    ・・・・
    	void fnDispText(QString dispChar) {
    		emit dispText(dispChar);
    	}
    
    signals:
    	void dispText(QString dispChar);
    }
    

    0b62a083-22a3-4091-a570-36a4397f27de-キャプチャ.PNG
    Figure. the component image of a sample project

    This is the project some characters are displayed on a text when pushing a button on a window. "But if I think that I avoid the circular dependency, I have a problem I can't make an instance of "QmlAccessSignal" in "Process" and
    can't delegate that of "QmlAccessSignal". Do you have any solution?


  • Lifetime Qt Champion

    Hi,

    That architecture is pretty convoluted for no real benefits. Why not have your Process class inherit QObject and properly implemented signals and slots ?



  • @SGaist said in How to avoid a circular dependency in Qt creator:

    convoluted

    Hello. Do you mean why I didn't implement signals/slots in Process inherited QObject ??


  • Lifetime Qt Champion

    @SGaist said in How to avoid a circular dependency in Qt creator:

    Why not have your Process class inherit QObject and properly implemented signals and slots ?

    That is what I was asking.



  • @SGaist said in How to avoid a circular dependency in Qt creator:

    Why not have your Process class inherit QObject and properly implemented signals and slots ?

    I see. Is it possible to avoid a circular dependency ?
    For example, using your suggestion, I assume I make a components which the "Process" delegate to the "ProcessSlot" class.
    "ProcessSlot" processes a value(in fnProcessSlot) which gets at a slot function in "Process". "ProcessSlot" have to emit the value to QML. But "ProcessSlot" have no signal function to QML directly. So "ProcessSlot" delegating the value of the signal to "Process", a the signal function in Process emits the value(in fnDispTextSignal) to QML. But in case of this component, a circular dependency happens between "Process" and ProcessSlot. I don't want to make this component.

    tetet.PNG


  • Moderators

    So you want to create a circular dependency but you want to avoid it?

    You don't need this "ProcessSlot" class at all, just forget about it. Do the processing in Process class and all will work fine.

    If you really want your ProcessSlot to be there, make it a QObject, too - then you can send a signal to Process when computation in ProcessSlot is done.

    BTW. All this sounds a lot like reinventing the wheel. See QtConcurrent, QFuture classes and/ or QtPromise library.


Log in to reply