diff options
Diffstat (limited to 'sources/pyside6/plugins/designer/designercustomwidgets.cpp')
-rw-r--r-- | sources/pyside6/plugins/designer/designercustomwidgets.cpp | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/sources/pyside6/plugins/designer/designercustomwidgets.cpp b/sources/pyside6/plugins/designer/designercustomwidgets.cpp index 5585c7d22..d23156a9d 100644 --- a/sources/pyside6/plugins/designer/designercustomwidgets.cpp +++ b/sources/pyside6/plugins/designer/designercustomwidgets.cpp @@ -1,6 +1,7 @@ // Copyright (C) 2021 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +#undef slots #include <Python.h> // Include before Qt headers due to 'slots' macro definition #include "designercustomwidgets.h" @@ -17,6 +18,8 @@ #include <string_view> +using namespace Qt::StringLiterals; + Q_LOGGING_CATEGORY(lcPySidePlugin, "qt.pysideplugin") static const char pathVar[] = "PYSIDE_DESIGNER_PLUGINS"; @@ -27,7 +30,7 @@ static const char pythonPathVar[] = "PYTHONPATH"; static QDesignerCustomWidgetCollectionInterface *findPyDesignerCustomWidgetCollection() { static const char propertyName[] = "__qt_PySideCustomWidgetCollection"; - if (auto coreApp = QCoreApplication::instance()) { + if (auto *coreApp = QCoreApplication::instance()) { const QVariant value = coreApp->property(propertyName); if (value.isValid() && value.canConvert<void *>()) return reinterpret_cast<QDesignerCustomWidgetCollectionInterface *>(value.value<void *>()); @@ -47,17 +50,17 @@ static QString pyStringToQString(PyObject *s) static QString pyStr(PyObject *o) { PyObject *pstr = PyObject_Str(o); - return pstr ? pyStringToQString(pstr) : QString(); + return pstr != nullptr ? pyStringToQString(pstr) : QString(); } static QString pyErrorMessage() { - QString result = QLatin1String("<error information not available>"); + QString result = "<error information not available>"_L1; PyObject *ptype = {}; PyObject *pvalue = {}; PyObject *ptraceback = {}; PyErr_Fetch(&ptype, &pvalue, &ptraceback); - if (pvalue) + if (pvalue != nullptr) result = pyStr(pvalue); PyErr_Restore(ptype, pvalue, ptraceback); return result; @@ -84,7 +87,7 @@ static bool runPyScript(const char *script, QString *errorMessage) { PyObject *main = PyImport_AddModule("__main__"); if (main == nullptr) { - *errorMessage = QLatin1String("Internal error: Cannot retrieve __main__"); + *errorMessage = "Internal error: Cannot retrieve __main__"_L1; return false; } PyObject *globalDictionary = PyModule_GetDict(main); @@ -115,8 +118,7 @@ static bool runPyScriptFile(const QString &fileName, QString *errorMessage) file.close(); const bool ok = runPyScript(script.constData(), errorMessage); if (!ok && !errorMessage->isEmpty()) { - errorMessage->prepend(QLatin1String("Error running ") + fileName - + QLatin1String(": ")); + errorMessage->prepend("Error running "_L1 + fileName + ": "_L1); } return ok; } @@ -154,10 +156,10 @@ static void initVirtualEnvironment() pythonPath.append(virtualEnvPath + R"(\Lib\site-packages)"); break; case QOperatingSystemVersion::MacOS: - pythonPath.append(virtualEnvPath + QByteArrayLiteral("/lib/python") + + pythonPath.append(virtualEnvPath + "/lib/python"_ba + QByteArray::number(majorVersion) + '.' + QByteArray::number(minorVersion) - + QByteArrayLiteral("/site-packages")); + + "/site-packages"_ba); break; default: break; @@ -175,13 +177,20 @@ static void initPython() qAddPostRoutine(Py_Finalize); } +static bool withinQtDesigner = false; + PyDesignerCustomWidgets::PyDesignerCustomWidgets(QObject *parent) : QObject(parent) { qCDebug(lcPySidePlugin, "%s", __FUNCTION__); + withinQtDesigner = QCoreApplication::applicationName() == u"Designer" + && QCoreApplication::organizationName() == u"QtProject"; + if (!qEnvironmentVariableIsSet(pathVar)) { - qCWarning(lcPySidePlugin, "Environment variable %s is not set, bailing out.", - pathVar); + if (withinQtDesigner) { + qCWarning(lcPySidePlugin, "Environment variable %s is not set, bailing out.", + pathVar); + } return; } @@ -197,7 +206,7 @@ PyDesignerCustomWidgets::PyDesignerCustomWidgets(QObject *parent) : QObject(pare QDir dir(p); if (dir.exists()) { const QFileInfoList matches = - dir.entryInfoList({QStringLiteral("register*.py")}, QDir::Files, + dir.entryInfoList({u"register*.py"_s}, QDir::Files, QDir::Name); for (const auto &fi : matches) pythonFiles.append(fi.absoluteFilePath()); @@ -224,11 +233,13 @@ PyDesignerCustomWidgets::PyDesignerCustomWidgets(QObject *parent) : QObject(pare qputenv(pythonPathVar, value); } - initPython(); + // Might be initialized already, for example, when loaded from QUiLoader. + if (Py_IsInitialized() == 0) + initPython(); // Run all register*py files QString errorMessage; - for (const auto &pythonFile : qAsConst(pythonFiles)) { + for (const auto &pythonFile : std::as_const(pythonFiles)) { qCDebug(lcPySidePlugin) << "running" << pythonFile; if (!runPyScriptFile(pythonFile, &errorMessage)) qCWarning(lcPySidePlugin, "%s", qPrintable(errorMessage)); @@ -242,8 +253,9 @@ PyDesignerCustomWidgets::~PyDesignerCustomWidgets() QList<QDesignerCustomWidgetInterface *> PyDesignerCustomWidgets::customWidgets() const { - if (auto collection = findPyDesignerCustomWidgetCollection()) + if (auto *collection = findPyDesignerCustomWidgetCollection()) return collection->customWidgets(); - qCWarning(lcPySidePlugin, "No instance of QPyDesignerCustomWidgetCollection was found."); + if (withinQtDesigner) + qCWarning(lcPySidePlugin, "No instance of QPyDesignerCustomWidgetCollection was found."); return {}; } |