Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to avoid a circular dependency in Qt creator
Forum Updated to NodeBB v4.3 + New Features

How to avoid a circular dependency in Qt creator

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 731 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    morita
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      SGaistS 1 Reply Last reply
      1
      • M Offline
        M Offline
        morita
        wrote on last edited by
        #3

        @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 ??

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

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

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            morita
            wrote on last edited by
            #5

            @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

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              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.

              (Z(:^

              M 1 Reply Last reply
              2
              • sierdzioS sierdzio

                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.

                M Offline
                M Offline
                morita
                wrote on last edited by
                #7

                @sierdzio I want to avoid a circular dependency.
                If I inherit QObject to ProessSlot and send a signal to Process , a circular dependency doesn't happen as you told me.The reason why I make this component is that I have multi classes like ProcessSlot which Process delegates
                and multi classes which a child class like ProcessSlot delegate.
                And I want to utilize "QmlAcessSignal" in child class and send signals in that.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved