Skip to content

Commit

Permalink
UI: Use custom property on QAction to retain profile or collection name
Browse files Browse the repository at this point in the history
On some platforms (e.g. KDE) accelerators are automatically added to
the text properties of QActions, thus changing the value returned by
text().

Thus we cannot rely on the text to always represent the same text that
we set originally and have to explicitly store and retrieve the value
as a property.

Coincidentally this not only fixes possible issues on other platforms,
but is also architecturally more correct.
  • Loading branch information
PatTheMav authored and RytoEX committed Oct 21, 2024
1 parent 918fe61 commit 0721027
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 7 additions & 4 deletions UI/window-basic-main-profiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ void OBSBasic::ChangeProfile()
}

const std::string_view currentProfileName{config_get_string(App()->GetUserConfig(), "Basic", "Profile")};
const std::string selectedProfileName{action->text().toStdString()};
const QVariant qProfileName = action->property("profile_name");
const std::string selectedProfileName{qProfileName.toString().toStdString()};

if (currentProfileName == selectedProfileName) {
action->setChecked(true);
Expand All @@ -270,7 +271,7 @@ void OBSBasic::ChangeProfile()
if (!foundProfile) {
const std::string errorMessage{"Selected profile not found: "};

throw std::invalid_argument(errorMessage + currentProfileName.data());
throw std::invalid_argument(errorMessage + selectedProfileName.data());
}

const OBSProfile &selectedProfile = foundProfile.value();
Expand Down Expand Up @@ -307,9 +308,11 @@ void OBSBasic::RefreshProfiles(bool refreshCache)
for (auto &name : sortedProfiles) {
const std::string profileName = name.toStdString();
try {
OBSProfile &profile = profiles.at(profileName);
const OBSProfile &profile = profiles.at(profileName);
const QString qProfileName = QString().fromStdString(profileName);

QAction *action = new QAction(QString().fromStdString(profileName), this);
QAction *action = new QAction(qProfileName, this);
action->setProperty("profile_name", qProfileName);
action->setProperty("file_name", QString().fromStdString(profile.directoryName));
connect(action, &QAction::triggered, this, &OBSBasic::ChangeProfile);
action->setCheckable(true);
Expand Down
9 changes: 6 additions & 3 deletions UI/window-basic-main-scene-collections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ void OBSBasic::ChangeSceneCollection()

const std::string_view currentCollectionName{
config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
const std::string selectedCollectionName{action->text().toStdString()};
const QVariant qCollectionName = action->property("collection_name");
const std::string selectedCollectionName{qCollectionName.toString().toStdString()};

if (currentCollectionName == selectedCollectionName) {
action->setChecked(true);
Expand Down Expand Up @@ -310,9 +311,11 @@ void OBSBasic::RefreshSceneCollections(bool refreshCache)
for (auto &name : sortedSceneCollections) {
const std::string collectionName = name.toStdString();
try {
OBSSceneCollection &collection = collections.at(collectionName);
const OBSSceneCollection &collection = collections.at(collectionName);
const QString qCollectionName = QString().fromStdString(collectionName);

QAction *action = new QAction(QString().fromStdString(collectionName), this);
QAction *action = new QAction(qCollectionName, this);
action->setProperty("collection_name", qCollectionName);
action->setProperty("file_name", QString().fromStdString(collection.fileName));
connect(action, &QAction::triggered, this, &OBSBasic::ChangeSceneCollection);
action->setCheckable(true);
Expand Down

0 comments on commit 0721027

Please sign in to comment.