Frage

I am designing a game that includes a world map widget; this widget has 100+ region widgets as its children. There is also a separate information panel widget. When I click on a region, I want that information to be sent and displayed on the information panel. My thoughts so far give me 2 options:

  1. Connect each of the 100+ regions to the information panel. This seems ugly to me as I feel like the information panel should only know about the world map widget, not its internal workings (such as children).

  2. Connect each of the 100+ regions to the map widget, and then have the map send another signal to be picked up by other widgets in the game such as the information panel. This seems nicer as far as having independent widgets goes, but requires 2 signals instead of 1.

Is one of these methods preferable to the other? Or is there another solution I am missing entirely?

War es hilfreich?

Lösung

I used to work for a company which made its own geospatial information system (GIS) product and we had this exact use case using Qt as well.

From my experience we would use option #2 as it encapsulated the details and relationships between the information panel (i.e. a view) and the world map (i.e. a model).

In the future maybe your world map will contain more than selectable regions and the information panel may not want to have to start knowing about all of the different entities in your world map to display them. So yes while #2 results in some additional signals it can be better from an encapsulation and expansion standpoint.

For example connect the information panel to a signal from your world map regarding a selected entity rather than a region. Sure a region is an entity and using region directly could work but again in the future maybe your world map will have selectable buildings, markers, vehicles, etc...

i.e.

connect( mapWidget, SIGNAL( selectedEntityChanged( MapEntity* ) ), 
         infoPanel, SLOT( onSelectedEntityChanged( MapEntity* ) ) );

This way your information panel could be designed to generically display information about an entity, again from which a region is derived. If you want to add say a vehicle to your map and make it so the information panel can display it, all you'll have to do is derive your vehicle from entity and you'll be good to go.

Great question welcome to StackOverflow!

Andere Tipps

Remember that each QObject comes with a property system - a general purpose, Python-like key-value store. The keys are strings, and the values are variants. Since QWidget is a QObject, you can leverage that. Just by itself this makes QSignalMapper quite redundant.

You can also use the undocumented, but slightly faster userData mechanism - it uses integer keys instead of string keys. The unique key ids are obtained through registerUserData(), with semantics otherwise identical to QEvent::registerEventType.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top