Pārlūkot izejas kodu

UI: Use custom property on QAction to retain profile or collection name

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.
PatTheMav 1 gadu atpakaļ
vecāks
revīzija
072102701c

+ 7 - 4
UI/window-basic-main-profiles.cpp

@@ -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);
@@ -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();
@@ -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);

+ 6 - 3
UI/window-basic-main-scene-collections.cpp

@@ -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);
@@ -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);