Skip to content

frontend: Use qobject_cast when casting QObjects#11784

Open
cg2121 wants to merge 1 commit intoobsproject:masterfrom
cg2121:replace-dynamic-cast
Open

frontend: Use qobject_cast when casting QObjects#11784
cg2121 wants to merge 1 commit intoobsproject:masterfrom
cg2121:replace-dynamic-cast

Conversation

@cg2121
Copy link
Copy Markdown
Contributor

@cg2121 cg2121 commented Jan 28, 2025

Description

When casting between QObjects, qobject_cast should always be used. This increases performance as there is no RTTI (Run Time Type Information) with qobject_cast, like there is with dynamic_cast.

Using reinterpret_cast is bad practice, as you should use it only in very specific cases.

Motivation and Context

Better code

How Has This Been Tested?

compiled and ran OBS

Types of changes

  • Bug fix (non-breaking change which fixes an issue) -->
  • Performance enhancement (non-breaking change which improves efficiency)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

@cg2121 cg2121 force-pushed the replace-dynamic-cast branch from eb3f82d to 5582ea1 Compare January 28, 2025 11:49
@cg2121 cg2121 changed the title frontend: Use qobject_cast instead of dynamic_cast frontend: Use qobject_cast when casting QObjects Jan 28, 2025
@cg2121 cg2121 added Bug Fix Non-breaking change which fixes an issue UI/UX Anything to do with changes or additions to UI/UX elements. labels Jan 28, 2025
@cg2121 cg2121 marked this pull request as draft January 28, 2025 12:50
@cg2121
Copy link
Copy Markdown
Contributor Author

cg2121 commented Jan 28, 2025

Converting to draft, as #11785 needs to be merged first.

@cg2121 cg2121 marked this pull request as ready for review May 2, 2025 05:39
@cg2121 cg2121 force-pushed the replace-dynamic-cast branch from 5582ea1 to 7808d32 Compare May 2, 2025 05:41
@cg2121 cg2121 force-pushed the replace-dynamic-cast branch from 7808d32 to a9342f8 Compare May 5, 2025 18:34
@cg2121
Copy link
Copy Markdown
Contributor Author

cg2121 commented May 5, 2025

This PR now builds correctly

@cg2121 cg2121 requested a review from PatTheMav May 5, 2025 18:47
@cg2121 cg2121 force-pushed the replace-dynamic-cast branch 2 times, most recently from 3623034 to c2398aa Compare May 5, 2025 18:51
@RytoEX RytoEX self-assigned this May 5, 2025
Copy link
Copy Markdown
Member

@PatTheMav PatTheMav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I'm fine with this change, though it obviously touches a whole lot casts in the code that touch a whole lot of aspects of our frontend code, so this might be too late for the next version but should possibly merged after the next version is stabilised to give it some time to exist on the master branch.

@RytoEX WDYT?

@RytoEX
Copy link
Copy Markdown
Member

RytoEX commented Nov 19, 2025

Overall I'm fine with this change, though it obviously touches a whole lot casts in the code that touch a whole lot of aspects of our frontend code, so this might be too late for the next version but should possibly merged after the next version is stabilised to give it some time to exist on the master branch.

@RytoEX WDYT?

I think this should be merged for 32.1, merge timing TBD. It does have a merge conflict though.

@Warchamp7 Warchamp7 force-pushed the replace-dynamic-cast branch from c2398aa to 6789390 Compare November 27, 2025 22:09
@Warchamp7
Copy link
Copy Markdown
Member

I've rebased this PR

@Warchamp7 Warchamp7 moved this from In Review to Ready For Review in OBS Studio 32.1 PR Considerations Nov 27, 2025
@Warchamp7 Warchamp7 moved this from Ready For Review to Ready For Merge in OBS Studio 32.1 PR Considerations Nov 27, 2025
@RytoEX RytoEX requested review from Warchamp7 and gxalpha December 18, 2025 01:37
@RytoEX RytoEX added this to the OBS Studio 32.1 milestone Dec 18, 2025
@RytoEX RytoEX force-pushed the replace-dynamic-cast branch from 6789390 to 4b832ef Compare December 18, 2025 01:38
@RytoEX RytoEX self-requested a review December 18, 2025 02:43
Copy link
Copy Markdown
Member

@gxalpha gxalpha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just looking at it, I think most of these should use static_cast instead of either dynamic_cast or qobject_cast. qobject_cast is better than dynamic_cast if you want the dynamic behavior (i.e. have the object be null if the cast fails), but if you already know/assume that the cast will succeed (and dont check for new_obj == nullptr), you can just use static_cast.

Also, original git commit authorship appears to have gotten lost, that should probably be reverted.

@RytoEX RytoEX moved this from Ready For Merge to In Review in OBS Studio 32.1 PR Considerations Dec 18, 2025
@RytoEX RytoEX moved this from In Review to Requires Changes in OBS Studio 32.1 PR Considerations Dec 18, 2025
@Warchamp7 Warchamp7 force-pushed the replace-dynamic-cast branch from 4b832ef to 7b82eec Compare December 25, 2025 16:56
@Warchamp7
Copy link
Copy Markdown
Member

Just looking at it, I think most of these should use static_cast instead of either dynamic_cast or qobject_cast. qobject_cast is better than dynamic_cast if you want the dynamic behavior (i.e. have the object be null if the cast fails), but if you already know/assume that the cast will succeed (and dont check for new_obj == nullptr), you can just use static_cast.

Also, original git commit authorship appears to have gotten lost, that should probably be reverted.

Adjusted the PR for this, as well as removed a macro in the AutoConfig files that were casting the wizard() method in favor of an inline method on those pages.

When casting between QObjects, qobject_cast should always be used for dynamic
casts. This increases performance as there is no RTTI (Run Time Type
Information) with qobject_cast, like there is with dynamic_cast. In other cases
where we are sure or assume the cast is valid, use static_cast.

Using reinterpret_cast is bad practice, as you should use it in very
specific cases.

Co-Authored-By: Clayton Groeneveld <19962531+cg2121@users.noreply.github.com>
@Warchamp7 Warchamp7 force-pushed the replace-dynamic-cast branch from 7b82eec to f6596c5 Compare December 25, 2025 17:12
@Warchamp7 Warchamp7 moved this from Requires Changes to Ready For Review in OBS Studio 32.1 PR Considerations Jan 6, 2026
OBSDataAutoRelease service_settings = obs_data_create();

wiz->customServer = IsCustomService();
autoConfig()->customServer = IsCustomService();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to call autoConfig() here for every single line instead setting up a local variable that holds the reference to the widget?

It doesn't seem like it's expected for that object to change between all those function calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Fix Non-breaking change which fixes an issue UI/UX Anything to do with changes or additions to UI/UX elements.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants