[vbox-dev] [PATCH] Use QMetaObject::invokeMethod instead of QTimer::singleShot(0, ...)

Alex Henrie alexhenrie24 at gmail.com
Tue Mar 31 00:39:08 GMT 2015


QTimer::singleShot(0, ...) is just a complicated wrapper around
QMetaObject::invokeMethod(..., Qt::QueuedConnection). It does lots of
parameter checks, string parsing, and dynamic memory allocation before
finally calling QMetaObject::invokeMethod:

(Qt4) https://qt.gitorious.org/qt/qt/source/efce5d8361af41bf60dd16ce5aec65fe2fd84f88:src/corelib/kernel/qtimer.cpp#L349
(Qt5) https://qt.gitorious.org/qt/qtbase/source/2c1d597b653f16dc80a87d637c94213120f32d90:src/corelib/kernel/qtimer.cpp#L366
(Qt5) https://qt.gitorious.org/qt/qtbase/source/2c1d597b653f16dc80a87d637c94213120f32d90:src/corelib/kernel/qtimer.cpp#L388

Using QMetaObject::invokeMethod directly makes the code easier to
understand and avoids unnecessary overhead.

This patch is provided under the MIT license.

Index: src/VBox/Frontends/VirtualBox/src/globals/VBoxGlobal.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/globals/VBoxGlobal.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/globals/VBoxGlobal.cpp	(working copy)
@@ -3839,7 +3839,7 @@
     if (!list.isEmpty())
     {
         m_ArgUrlList = list;
-        QTimer::singleShot(0, &vboxGlobal().selectorWnd(), SLOT(sltOpenUrls()));
+        QMetaObject::invokeMethod(&vboxGlobal().selectorWnd(), "sltOpenUrls", Qt::QueuedConnection);
     }
     return fResult;
 }
Index: src/VBox/Frontends/VirtualBox/src/net/UIUpdateManager.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/net/UIUpdateManager.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/net/UIUpdateManager.cpp	(working copy)
@@ -447,7 +447,7 @@
 #ifdef VBOX_WITH_UPDATE_REQUEST
     /* Ask updater to check for the first time: */
     if (gEDataManager->applicationUpdateEnabled() && !vboxGlobal().isVMConsoleProcess())
-        QTimer::singleShot(0, this, SLOT(sltCheckIfUpdateIsNecessary()));
+        QMetaObject::invokeMethod(this, "sltCheckIfUpdateIsNecessary", Qt::QueuedConnection);
 #endif /* VBOX_WITH_UPDATE_REQUEST */
 }
 
Index: src/VBox/Frontends/VirtualBox/src/runtime/UIDnDMIMEData.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/UIDnDMIMEData.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/UIDnDMIMEData.cpp	(working copy)
@@ -82,7 +82,7 @@
      *       events will come through anymore. At this point DoDragDrop is modal
      *       and will take care of all the input handling. */
 #ifndef RT_OS_WINDOWS
-    QTimer::singleShot(0, this, SLOT(sltInstallEventFilter()));
+    QMetaObject::invokeMethod(this, "sltInstallEventFilter", Qt::QueuedConnection);
 #endif
 
 #ifdef DEBUG
Index: src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp	(working copy)
@@ -1526,7 +1526,7 @@
         pWidget->close();
         if (!pWidget->isHidden())
             pWidget->hide();
-        QTimer::singleShot(0, this, SLOT(sltClose()));
+        QMetaObject::invokeMethod(this, "sltClose", Qt::QueuedConnection);
         return;
     }
 
Index: src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp	(working copy)
@@ -1299,7 +1299,7 @@
                         QApplication::focusWidget()->clearFocus();
                         qApp->processEvents();
                     }
-                    QTimer::singleShot(0, this, SLOT(setFocus()));
+                    QMetaObject::invokeMethod(this, "setFocus", Qt::QueuedConnection);
                 }
                 break;
             }
Index: src/VBox/Frontends/VirtualBox/src/runtime/fullscreen/UIKeyboardHandlerFullscreen.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/fullscreen/UIKeyboardHandlerFullscreen.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/fullscreen/UIKeyboardHandlerFullscreen.cpp	(working copy)
@@ -62,7 +62,7 @@
                 if (isHostKeyPressed() && pKeyEvent->key() == gShortcutPool->shortcut(GUI_Input_MachineShortcuts, QString("PopupMenu")).sequence())
                 {
                     /* Post request to show popup-menu: */
-                    QTimer::singleShot(0, m_pMachineLogic, SLOT(sltInvokePopupMenu()));
+                    QMetaObject::invokeMethod(m_pMachineLogic, "sltInvokePopupMenu", Qt::QueuedConnection);
                     /* Filter-out this event: */
                     return true;
                 }
Index: src/VBox/Frontends/VirtualBox/src/runtime/fullscreen/UIMachineLogicFullscreen.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/fullscreen/UIMachineLogicFullscreen.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/fullscreen/UIMachineLogicFullscreen.cpp	(working copy)
@@ -384,7 +384,7 @@
     if (m_pPopupMenu && !m_pPopupMenu->isEmpty())
     {
         m_pPopupMenu->popup(activeMachineWindow()->geometry().center());
-        QTimer::singleShot(0, m_pPopupMenu, SLOT(sltHighlightFirstAction()));
+        QMetaObject::invokeMethod(m_pPopupMenu, "sltHighlightFirstAction", Qt::QueuedConnection);
     }
 }
 
Index: src/VBox/Frontends/VirtualBox/src/runtime/fullscreen/UIMachineViewFullscreen.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/fullscreen/UIMachineViewFullscreen.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/fullscreen/UIMachineViewFullscreen.cpp	(working copy)
@@ -89,7 +89,7 @@
                 setMaxGuestSize();
                 /* And resize guest to that size: */
                 if (m_bIsGuestAutoresizeEnabled && uisession()->isGuestSupportsGraphics())
-                    QTimer::singleShot(0, this, SLOT(sltPerformGuestResize()));
+                    QMetaObject::invokeMethod(this, "sltPerformGuestResize", Qt::QueuedConnection);
                 break;
             }
             default:
Index: src/VBox/Frontends/VirtualBox/src/runtime/normal/UIKeyboardHandlerNormal.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/normal/UIKeyboardHandlerNormal.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/normal/UIKeyboardHandlerNormal.cpp	(working copy)
@@ -100,7 +100,7 @@
                         else
                         {
                             /* Post request to show popup-menu: */
-                            QTimer::singleShot(0, m_pMachineLogic, SLOT(sltInvokePopupMenu()));
+                            QMetaObject::invokeMethod(m_pMachineLogic, "sltInvokePopupMenu", Qt::QueuedConnection);
                         }
                         /* Filter-out this event: */
                         return true;
Index: src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineLogicNormal.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineLogicNormal.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineLogicNormal.cpp	(working copy)
@@ -102,7 +102,7 @@
     if (m_pPopupMenu && !m_pPopupMenu->isEmpty())
     {
         m_pPopupMenu->popup(activeMachineWindow()->geometry().center());
-        QTimer::singleShot(0, m_pPopupMenu, SLOT(sltHighlightFirstAction()));
+        QMetaObject::invokeMethod(m_pPopupMenu, "sltHighlightFirstAction", Qt::QueuedConnection);
     }
 }
 #endif /* RT_OS_DARWIN */
Index: src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineWindowNormal.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineWindowNormal.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineWindowNormal.cpp	(working copy)
@@ -383,7 +383,7 @@
 
         /* Normalize to the optimal size: */
 #ifdef Q_WS_X11
-        QTimer::singleShot(0, this, SLOT(sltNormalizeGeometry()));
+        QMetaObject::invokeMethod(this, "sltNormalizeGeometry", Qt::QueuedConnection);
 #else /* !Q_WS_X11 */
         normalizeGeometry(true /* adjust position */);
 #endif /* !Q_WS_X11 */
Index: src/VBox/Frontends/VirtualBox/src/runtime/scale/UIKeyboardHandlerScale.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/scale/UIKeyboardHandlerScale.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/scale/UIKeyboardHandlerScale.cpp	(working copy)
@@ -69,7 +69,7 @@
                 if (isHostKeyPressed() && pKeyEvent->key() == gShortcutPool->shortcut(GUI_Input_MachineShortcuts, QString("PopupMenu")).sequence())
                 {
                     /* Post request to show popup-menu: */
-                    QTimer::singleShot(0, m_pMachineLogic, SLOT(sltInvokePopupMenu()));
+                    QMetaObject::invokeMethod(m_pMachineLogic, "sltInvokePopupMenu", Qt::QueuedConnection);
                     /* Filter-out this event: */
                     return true;
                 }
Index: src/VBox/Frontends/VirtualBox/src/runtime/scale/UIMachineLogicScale.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/scale/UIMachineLogicScale.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/scale/UIMachineLogicScale.cpp	(working copy)
@@ -69,7 +69,7 @@
     if (m_pPopupMenu && !m_pPopupMenu->isEmpty())
     {
         m_pPopupMenu->popup(activeMachineWindow()->geometry().center());
-        QTimer::singleShot(0, m_pPopupMenu, SLOT(sltHighlightFirstAction()));
+        QMetaObject::invokeMethod(m_pPopupMenu, "sltHighlightFirstAction", Qt::QueuedConnection);
     }
 }
 #endif /* !Q_WS_MAC */
Index: src/VBox/Frontends/VirtualBox/src/runtime/seamless/UIKeyboardHandlerSeamless.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/seamless/UIKeyboardHandlerSeamless.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/seamless/UIKeyboardHandlerSeamless.cpp	(working copy)
@@ -69,7 +69,7 @@
                 if (isHostKeyPressed() && pKeyEvent->key() == gShortcutPool->shortcut(GUI_Input_MachineShortcuts, QString("PopupMenu")).sequence())
                 {
                     /* Post request to show popup-menu: */
-                    QTimer::singleShot(0, m_pMachineLogic, SLOT(sltInvokePopupMenu()));
+                    QMetaObject::invokeMethod(m_pMachineLogic, "sltInvokePopupMenu", Qt::QueuedConnection);
                     /* Filter-out this event: */
                     return true;
                 }
Index: src/VBox/Frontends/VirtualBox/src/runtime/seamless/UIMachineLogicSeamless.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/seamless/UIMachineLogicSeamless.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/seamless/UIMachineLogicSeamless.cpp	(working copy)
@@ -167,7 +167,7 @@
     if (m_pPopupMenu && !m_pPopupMenu->isEmpty())
     {
         m_pPopupMenu->popup(activeMachineWindow()->geometry().center());
-        QTimer::singleShot(0, m_pPopupMenu, SLOT(sltHighlightFirstAction()));
+        QMetaObject::invokeMethod(m_pPopupMenu, "sltHighlightFirstAction", Qt::QueuedConnection);
     }
 }
 #endif /* !Q_WS_MAC */
Index: src/VBox/Frontends/VirtualBox/src/runtime/seamless/UIMachineViewSeamless.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/runtime/seamless/UIMachineViewSeamless.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/runtime/seamless/UIMachineViewSeamless.cpp	(working copy)
@@ -104,7 +104,7 @@
                 setMaxGuestSize();
                 /* And resize guest to that size: */
                 if (uisession()->isGuestSupportsGraphics())
-                    QTimer::singleShot(0, this, SLOT(sltPerformGuestResize()));
+                    QMetaObject::invokeMethod(this, "sltPerformGuestResize", Qt::QueuedConnection);
                 break;
             }
             default:
Index: src/VBox/Frontends/VirtualBox/src/selector/UISelectorWindow.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/selector/UISelectorWindow.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/selector/UISelectorWindow.cpp	(working copy)
@@ -1062,7 +1062,7 @@
 {
     /* Make sure user warned about inaccessible medium(s)
      * even if enumeration had finished before selector window shown: */
-    QTimer::singleShot(0, this, SLOT(sltHandleMediumEnumerationFinish()));
+    QMetaObject::invokeMethod(this, "sltHandleMediumEnumerationFinish", Qt::QueuedConnection);
 }
 
 #ifdef Q_WS_MAC
Index: src/VBox/Frontends/VirtualBox/src/selector/UIVMDesktop.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/selector/UIVMDesktop.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/selector/UIVMDesktop.cpp	(working copy)
@@ -258,7 +258,7 @@
     /* Cocoa stuff should be async...
      * Do not ask me why but otherwise
      * it conflicts with native handlers. */
-    QTimer::singleShot(0, this, SLOT(sltInit()));
+    QMetaObject::invokeMethod(this, "sltInit", Qt::QueuedConnection);
 #else /* !Q_WS_MAC */
     sltInit();
 #endif /* !Q_WS_MAC */
Index: src/VBox/Frontends/VirtualBox/src/settings/machine/UIMachineSettingsSF.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/settings/machine/UIMachineSettingsSF.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/settings/machine/UIMachineSettingsSF.cpp	(working copy)
@@ -609,7 +609,7 @@
     connect (mTwFolders->header(), SIGNAL (sectionResized (int, int, int)), this, SLOT (adjustFields()));
 
     /* Adjusting size after all pending show events are processed. */
-    QTimer::singleShot (0, this, SLOT (adjustList()));
+    QMetaObject::invokeMethod (this, "adjustList", Qt::QueuedConnection);
 }
 
 SFTreeViewItem* UIMachineSettingsSF::root(UISharedFolderType sharedFolderType)
Index: src/VBox/Frontends/VirtualBox/src/widgets/VBoxFilePathSelectorWidget.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/widgets/VBoxFilePathSelectorWidget.cpp	(revision 54999)
+++ src/VBox/Frontends/VirtualBox/src/widgets/VBoxFilePathSelectorWidget.cpp	(working copy)
@@ -275,7 +275,7 @@
 bool VBoxFilePathSelectorWidget::eventFilter (QObject *aObj, QEvent *aEv)
 {
     if (mIsMouseAwaited && (aEv->type() == QEvent::MouseButtonPress))
-        QTimer::singleShot (0, this, SLOT (refreshText()));
+        QMetaObject::invokeMethod (this, "refreshText", Qt::QueuedConnection);
 
     return QIWithRetranslateUI<QComboBox>::eventFilter (aObj, aEv);
 }




More information about the vbox-dev mailing list