Sending signals between 2 cpp files back and forth without circular dependency?
-
Hello,
what is the best approach to sending a string between two .cpp files without getting a circular dependency, without using a forward declaration?Both widgets are inside dockwidgets which are both created inside the mainwindow:
mainwindow.cpp-->dockWidgetA.cpp-->QTreeView.cpp (send and receive a String here)
mainwindow.cpp-->dockWidgetB.cpp-->TextEdit.cpp (send and receive a String here)
Best regards
StudentScripter -
Depends. Signals and slots or pass it directly.
Why should there be a circular dependency?
You just dont include each others header :)
MainWindow
knows about its childs but the children shouldnt know exactly aboutMainWindow
. The connection is established in yourMainWindow
-
@Pl45m4 Yeah well but how can i pass from QTreeView.cpp to TextEdit.cpp? Thy don't know about each other and also Mainwindow.cpp does not know about the exact instances of QTreeView or TextEdit as they are declared to be children in their respective Dockwidget subclass.
-
Then make use of the meta object tree (parent-child-structure).
TheQDockWidget
should be a parent of each widget whileQMainWindow
is parent of eachQDockWidget
:In mainWindow something like:
QTreeView *view = this->findChild<QTreeView *>("objectname"); // when there are multiple TreeViews, you can specify an object name to get the right one QTextEdit *edit = this->findChild<QTextEdit *>("textEdit_objectName"); // same here... connect(edit, &QTextEdit::sendString, view, &QTreeView::displayString);