1
votes

I am creating a UI with Qt and there are two elements which may or may not be present. Additionally, their parents are different elements as well. However, one affects the other.

How should I structure the signals/slots (or should I not even use that pattern) in the best way?

The methods that come to mind all seem like hacks:

  • create a signal/slot in all parents and pass up and then back down the signal
  • create a signal/slot in the closest common parent of both then have the children connect their signals/slots to the parents'
  • on creation of one navigate the structure of the other to get the element and then connect signals/slots directly. Any guidance here is greatly appreciated.

Edit: "present" means that there is a button that the user may press that creates an element and adds it to the layout. So depending on the combination of button presses, an element may be present or not.

"affect" means it changes its state. for example, there is a list of items and a button elsewhere which adds an element to the list.

For an example, imagine a tabbed pane which contains a todo list. There is a button not in the tabbed pane which adds an item to the list. The tabbed pane does NOT create all the elements of the pane. It creates only the elements of the visible pane and deletes them when the pane is switched away. Therefore, the list may or may not exist.

1
Please paste code; that is worth thousand words. :) What do you mean by "present", like visible/disabled/etc? What do you mean by "affect"? Why do you mention they have different parents? Also, what task are you trying to solve? Could you provide a specific example?lpapp
I added some edits to explain the terminology. As far as why I mention they have different parents, that is because it complicates the solution. The question is about a specific problem, but I would rather have a generic solution than a specific one. Also, having code here would take up wayyyy too much space.chacham15
No, please paste the shortest code representing your issue.lpapp
I tried to add a concrete example. I cannot paste code for legal reasons unless I rewrite all of it just to paste here which would take way too much time as you can tell from the number of custom components.chacham15
@chacham15: the code would explain it well what you wish, and writing dummy names would abstract your business logic out. See sscce.org for details, or you could create a screenshot with the QtDesigner or simply just writing some qml up for representing the issue. It is hard to get what you mean in the current state. I am still puzzled why you cannot just use connect(myUiElement1, SIGNAL(mysignal(...)), myUiElement2, SLOT(mySlot(...)));lpapp

1 Answers

1
votes

The UI elements are QWidgets. All QWidgets are QObjects. Any QObject's signals can be connect()ed to any other object's slots. The hierarchy of parent/child relationships is entirely immaterial.

You seem to be confusing signal-slot connections with events, which can be in fact passed up the object hierarchy if they remain ignored by given object.

It's also worth noting that signal-slot connections are safe in spite of QObjects being destroyed. When an object with connected signals or slots gets destroyed, the connections are safely torn down. The only thing you can't do is deleting the sender nor receiver object within a slot - use object->deleteLater() instead.