Welcome, Skyler.
Signals and slots are one of the most powerful mechanisms in Qt and it was also what attracted me to Qt in the first place, in addition to the cross-platform portability.
To answer your question, I will pose another: Why do you need to create buttons and other widgets dynamically?
Consider creating a UI that you can define statically using the Qt UI editor; use the ability to create widget overlays (using QStackedWidget / QStackedLayout stacked widgets / layouts) or simulate dynamic behavior by showing / hiding widgets in containers controlled by QLayout-based layouts which will dynamically adjust based on visibility of the contained widgets.
Creating a truly dynamic UI where widgets are created and destroyed programmatically is difficult and bug-prone. If you really must do this, consider using QSignalMapper. This class is designed to funnel a signal (like QPushButton::clicked()) through a single slot (QSignalMapper::map()) which then emits one of the "mappedxxx()" signals with an identifier for the widget that emitted the original signal. That mapping signal can then be connected to a slot that decodes the identifier and dispatches to the appropriate handler. This avoids the use of messy lambda captures. I find lambdas hard to debug except in the simplest use cases.
It is personal preference, but I prefer to create signal-slot connections in code rather than define them in the UI editor. This makes it easy to identify which signals and slots are (or are not) conected. Doing it in the UI editor is prone to bugginess - the creation of the connection is done in boilerplate code generated by MOC and UIC, which is difficult to inspect and debug. More than once I have mistakenly defined a connection in the UI editor and again in code and then see the mysterious result that a single button click results in two calls to the onClicked() slot.
An ironclad rule for UI construction: Always use QLayout-derived classes to contain and arrange the child widgets in a parent QWidget, QDialog or other widget designed for parenting children. Never create a UI by manually placing child widgets within a parent's coordinate space. Layouts will dynamically control the size and placement of child widgets as the parent widgets are resized and will ensure that alignments and other relative positioning is maintained.
Hope this helps.




Reply With Quote

Bookmarks