Qt5 Virtual Slot
The QDesignerFormWindowInterface class allows you to query and manipulate form windows appearing in Qt Designer's workspace. More...
You warrant to, provide Slot Qt5 true, accurate, current and complete information regarding identity during the registration process. Any false information or impersonation of any person or entity, misrepresentation regarding any affiliation with another person, entity or association, use of false headers or other acts or omissions to conceal. Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. Signals and slots are made possible by Qt's meta-object system.
Header: | #include <QDesignerFormWindowInterface> |
qmake: | QT += designer |
Inherits: | QWidget |
Public Types
If i create a class from a base class with virtual slots, the slots never get called with the new connect-flavour. If i use the old connect-syntax, the slot gets called. What could be the problem? @ class BaseClass: public QObject public slots: virt. override virtual QVariant QTextBrowser:: loadResource (int type, const QUrl &name) Reimplemented from QTextEdit::loadResource. This function is called when the document is loaded and for each image in the document. The type indicates the type of resource to be loaded. An invalid QVariant is returned if the resource cannot be loaded. The default implementation ignores type and tries to. In Qt 5, at least, if you're using the obj-ptr, member-func-ptr, obj-ptr, member-func-ptr version of connect, none of your slots need to be declared as such. – Kyle Strand Jul 13 '16 at 17:01 have to add some really weird behavior: when you mark the overriden methods as slots in the header of the subclass, slots get called all the time even.
flags | Feature |
enum | FeatureFlag { EditFeature, GridFeature, TabOrderFeature, DefaultFeature } |
enum | ResourceFileSaveMode { SaveAllResourceFiles, SaveOnlyUsedResourceFiles, DontSaveResourceFiles } |
Public Functions
QDesignerFormWindowInterface(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()) | |
virtual | ~QDesignerFormWindowInterface() |
virtual QDir | absoluteDir() const = 0 |
QStringList | activeResourceFilePaths() const |
virtual void | addResourceFile(const QString &path) = 0 |
virtual QString | author() const = 0 |
virtual QStringList | checkContents() const = 0 |
virtual QString | comment() const = 0 |
virtual QString | contents() const = 0 |
virtual QDesignerFormEditorInterface * | core() const |
virtual QDesignerFormWindowCursorInterface * | cursor() const = 0 |
virtual void | emitSelectionChanged() = 0 |
virtual QString | exportMacro() const = 0 |
virtual QDesignerFormWindowInterface::Feature | features() const = 0 |
virtual QString | fileName() const = 0 |
virtual QWidget * | formContainer() const = 0 |
virtual QPoint | grid() const = 0 |
virtual bool | hasFeature(QDesignerFormWindowInterface::Feature feature) const = 0 |
virtual QStringList | includeHints() const = 0 |
virtual bool | isDirty() const = 0 |
virtual bool | isManaged(QWidget *widget) const = 0 |
virtual void | layoutDefault(int *margin, int *spacing) = 0 |
virtual void | layoutFunction(QString *margin, QString *spacing) = 0 |
virtual QString | pixmapFunction() const = 0 |
virtual void | removeResourceFile(const QString &path) = 0 |
virtual QDesignerFormWindowInterface::ResourceFileSaveMode | resourceFileSaveMode() const = 0 |
virtual QStringList | resourceFiles() const = 0 |
virtual void | setAuthor(const QString &author) = 0 |
virtual void | setComment(const QString &comment) = 0 |
virtual bool | setContents(QIODevice *device, QString *errorMessage = 0) = 0 |
virtual void | setExportMacro(const QString &exportMacro) = 0 |
virtual void | setIncludeHints(const QStringList &includeHints) = 0 |
virtual void | setLayoutDefault(int margin, int spacing) = 0 |
virtual void | setLayoutFunction(const QString &margin, const QString &spacing) = 0 |
virtual void | setMainContainer(QWidget *mainContainer) = 0 |
virtual void | setPixmapFunction(const QString &pixmapFunction) = 0 |
virtual void | setResourceFileSaveMode(QDesignerFormWindowInterface::ResourceFileSaveMode behavior) = 0 |
Public Slots
void | activateResourceFilePaths(const QStringList &paths, int *errorCount = nullptr, QString *errorMessages = nullptr) |
virtual void | clearSelection(bool update = true) = 0 |
virtual void | manageWidget(QWidget *widget) = 0 |
virtual void | selectWidget(QWidget *widget, bool select = true) = 0 |
virtual bool | setContents(const QString &contents) = 0 |
virtual void | setDirty(bool dirty) = 0 |
virtual void | setFeatures(QDesignerFormWindowInterface::Feature features) = 0 |
virtual void | setFileName(const QString &fileName) = 0 |
virtual void | setGrid(const QPoint &grid) = 0 |
virtual void | unmanageWidget(QWidget *widget) = 0 |
Signals
void | aboutToUnmanageWidget(QWidget *widget) |
void | activated(QWidget *widget) |
void | changed() |
void | featureChanged(QDesignerFormWindowInterface::Feature feature) |
void | fileNameChanged(const QString &fileName) |
void | geometryChanged() |
void | mainContainerChanged(QWidget *mainContainer) |
void | objectRemoved(QObject *object) |
void | resourceFilesChanged() |
void | selectionChanged() |
void | widgetManaged(QWidget *widget) |
void | widgetRemoved(QWidget *widget) |
void | widgetUnmanaged(QWidget *widget) |
Static Public Members
QDesignerFormWindowInterface * | findFormWindow(QWidget *widget) |
QDesignerFormWindowInterface * | findFormWindow(QObject *object) |
Detailed Description
QDesignerFormWindowInterface provides information about the associated form window as well as allowing its properties to be altered. The interface is not intended to be instantiated directly, but to provide access to Qt Designer's current form windows controlled by Qt Designer's form window manager.
If you are looking for the form window containing a specific widget, you can use the static QDesignerFormWindowInterface::findFormWindow() function:
But in addition, you can access any of the current form windows through Qt Designer's form window manager: Use the QDesignerFormEditorInterface::formWindowManager() function to retrieve an interface to the manager. Once you have this interface, you have access to all of Qt Designer's current form windows through the QDesignerFormWindowManagerInterface::formWindow() function. For example:
The pointer to Qt Designer's current QDesignerFormEditorInterface object (formEditor
in the example above) is provided by the QDesignerCustomWidgetInterface::initialize() function's parameter. When implementing a custom widget plugin, you must subclass the QDesignerCustomWidgetInterface class to expose your plugin to Qt Designer.
Once you have the form window, you can query its properties. For example, a plain custom widget plugin is managed by Qt Designer only at its top level, i.e. none of its child widgets can be resized in Qt Designer's workspace. But QDesignerFormWindowInterface provides you with functions that enables you to control whether a widget should be managed by Qt Designer, or not:
The complete list of functions concerning widget management is: isManaged(), manageWidget() and unmanageWidget(). There is also several associated signals: widgetManaged(), widgetRemoved(), aboutToUnmanageWidget() and widgetUnmanaged().
In addition to controlling the management of widgets, you can control the current selection in the form window using the selectWidget(), clearSelection() and emitSelectionChanged() functions, and the selectionChanged() signal.
You can also retrieve information about where the form is stored using absoluteDir(), its include files using includeHints(), and its layout and pixmap functions using layoutDefault(), layoutFunction() and pixmapFunction(). You can find out whether the form window has been modified (but not saved) or not, using the isDirty() function. You can retrieve its author(), its contents(), its fileName(), associated comment() and exportMacro(), its mainContainer(), its features(), its grid() and its resourceFiles().
The interface provides you with functions and slots allowing you to alter most of this information as well. The exception is the directory storing the form window. Finally, there is several signals associated with changes to the information mentioned above and to the form window in general.
See also QDesignerFormWindowCursorInterface, QDesignerFormEditorInterface, and QDesignerFormWindowManagerInterface.
Member Type Documentation
enum QDesignerFormWindowInterface::FeatureFlag
flags QDesignerFormWindowInterface::Feature
This enum describes the features that are available and can be controlled by the form window interface. These values are used when querying the form window to determine which features it supports:
Constant | Value | Description |
---|---|---|
QDesignerFormWindowInterface::EditFeature | 0x01 | Form editing |
QDesignerFormWindowInterface::GridFeature | 0x02 | Grid display and snap-to-grid facilities for editing |
QDesignerFormWindowInterface::TabOrderFeature | 0x04 | Tab order management |
QDesignerFormWindowInterface::DefaultFeature | EditFeature GridFeature | Support for default features (form editing and grid) |
The Feature type is a typedef for QFlags<FeatureFlag>. It stores an OR combination of FeatureFlag values.
See also hasFeature() and features().
enum QDesignerFormWindowInterface::ResourceFileSaveMode
This enum describes how resource files are saved.
Constant | Value | Description |
---|---|---|
QDesignerFormWindowInterface::SaveAllResourceFiles | 0 | Save all resource files. |
QDesignerFormWindowInterface::SaveOnlyUsedResourceFiles | 1 | Save resource files used by form. |
QDesignerFormWindowInterface::DontSaveResourceFiles | 2 | Do not save resource files. |
This enum was introduced or modified in Qt 5.0.
Member Function Documentation
QDesignerFormWindowInterface::QDesignerFormWindowInterface(QWidget *parent = nullptr, Qt::WindowFlagsflags = Qt::WindowFlags())
Constructs a form window interface with the given parent and the specified window flags.
[signal]
void QDesignerFormWindowInterface::aboutToUnmanageWidget(QWidget *widget)
Qt Virtual Slot
This signal is emitted whenever a widget on the form is about to become unmanaged. When this signal is emitted, the specified widget is still managed, and a widgetUnmanaged() signal will follow, indicating when it is no longer managed.
See also unmanageWidget() and isManaged().
[slot]
void QDesignerFormWindowInterface::activateResourceFilePaths(const QStringList &paths, int *errorCount = nullptr, QString *errorMessages = nullptr)
Activates the resource (.qrc) file paths paths, returning the count of errors in errorCount and error message in errorMessages. Qt Designer loads the resources using the QResource class, making them available for form editing.
In IDE integrations, a list of the project's resource (.qrc) file can be activated, making them available to Qt Designer.
This function was introduced in Qt 5.0.
See also activeResourceFilePaths() and QResource.
[signal]
void QDesignerFormWindowInterface::activated(QWidget *widget)
This signal is emitted whenever a widget is activated on the form. The activated widget is specified by widget.
[signal]
void QDesignerFormWindowInterface::changed()
This signal is emitted whenever a form is changed.
[pure virtual slot]
void QDesignerFormWindowInterface::clearSelection(boolupdate = true)
Clears the current selection in the form window. If update is true, the emitSelectionChanged() function is called, emitting the selectionChanged() signal.
See also selectWidget().
[signal]
void QDesignerFormWindowInterface::featureChanged(QDesignerFormWindowInterface::Featurefeature)
This signal is emitted whenever a feature changes in the form. The new feature is specified by feature.
See also setFeatures().
[signal]
void QDesignerFormWindowInterface::fileNameChanged(const QString &fileName)
This signal is emitted whenever the file name of the form changes. The new file name is specified by fileName.
See also setFileName().
[signal]
void QDesignerFormWindowInterface::geometryChanged()
This signal is emitted whenever the form's geometry changes.
[signal]
void QDesignerFormWindowInterface::mainContainerChanged(QWidget *mainContainer)
This signal is emitted whenever the main container changes. The new container is specified by mainContainer.
See also setMainContainer().
[pure virtual slot]
void QDesignerFormWindowInterface::manageWidget(QWidget *widget)
Qt5 Virtual Slots
Instructs the form window to manage the specified widget.
See also isManaged(), unmanageWidget(), and widgetManaged().
[signal]
void QDesignerFormWindowInterface::objectRemoved(QObject *object)
This signal is emitted whenever an object (such as an action or a QButtonGroup) is removed from the form. The object that was removed is specified by object.
This function was introduced in Qt 4.5.
[signal]
void QDesignerFormWindowInterface::resourceFilesChanged()
This signal is emitted whenever the list of resource files used by the form changes.
See also resourceFiles().
[pure virtual slot]
void QDesignerFormWindowInterface::selectWidget(QWidget *widget, boolselect = true)
If select is true, the given widget is selected; otherwise the widget is deselected.
See also clearSelection() and selectionChanged().
[signal]
void QDesignerFormWindowInterface::selectionChanged()
This signal is emitted whenever the selection in the form changes.
See also selectWidget() and clearSelection().
[pure virtual slot]
bool QDesignerFormWindowInterface::setContents(const QString &contents)
Sets the contents of the form using data read from the specified contents string and returns whether the operation succeeded.
See also contents().
[pure virtual slot]
void QDesignerFormWindowInterface::setDirty(booldirty)
If dirty is true, the form window is marked as dirty, meaning that it is modified but not saved. If dirty is false, the form window is considered to be unmodified.
See also isDirty().
[pure virtual slot]
void QDesignerFormWindowInterface::setFeatures(QDesignerFormWindowInterface::Featurefeatures)
Enables the specified features for the form window.
See also features() and featureChanged().
[pure virtual slot]
void QDesignerFormWindowInterface::setFileName(const QString &fileName)
Sets the file name for the form to the given fileName.
See also fileName() and fileNameChanged().
[pure virtual slot]
void QDesignerFormWindowInterface::setGrid(const QPoint &grid)
Sets the grid size for the form window to the point specified by grid. In this function, the coordinates in the QPoint are used to specify the dimensions of a rectangle in the grid.
See also grid().
[pure virtual slot]
void QDesignerFormWindowInterface::unmanageWidget(QWidget *widget)
Instructs the form window not to manage the specified widget.
See also aboutToUnmanageWidget() and widgetUnmanaged().
[signal]
void QDesignerFormWindowInterface::widgetManaged(QWidget *widget)
This signal is emitted whenever a widget on the form becomes managed. The newly managed widget is specified by widget.
See also manageWidget().
[signal]
void QDesignerFormWindowInterface::widgetRemoved(QWidget *widget)
This signal is emitted whenever a widget is removed from the form. The widget that was removed is specified by widget.
[signal]
void QDesignerFormWindowInterface::widgetUnmanaged(QWidget *widget)
This signal is emitted whenever a widget on the form becomes unmanaged. The newly released widget is specified by widget.
See also unmanageWidget() and aboutToUnmanageWidget().
[virtual]
QDesignerFormWindowInterface::~QDesignerFormWindowInterface()
Destroys the form window interface.
[pure virtual]
QDir QDesignerFormWindowInterface::absoluteDir() const
Returns the absolute location of the directory containing the form shown in the form window.
QStringList QDesignerFormWindowInterface::activeResourceFilePaths() const
Returns the active resource (.qrc) file paths currently loaded in Qt Designer.
This function was introduced in Qt 5.0.
See also activateResourceFilePaths().
[pure virtual]
void QDesignerFormWindowInterface::addResourceFile(const QString &path)
Adds the resource file at the given path to those used by the form.
See also resourceFiles() and resourceFilesChanged().
[pure virtual]
QString QDesignerFormWindowInterface::author() const
Returns details of the author or creator of the form currently being displayed in the window.
See also setAuthor().
[pure virtual]
QStringList QDesignerFormWindowInterface::checkContents() const
Performs checks on the current form and returns a list of richtext warnings about potential issues (for example, top level spacers on unlaid-out forms).
IDE integrations can call this before handling starting a save operation.
This function was introduced in Qt 5.0.
[pure virtual]
QString QDesignerFormWindowInterface::comment() const
Returns comments about the form currently being displayed in the window.
See also setComment().
[pure virtual]
QString QDesignerFormWindowInterface::contents() const
Returns details of the contents of the form currently being displayed in the window.
See also setContents().
[virtual]
QDesignerFormEditorInterface *QDesignerFormWindowInterface::core() const
Returns a pointer to Qt Designer's current QDesignerFormEditorInterface object.
[pure virtual]
QDesignerFormWindowCursorInterface *QDesignerFormWindowInterface::cursor() const
Returns the cursor interface used by the form window.
[pure virtual]
void QDesignerFormWindowInterface::emitSelectionChanged()
Emits the selectionChanged() signal.
See also selectWidget() and clearSelection().
[pure virtual]
QString QDesignerFormWindowInterface::exportMacro() const
Returns the export macro associated with the form currently being displayed in the window. The export macro is used when the form is compiled to create a widget plugin.
See also setExportMacro() and Creating Custom Widgets for Qt Designer.
[pure virtual]
QDesignerFormWindowInterface::Feature QDesignerFormWindowInterface::features() const
Returns a combination of the features provided by the form window associated with the interface. The value returned can be tested against the Feature enum values to determine which features are supported by the window.
See also setFeatures() and hasFeature().
[pure virtual]
QString QDesignerFormWindowInterface::fileName() const
Returns the file name of the UI file that describes the form currently being shown.
See also setFileName().
[static]
QDesignerFormWindowInterface *QDesignerFormWindowInterface::findFormWindow(QWidget *widget)
Returns the form window interface for the given widget.
[static]
QDesignerFormWindowInterface *QDesignerFormWindowInterface::findFormWindow(QObject *object)
Returns the form window interface for the given object.
This function was introduced in Qt 4.4.
[pure virtual]
QWidget *QDesignerFormWindowInterface::formContainer() const
Returns the form the widget containing the main container widget.
This function was introduced in Qt 5.0.
[pure virtual]
QPoint QDesignerFormWindowInterface::grid() const
Returns the grid spacing used by the form window.
See also setGrid().
[pure virtual]
bool QDesignerFormWindowInterface::hasFeature(QDesignerFormWindowInterface::Featurefeature) const
Returns true if the form window offers the specified feature; otherwise returns false.
See also features().
[pure virtual]
QStringList QDesignerFormWindowInterface::includeHints() const
Returns a list of the header files that will be included in the form window's associated UI file.
Header files may be local, i.e. relative to the project's directory, 'mywidget.h'
, or global, i.e. part of Qt or the compilers standard libraries: <QtGui/QWidget>
.
See also setIncludeHints().
[pure virtual]
bool QDesignerFormWindowInterface::isDirty() const
Returns true if the form window is 'dirty' (modified but not saved); otherwise returns false.
See also setDirty().
[pure virtual]
bool QDesignerFormWindowInterface::isManaged(QWidget *widget) const
Returns true if the specified widget is managed by the form window; otherwise returns false.
See also manageWidget().
[pure virtual]
void QDesignerFormWindowInterface::layoutDefault(int *margin, int *spacing)
Fills in the default margin and spacing for the form's default layout in the margin and spacing variables specified.
See also setLayoutDefault().
[pure virtual]
void QDesignerFormWindowInterface::layoutFunction(QString *margin, QString *spacing)
Fills in the current margin and spacing for the form's layout in the margin and spacing variables specified.
See also setLayoutFunction().
[pure virtual]
QString QDesignerFormWindowInterface::pixmapFunction() const
Returns the name of the function used to load pixmaps into the form window.
See also setPixmapFunction().
[pure virtual]
void QDesignerFormWindowInterface::removeResourceFile(const QString &path)
Removes the resource file at the specified path from the list of those used by the form.
See also resourceFiles() and resourceFilesChanged().
[pure virtual]
QDesignerFormWindowInterface::ResourceFileSaveMode QDesignerFormWindowInterface::resourceFileSaveMode() const
Returns the resource file save mode behavior.
See also setResourceFileSaveMode().
[pure virtual]
QStringList QDesignerFormWindowInterface::resourceFiles() const
Returns a list of paths to resource files that are currently being used by the form window.
See also addResourceFile() and removeResourceFile().
[pure virtual]
void QDesignerFormWindowInterface::setAuthor(const QString &author)
Sets the details for the author or creator of the form to the author specified.
See also author().
[pure virtual]
void QDesignerFormWindowInterface::setComment(const QString &comment)
Sets the information about the form to the comment specified. This information should be a human-readable comment about the form.
See also comment().
[pure virtual]
bool QDesignerFormWindowInterface::setContents(QIODevice *device, QString *errorMessage = 0)
Sets the form's contents using data obtained from the given device and returns whether loading succeeded. If it fails, the error message is returned in errorMessage.
Data can be read from QFile objects or any other subclass of QIODevice.
See also contents().
[pure virtual]
void QDesignerFormWindowInterface::setExportMacro(const QString &exportMacro)
Sets the form window's export macro to exportMacro. The export macro is used when building a widget plugin to export the form's interface to other components.
See also exportMacro().
[pure virtual]
void QDesignerFormWindowInterface::setIncludeHints(const QStringList &includeHints)
Sets the header files that will be included in the form window's associated UI file to the specified includeHints.
Header files may be local, i.e. relative to the project's directory, 'mywidget.h'
, or global, i.e. part of Qt or the compilers standard libraries: <QtGui/QWidget>
.
See also includeHints().
[pure virtual]
void QDesignerFormWindowInterface::setLayoutDefault(intmargin, intspacing)
Sets the default margin and spacing for the form's layout.
See also layoutDefault().
[pure virtual]
void QDesignerFormWindowInterface::setLayoutFunction(const QString &margin, const QString &spacing)
Sets the margin and spacing for the form's layout.
The default layout properties will be replaced by the corresponding layout functions when uic
generates code for the form, that is, if the functions are specified. This is useful when different environments requires different layouts for the same form.
See also layoutFunction().
[pure virtual]
void QDesignerFormWindowInterface::setMainContainer(QWidget *mainContainer)
Sets the main container widget on the form to the specified mainContainer.
See also mainContainerChanged().
[pure virtual]
void QDesignerFormWindowInterface::setPixmapFunction(const QString &pixmapFunction)
Sets the function used to load pixmaps into the form window to the given pixmapFunction.
See also pixmapFunction().
[pure virtual]
void QDesignerFormWindowInterface::setResourceFileSaveMode(QDesignerFormWindowInterface::ResourceFileSaveModebehavior)
Sets the resource file save mode behavior.
See also resourceFileSaveMode().
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
EnArBgDeElEsFaFiFrHiHuItJaKnKoMsNlPlPtRuSqThTrUkZh
This page was used to describe the new signal and slot syntax during its development. The feature is now released with Qt 5.
- Differences between String-Based and Functor-Based Connections (Official documentation)
- Introduction (Woboq blog)
- Implementation Details (Woboq blog)
Note: This is in addition to the old string-based syntax which remains valid.
- 1Connecting in Qt 5
- 2Disconnecting in Qt 5
- 4Error reporting
- 5Open questions
Connecting in Qt 5
There are several ways to connect a signal in Qt 5.
Old syntax
Qt 5 continues to support the old string-based syntax for connecting signals and slots defined in a QObject or any class that inherits from QObject (including QWidget)
New: connecting to QObject member
Here's Qt 5's new way to connect two QObjects and pass non-string objects:
Pros
- Compile time check of the existence of the signals and slot, of the types, or if the Q_OBJECT is missing.
- Argument can be by typedefs or with different namespace specifier, and it works.
- Possibility to automatically cast the types if there is implicit conversion (e.g. from QString to QVariant)
- It is possible to connect to any member function of QObject, not only slots.
Cons
- More complicated syntax? (you need to specify the type of your object)
- Very complicated syntax in cases of overloads? (see below)
- Default arguments in slot is not supported anymore.
New: connecting to simple function
The new syntax can even connect to functions, not just QObjects:
Pros
- Can be used with std::bind:
- Can be used with C++11 lambda expressions:
Cons
- There is no automatic disconnection when the 'receiver' is destroyed because it's a functor with no QObject. However, since 5.2 there is an overload which adds a 'context object'. When that object is destroyed, the connection is broken (the context is also used for the thread affinity: the lambda will be called in the thread of the event loop of the object used as context).
Disconnecting in Qt 5
As you might expect, there are some changes in how connections can be terminated in Qt 5, too.
Old way
You can disconnect in the old way (using SIGNAL, SLOT) but only if
- You connected using the old way, or
- If you want to disconnect all the slots from a given signal using wild card character
Symetric to the function pointer one
Only works if you connected with the symmetric call, with function pointers (Or you can also use 0 for wild card)In particular, does not work with static function, functors or lambda functions.
New way using QMetaObject::Connection
Works in all cases, including lambda functions or functors.
Asynchronous made easier
With C++11 it is possible to keep the code inline
Here's a QDialog without re-entering the eventloop, and keeping the code where it belongs:
Another example using QHttpServer : http://pastebin.com/pfbTMqUm
Error reporting
Tested with GCC.
Fortunately, IDEs like Qt Creator simplifies the function naming
Missing Q_OBJECT in class definition
Type mismatch
Open questions
Default arguments in slot
If you have code like this:
The old method allows you to connect that slot to a signal that does not have arguments.But I cannot know with template code if a function has default arguments or not.So this feature is disabled.
There was an implementation that falls back to the old method if there are more arguments in the slot than in the signal.This however is quite inconsistent, since the old method does not perform type-checking or type conversion. It was removed from the patch that has been merged.
Overload
As you might see in the example above, connecting to QAbstractSocket::error is not really beautiful since error has an overload, and taking the address of an overloaded function requires explicit casting, e.g. a connection that previously was made as follows:
connect(mySpinBox, SIGNAL(valueChanged(int)), mySlider, SLOT(setValue(int));
cannot be simply converted to:
...because QSpinBox has two signals named valueChanged() with different arguments. Instead, the new code needs to be:
Unfortunately, using an explicit cast here allows several types of errors to slip past the compiler. Adding a temporary variable assignment preserves these compile-time checks:
Some macro could help (with C++11 or typeof extensions). A template based solution was introduced in Qt 5.7: qOverload
The best thing is probably to recommend not to overload signals or slots …
… but we have been adding overloads in past minor releases of Qt because taking the address of a function was not a use case we support. But now this would be impossible without breaking the source compatibility.
Disconnect
Should QMetaObject::Connection have a disconnect() function?
The other problem is that there is no automatic disconnection for some object in the closure if we use the syntax that takes a closure.One could add a list of objects in the disconnection, or a new function like QMetaObject::Connection::require
Callbacks
Function such as QHostInfo::lookupHost or QTimer::singleShot or QFileDialog::open take a QObject receiver and char* slot.This does not work for the new method.If one wants to do callback C++ way, one should use std::functionBut we cannot use STL types in our ABI, so a QFunction should be done to copy std::function.In any case, this is irrelevant for QObject connections.