• Spectacle源码及分析3 —— Main.cpp解析2


    接前一篇文章《Spectacle源码及分析2 —— Main.cpp解析1》:

    Spectacle源码及分析2 —— Main.cpp解析1_蓝天居士的博客-CSDN博客

    继续深入分析Main.cpp。

    (7)代码片段:

    SpectacleCore lCore;

     SpectacleCore类的定义在src/SpectacleCore.h中,源码如下:

    1. class SpectacleCore : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. enum class StartMode {
    6. Gui = 0,
    7. DBus = 1,
    8. Background = 2,
    9. };
    10. explicit SpectacleCore(QObject *parent = nullptr);
    11. ~SpectacleCore() override = default;
    12. void init();
    13. QString filename() const;
    14. void setFilename(const QString &filename);
    15. void populateCommandLineParser(QCommandLineParser *lCmdLineParser);
    16. void initGuiNoScreenshot();
    17. Q_SIGNALS:
    18. void errorMessage(const QString &errString);
    19. void allDone();
    20. void grabFailed();
    21. public Q_SLOTS:
    22. void takeNewScreenshot(Spectacle::CaptureMode theCaptureMode, int theTimeout, bool theIncludePointer, bool theIncludeDecorations);
    23. void showErrorMessage(const QString &theErrString);
    24. void screenshotUpdated(const QPixmap &thePixmap);
    25. void screenshotsUpdated(const QVector &imgs);
    26. void screenshotCanceled();
    27. void screenshotFailed();
    28. void doStartDragAndDrop();
    29. void doNotify(const QUrl &theSavedAt);
    30. void doCopyPath(const QUrl &savedAt);
    31. void onActivateRequested(QStringList arguments, const QString & /*workingDirectory */);
    32. private:
    33. void ensureGuiInitiad();
    34. void initGui(int theDelay, bool theIncludePointer, bool theIncludeDecorations);
    35. Platform::GrabMode toPlatformGrabMode(Spectacle::CaptureMode theCaptureMode);
    36. void setUpShortcuts();
    37. StartMode mStartMode;
    38. bool mNotify;
    39. QString mFileNameString;
    40. QUrl mFileNameUrl;
    41. PlatformPtr mPlatform;
    42. MainWindowPtr mMainWindow = nullptr;
    43. EditorPtr mQuickEditor;
    44. bool mIsGuiInited = false;
    45. bool mCopyImageToClipboard;
    46. bool mCopyLocationToClipboard;
    47. bool mSaveToOutput;
    48. bool mEditExisting;
    49. bool mExistingLoaded;
    50. KWayland::Client::PlasmaShell *mWaylandPlasmashell = nullptr;
    51. };

    (8)代码片段:

    1. QCommandLineParser lCmdLineParser;
    2. aboutData.setupCommandLine(&lCmdLineParser);
    3. lCore.populateCommandLineParser(&lCmdLineParser);

    设置命令行解析器。

    (9)代码片段:

    1. // first parsing for help-about
    2. lCmdLineParser.process(app.arguments());
    3. aboutData.processCommandLine(&lCmdLineParser);

    首先解析“’帮助”中的“关于”项。对应的图形界面如下图所示:

    (10)代码片段:

    QGuiApplication::setFallbackSessionManagementEnabled(false);

    QGuiApplication::setFallbackSessionManagementEnabled的介绍如下(引自https://doc.qt.io/qt-5/qguiapplication.html#setFallbackSessionManagementEnabled

    void QGuiApplication::setFallbackSessionManagementEnabled(bool enabled)

    Sets whether QGuiApplication will use fallback session management to enabled.

    This function was introduced in Qt 5.6.

    (11)代码片段:

    1. auto disableSessionManagement = [](QSessionManager &sm) {
    2. sm.setRestartHint(QSessionManager::RestartNever);
    3. };

    QSessionManager::setRestartHint的介绍如下(引自QSessionManager — Qt for Python

    PySide2.QtGui.QSessionManager.setRestartHint(arg__1)

    Parameters:

    arg__1RestartHint

    Sets the application’s restart hint to hint . On application startup, the hint is set to RestartIfRunning .

    Note

    These flags are only hints, a session manager may or may not respect them.

    We recommend setting the restart hint in saveStateRequest() because most session managers perform a checkpoint shortly after an application’s startup.

    (12)代码片段:

    1. QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement);
    2. QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement);

    以下内容引自:QObject::connect 的几种连接方式_也许云知道的博客-CSDN博客_qobject::connect

    QObject::connect 的几种连接方式

    一、使用 SIGNAL、SLOT

    [static] QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)

    For example

    1. QLabel *label = new QLabel;
    2. QScrollBar *scrollBar = new QScrollBar;
    3. QObject::connect(scrollBar, SIGNAL(valueChanged(int)),label, SLOT(setNum(int)));

    二、使用 PointerToMemberFunction

    [static] QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)

    For example

    1. QLabel *label = new QLabel;
    2. QLineEdit *lineEdit = new QLineEdit;
    3. QObject::connect(lineEdit, &QLineEdit::textChanged,label, &QLabel::setText);

    这种方式有个问题,就是当你的槽函数有两个重载时,它会连接哪个呢?所以需要指定

    1. // 假设类的槽函数有连个重载,分别为 void Example::func() 、void Example::func(int)
    2. // 我们需要连接 void Example::func(int) ,则
    3. connect(this, somesignal, example, static_cast<void (Example:: *)(int)>(&Example::func));

    三、使用 lambda

    [static] QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)

    For example

    1. void someFunction();
    2. QPushButton *button = new QPushButton;
    3. QObject::connect(button, &QPushButton::clicked, someFunction);

     Lambda expressions can also be used:

    1. QByteArray page = ...;
    2. QTcpSocket *socket = new QTcpSocket;
    3. socket->connectToHost("qt-project.org", 80);
    4. QObject::connect(socket, &QTcpSocket::connected, [=] () {socket->write("GET " + page + "\r\n");});

  • 相关阅读:
    为什么不同版本的Qt库有时候也能相互链接?
    pat basic 1084 外观数列
    Django 的 settings 全局配置文件
    【计算机网络笔记】分组交换 vs 电路交换
    突破编程_C++_设计模式(解释器模式)
    基于ssm的公选课申报系统
    AI驱动全年业绩超预期!百度2023年营收1345.98亿元,净利润大涨39%
    c++ 阅读笔记
    【算法刷题 | 动态规划14】6.28(最大子数组和、判断子序列、不同的子序列)
    软件测试职业生涯需要编写的全套文档模板,收藏这一篇就够了 ~
  • 原文地址:https://blog.csdn.net/phmatthaus/article/details/127859845