From 404cf0f63229c6b46a77220769aebab10b1b383a Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Thu, 21 Oct 2021 10:54:48 +0200 Subject: [PATCH] CppEditor: Mark generated setter as slot only for QObject-derived class Fixes: QTCREATORBUG-25789 Change-Id: I79174bcd617eb54540009491031cfae380383c41 Reviewed-by: David Schulz --- src/plugins/cppeditor/cppquickfix_test.cpp | 27 ++++++++++++++-------- src/plugins/cppeditor/cppquickfixes.cpp | 17 +++++++++++--- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 95b2441e68e..7cfe3206915 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -3236,6 +3236,8 @@ void QuickfixTest::testGenerateGetterSetterOnlySetter() QList testDocuments; QByteArray original; QByteArray expected; + QuickFixSettings s; + s->setterAsSlot = true; // To be ignored, as we don't have QObjects here. // Header File original = @@ -3263,7 +3265,6 @@ void QuickfixTest::testGenerateGetterSetterOnlySetter() "}\n"; testDocuments << CppTestDocument::create("file.cpp", original, expected); - QuickFixSettings s; s->setterInCppFileFrom = 1; s->setterParameterNameTemplate = "value"; @@ -3547,10 +3548,12 @@ void QuickfixTest::testInsertQtPropertyMembers_data() QTest::addColumn("expected"); QTest::newRow("InsertQtPropertyMembers") - << _("struct XmarksTheSpot {\n" + << _("struct QObject { void connect(); }\n" + "struct XmarksTheSpot : public QObject {\n" " @Q_PROPERTY(int it READ getIt WRITE setIt RESET resetIt NOTIFY itChanged)\n" "};\n") - << _("struct XmarksTheSpot {\n" + << _("struct QObject { void connect(); }\n" + "struct XmarksTheSpot : public QObject {\n" " Q_PROPERTY(int it READ getIt WRITE setIt RESET resetIt NOTIFY itChanged)\n" "\n" "public:\n" @@ -3582,10 +3585,12 @@ void QuickfixTest::testInsertQtPropertyMembers_data() "}\n"); QTest::newRow("InsertQtPropertyMembersResetWithoutSet") - << _("struct XmarksTheSpot {\n" + << _("struct QObject { void connect(); }\n" + "struct XmarksTheSpot : public QObject {\n" " @Q_PROPERTY(int it READ getIt RESET resetIt NOTIFY itChanged)\n" "};\n") - << _("struct XmarksTheSpot {\n" + << _("struct QObject { void connect(); }\n" + "struct XmarksTheSpot : public QObject {\n" " Q_PROPERTY(int it READ getIt RESET resetIt NOTIFY itChanged)\n" "\n" "public:\n" @@ -3615,10 +3620,12 @@ void QuickfixTest::testInsertQtPropertyMembers_data() "}\n"); QTest::newRow("InsertQtPropertyMembersResetWithoutSetAndNotify") - << _("struct XmarksTheSpot {\n" + << _("struct QObject { void connect(); }\n" + "struct XmarksTheSpot : public QObject {\n" " @Q_PROPERTY(int it READ getIt RESET resetIt)\n" "};\n") - << _("struct XmarksTheSpot {\n" + << _("struct QObject { void connect(); }\n" + "struct XmarksTheSpot : public QObject {\n" " Q_PROPERTY(int it READ getIt RESET resetIt)\n" "\n" "public:\n" @@ -3642,13 +3649,15 @@ void QuickfixTest::testInsertQtPropertyMembers_data() "}\n"); QTest::newRow("InsertQtPropertyMembersPrivateBeforePublic") - << _("class XmarksTheSpot {\n" + << _("struct QObject { void connect(); }\n" + "class XmarksTheSpot : public QObject {\n" "private:\n" " @Q_PROPERTY(int it READ getIt WRITE setIt NOTIFY itChanged)\n" "public:\n" " void find();\n" "};\n") - << _("class XmarksTheSpot {\n" + << _("struct QObject { void connect(); }\n" + "class XmarksTheSpot : public QObject {\n" "private:\n" " Q_PROPERTY(int it READ getIt WRITE setIt NOTIFY itChanged)\n" " int m_it;\n" diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 91974761c28..6f5d9d07d17 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -3971,9 +3971,20 @@ void GetterSetterRefactoringHelper::performGeneration(ExistingGetterSetterData d } // setter declaration - const InsertionPointLocator::AccessSpec setterAccessSpec = m_settings->setterAsSlot - ? InsertionPointLocator::PublicSlot - : InsertionPointLocator::Public; + InsertionPointLocator::AccessSpec setterAccessSpec = InsertionPointLocator::Public; + if (m_settings->setterAsSlot) { + const QByteArray connectName = "connect"; + const Identifier connectId(connectName.data(), connectName.size()); + const QList items = m_operation->context().lookup(&connectId, data.clazz); + for (const LookupItem &item : items) { + if (item.declaration() && item.declaration()->enclosingClass() + && overview.prettyName(item.declaration()->enclosingClass()->name()) + == "QObject") { + setterAccessSpec = InsertionPointLocator::PublicSlot; + break; + } + } + } const auto createSetterBodyWithSignal = [this, &getSetTemplate, &data] { QString body; QTextStream setter(&body);