Home · All Classes · Modules |
Sep 11, 2018 Nonetheless, the components need to communicate with each other and you need some kind of callback-mechanism. And this, ladies and gentlemen, this is where Qt's signals and slots comes to the rescue. This is the third post in the series 'Crash course in Qt for C developers' covering the signals and slots mechanism. The other topics are listed. QDialogs can provide a return value, and they can have default buttons. QDialogs can also have a QSizeGrip in their lower-right corner, using setSizeGripEnabled. Note that QDialog (and any other widget that has type Qt.Dialog ) uses the parent widget slightly differently from other classes in Qt. How Qt Signals and Slots Work - Part 3 - Queued and Inter Thread Connections. Activate to prepare a Qt::QueuedConnection slot call. The code showed here has been slightly simplified and commented: static void queuedactivate(QObject.sender. (void.)); // Ignore the return value as it makes no sense in a queued connection types 0. Sending Python values with signals and slots. On the #pyqt channel on Freenode, Khertan asked about sending Python values via Qt's signals and slots mechanism. The following example uses the PyQtPyObject value declaration with an old-style signal-slot connection, and again when the signal is emitted, to communicate a Python dictionary. Qt events signals and slots properties memory management. The QObject QObject is the base class to most Qt classes. A slot is defined in one of the slots sections A slot can return values, but not through connections Any number of signals can be connected to a slot.
The QDialog class is the base class of dialog windows. More...
Inherits QWidget.
Inherited by QAbstractPrintDialog, QColorDialog, QErrorMessage, QFileDialog, QFontDialog, QInputDialog, QMessageBox, QPageSetupDialog, QPrintPreviewDialog, QProgressDialog and QWizard.
Types
Methods
- __init__ (self, QWidget parent = None, Qt.WindowFlags flags = 0)
- closeEvent (self, QCloseEvent)
- done (self, int)
- int exec_ (self)
- bool isSizeGripEnabled (self)
- QSize minimumSizeHint (self)
- Qt.Orientation orientation (self)
- resizeEvent (self, QResizeEvent)
- setExtension (self, QWidget extension)
- setOrientation (self, Qt.Orientation orientation)
- setSizeGripEnabled (self, bool)
- showEvent (self, QShowEvent)
- QSize sizeHint (self)
Qt Signals
- void finished (int)
Detailed Description
The QDialog class is the base class of dialog windows.
A dialog window is a top-level window mostly used for short-termtasks and brief communications with the user. QDialogs may be modalor modeless. QDialogs can provide a returnvalue, and they can have defaultbuttons. QDialogs can also have a QSizeGrip in their lower-right corner, usingsetSizeGripEnabled().
Note that QDialog (and any other widget that has typeQt.Dialog) uses the parent widget slightly differentlyfrom other classes in Qt. A dialog is always a top-level widget,but if it has a parent, its default location is centered on top ofthe parent's top-level widget (if it is not top-level itself). Itwill also share the parent's taskbar entry.
Use the overload of the QWidget.setParent() function tochange the ownership of a QDialog widget. This function allows youto explicitly set the window flags of the reparented widget; usingthe overloaded function will clear the window flags specifying thewindow-system properties for the widget (in particular it willreset the Qt.Dialogflag).
Modal Dialogs
A modal dialog is a dialog that blocks input to othervisible windows in the same application. Dialogs that are used torequest a file name from the user or that are used to setapplication preferences are usually modal. Dialogs can be application modal (the default)or window modal.
When an application modal dialog is opened, the user must finishinteracting with the dialog and close it before they can access anyother window in the application. Window modal dialogs only blockaccess to the window associated with the dialog, allowing the userto continue to use other windows in an application.
The most common way to display a modal dialog is to call itsexec_() function. When the usercloses the dialog, exec_() willprovide a useful return value. Typically, toget the dialog to close and return the appropriate value, weconnect a default button, e.g. OK, to the accept() slot and a Cancel buttonto the reject() slot.Alternatively you can call the done() slot with Accepted orRejected.
An alternative is to call setModal(true) or setWindowModality(), thenshow(). Unlike exec_(), show() returns control to the callerimmediately. Calling setModal(true) is especially useful forprogress dialogs, where the user must have the ability to interactwith the dialog, e.g. to cancel a long running operation. If youuse show() and setModal(true)together to perform a long operation, you must call QApplication.processEvents()periodically during processing to enable the user to interact withthe dialog. (See QProgressDialog.)
Modeless Dialogs
A modeless dialog is a dialog that operates independentlyof other windows in the same application. Find and replace dialogsin word-processors are often modeless to allow the user to interactwith both the application's main window and with the dialog.
Modeless dialogs are displayed using show(), which returns control to the callerimmediately.
If you invoke the show()function after hiding a dialog, the dialog will be displayed in itsoriginal position. This is because the window manager decides theposition for windows that have not been explicitly placed by theprogrammer. To preserve the position of a dialog that has beenmoved by the user, save its position in your closeEvent() handler and then movethe dialog to that position, before showing it again.
Default Button
A dialog's default button is the button that's pressedwhen the user presses Enter (Return). This button is used tosignify that the user accepts the dialog's settings and wants toclose the dialog. Use QPushButton.setDefault(),QPushButton.isDefault() andQPushButton.autoDefault()to set and control the dialog's default button.
Escape Key
If the user presses the Esc key in a dialog, QDialog.reject() will be called. Thiswill cause the window to close: The close event cannot be ignored.
Extensibility
Extensibility is the ability to show the dialog in two ways: apartial dialog that shows the most commonly used options, and afull dialog that shows all the options. Typically an extensibledialog will initially appear as a partial dialog, but with aMore toggle button. If the user presses the Morebutton down, the dialog is expanded. The Extension Example shows how to achieveextensible dialogs using Qt.
Return Value (Modal Dialogs)
Modal dialogs are often used in situations where a return valueis required, e.g. to indicate whether the user pressed OK orCancel. A dialog can be closed by calling the accept() or the reject() slots, and exec_() will return Accepted orRejected as appropriate. The exec_() call returns the result of thedialog. The result is also available from result() if the dialog has not beendestroyed.
In order to modify your dialog's close behavior, you canreimplement the functions accept(), reject() or done(). The closeEvent() function should only bereimplemented to preserve the dialog's position or to override thestandard close or reject behavior.
Code Examples
A modal dialog:
A modeless dialog:
Type Documentation
Constant | Value |
---|---|
QDialog.Accepted | 1 |
QDialog.Rejected | 0 |
Method Documentation
QDialog.__init__ (self, QWidgetparent = None, Qt.WindowFlagsflags = 0)
The parent argument, if not None, causes self to be owned by Qt instead of PyQt.
Constructs a dialog with parent parent.
A dialog is always a top-level widget, but if it has a parent,its default location is centered on top of the parent. It will alsoshare the parent's taskbar entry.
The widget flags f are passed on to the QWidget constructor. If, for example, you don'twant a What's This button in the title bar of the dialog, passQt.WindowTitleHint |Qt.WindowSystemMenuHint inf.
See alsoQWidget.setWindowFlags().
QDialog.accept (self)
See alsoreject() anddone().
QDialog.closeEvent (self, QCloseEvent)
Reimplemented from QWidget.closeEvent().
QDialog.contextMenuEvent (self, QContextMenuEvent)
Reimplemented from QWidget.contextMenuEvent().
QDialog.done (self, int)
Closes the dialog and sets its result code to r. If thisdialog is shown with exec_(), done()causes the local event loop to finish, and exec_() to return r.
As with QWidget.close(),done() deletes the dialog if the Qt.WA_DeleteOnClose flag isset. If the dialog is the application's main widget, theapplication terminates. If the dialog is the last window closed,the QApplication.lastWindowClosed()signal is emitted.
See alsoaccept(),reject(), QApplication.activeWindow(),and QApplication.quit().
bool QDialog.eventFilter (self, QObject, QEvent)
Reimplemented from QObject.eventFilter().
int QDialog.exec_ (self)
Shows the dialog as a modaldialog, blocking until the user closes it. The function returnsa DialogCode result.
If the dialog is application modal, users cannotinteract with any other window in the same application until theyclose the dialog. If the dialog is window modal, only interactionwith the parent window is blocked while the dialog is open. Bydefault, the dialog is application modal.
See alsoopen(), show(), result(), and setWindowModality().
QWidget QDialog.extension (self)
bool QDialog.isSizeGripEnabled (self)
QDialog.keyPressEvent (self, QKeyEvent)
Reimplemented from QWidget.keyPressEvent().
QSize QDialog.minimumSizeHint (self)
Reimplemented from QWidget.minimumSizeHint().
QDialog.open (self)
Shows the dialog as a window modal dialog, returningimmediately.
This function was introduced in Qt 4.5.
See alsoexec_(), show(), result(), and setWindowModality().
Qt.Orientation QDialog.orientation (self)
QDialog.reject (self)
See alsoaccept() anddone().
QDialog.resizeEvent (self, QResizeEvent)
Reimplemented from QWidget.resizeEvent().
int QDialog.result (self)
Note: When used from QMessageBox instance the result code type isQMessageBox.StandardButton
Do not call this function if the dialog was constructed with theQt.WA_DeleteOnCloseattribute.
See alsosetResult().
QDialog.setExtension (self, QWidgetextension)
The extension argument has it's ownership transferred to Qt.
QDialog.setModal (self, bool modal)
QDialog.setOrientation (self, Qt.Orientationorientation)
QDialog.setResult (self, int r)
Note: We recommend that you use one of the values definedby QDialog.DialogCode.
See alsoresult().
QDialog.setSizeGripEnabled (self, bool)
QDialog.setVisible (self, bool visible)
Reimplemented from QWidget.setVisible().
QDialog.showEvent (self, QShowEvent)
Reimplemented from QWidget.showEvent().
QDialog.showExtension (self, bool)
QSize QDialog.sizeHint (self)
Reimplemented from QWidget.sizeHint().
Qt Signal Documentation
void accepted ()
This signal is emitted when the dialog has been accepted eitherby the user or by calling accept() or done() with the QDialog.Accepted argument.
Note that this signal is not emitted when hiding thedialog with hide() orsetVisible(false). This includes deleting the dialog while it isvisible.
This function was introduced in Qt 4.1.
See alsofinished()and rejected().
void finished (int)
This signal is emitted when the dialog's result code hasbeen set, either by the user or by calling done(), accept(), or reject().
Note that this signal is not emitted when hiding thedialog with hide() orsetVisible(false). This includes deleting the dialog while it isvisible.
This function was introduced in Qt 4.1.
See alsoaccepted()and rejected().
void rejected ()
This signal is emitted when the dialog has been rejected eitherby the user or by calling reject() or done() with the QDialog.Rejected argument.
Qt Signal Slot Return Value
Note that this signal is not emitted when hiding thedialog with hide() orsetVisible(false). This includes deleting the dialog while it isvisible.
This function was introduced in Qt 4.1.
See alsofinished()and accepted().
PyQt 4.11.4 for X11 | Copyright © Riverbank Computing Ltd and The Qt Company 2015 | Qt 4.8.7 |
All QML object types are QObject-derived types, whether they are internally implemented by the engine or defined by third-party sources. This means the QML engine can use the Qt Meta Object System to dynamically instantiate any QML object type and inspect the created objects.
This is useful for creating QML objects from C++ code, whether to display a QML object that can be visually rendered, or to integrate non-visual QML object data into a C++ application. Once a QML object is created, it can be inspected from C++ in order to read and write to properties, invoke methods and receive signal notifications.
Loading QML Objects from C++
A QML document can be loaded with QQmlComponent or QQuickView. QQmlComponent loads a QML document as a C++ object that can then be modified from C++ code. QQuickView also does this, but as QQuickView is a QWindow-derived class, the loaded object will also be rendered into a visual display; QQuickView is generally used to integrate a displayable QML object into an application's user interface.
For example, suppose there is a
MyItem.qml
file that looks like this:This QML document can be loaded with QQmlComponent or QQuickView with the following C++ code. Using a QQmlComponent requires calling QQmlComponent::create() to create a new instance of the component, while a QQuickView automatically creates an instance of the component, which is accessible via QQuickView::rootObject():
This
object
is the instance of the MyItem.qml
component that has been created. You can now modify the item's properties using QObject::setProperty() or QQmlProperty:Alternatively, you can cast the object to its actual type and call methods with compile-time safety. In this case the base object of
MyItem.qml
is an Item, which is defined by the QQuickItem class:You can also connect to any signals or call methods defined in the component using QMetaObject::invokeMethod() and QObject::connect(). See Invoking QML Methods and Connecting to QML Signals below for further details.
Accessing Loaded QML Objects by Object Name
QML components are essentially object trees with children that have siblings and their own children. Child objects of QML components can be located using the QObject::objectName property with QObject::findChild(). For example, if the root item in
MyItem.qml
had a child Rectangle item:The child could be located like this:
Note that an object may have multiple children with the same
objectName
. For example, ListView creates multiple instances of its delegate, so if its delegate is declared with a particular objectName, the ListView will have multiple children with the same objectName
. In this case, QObject::findChildren() can be used to find all children with a matching objectName
.Warning: While it is possible to use C++ to access and manipulate QML objects deep into the object tree, we recommend that you do not take this approach outside of application testing and prototyping. One strength of QML and C++ integration is the ability to implement the QML user interface separately from the C++ logic and dataset backend, and this strategy breaks if the C++ side reaches deep into the QML components to manipulate them directly. This would make it difficult to, for example, swap a QML view component for another view, if the new component was missing a required
objectName
. It is better for the C++ implementation to know as little as possible about the QML user interface implementation and the composition of the QML object tree.Accessing Members of a QML Object Type from C++
Properties
Any properties declared in a QML object are automatically accessible from C++. Given a QML item like this:
The value of the
someNumber
property can be set and read using QQmlProperty, or QObject::setProperty() and QObject::property():You should always use QObject::setProperty(), QQmlProperty or QMetaProperty::write() to change a QML property value, to ensure the QML engine is made aware of the property change. For example, say you have a custom type
PushButton
with a buttonText
property that internally reflects the value of a m_buttonText
member variable. Modifying the member variable directly like this is not a good idea:Since the value is changed directly, this bypasses Qt's meta-object system and the QML engine is not made aware of the property change. This means property bindings to
buttonText
would not be updated, and any onButtonTextChanged
handlers would not be called.Invoking QML Methods
All QML methods are exposed to the meta-object system and can be called from C++ using QMetaObject::invokeMethod(). Method parameters and return values passed from QML are always translated into QVariant values in C++.
Here is a C++ application that calls a QML method using QMetaObject::invokeMethod():
QML |
C++ |
Notice the Q_RETURN_ARG() and Q_ARG() arguments for QMetaObject::invokeMethod() must be specified as QVariant types, as this is the generic data type used for QML method parameters and return values.
Connecting to QML Signals
All QML signals are automatically available to C++, and can be connected to using QObject::connect() like any ordinary Qt C++ signal. In return, any C++ signal can be received by a QML object using signal handlers.
Here is a QML component with a signal named
qmlSignal
that is emitted with a string-type parameter. This signal is connected to a C++ object's slot using QObject::connect(), so that the cppSlot()
method is called whenever the qmlSignal
is emitted:Signal And Slot In Qt
When a QML object type is used as a signal parameter, the parameter should use var as the type, and the value should be received in C++ using the QVariant type:
Qt Slot Get Sender
© 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.