接前一篇文章《Spectacle源码及分析2 —— Main.cpp解析1》:
Spectacle源码及分析2 —— Main.cpp解析1_蓝天居士的博客-CSDN博客
继续深入分析Main.cpp。
(7)代码片段:
SpectacleCore lCore;
SpectacleCore类的定义在src/SpectacleCore.h中,源码如下:
- class SpectacleCore : public QObject
- {
- Q_OBJECT
-
- public:
- enum class StartMode {
- Gui = 0,
- DBus = 1,
- Background = 2,
- };
-
- explicit SpectacleCore(QObject *parent = nullptr);
- ~SpectacleCore() override = default;
- void init();
-
- QString filename() const;
- void setFilename(const QString &filename);
-
- void populateCommandLineParser(QCommandLineParser *lCmdLineParser);
- void initGuiNoScreenshot();
-
- Q_SIGNALS:
-
- void errorMessage(const QString &errString);
- void allDone();
- void grabFailed();
-
- public Q_SLOTS:
-
- void takeNewScreenshot(Spectacle::CaptureMode theCaptureMode, int theTimeout, bool theIncludePointer, bool theIncludeDecorations);
- void showErrorMessage(const QString &theErrString);
- void screenshotUpdated(const QPixmap &thePixmap);
- void screenshotsUpdated(const QVector
&imgs) ; - void screenshotCanceled();
- void screenshotFailed();
- void doStartDragAndDrop();
- void doNotify(const QUrl &theSavedAt);
- void doCopyPath(const QUrl &savedAt);
-
- void onActivateRequested(QStringList arguments, const QString & /*workingDirectory */);
-
- private:
- void ensureGuiInitiad();
- void initGui(int theDelay, bool theIncludePointer, bool theIncludeDecorations);
- Platform::GrabMode toPlatformGrabMode(Spectacle::CaptureMode theCaptureMode);
- void setUpShortcuts();
-
- StartMode mStartMode;
- bool mNotify;
- QString mFileNameString;
- QUrl mFileNameUrl;
- PlatformPtr mPlatform;
- MainWindowPtr mMainWindow = nullptr;
- EditorPtr mQuickEditor;
- bool mIsGuiInited = false;
- bool mCopyImageToClipboard;
- bool mCopyLocationToClipboard;
- bool mSaveToOutput;
- bool mEditExisting;
- bool mExistingLoaded;
-
- KWayland::Client::PlasmaShell *mWaylandPlasmashell = nullptr;
- };
(8)代码片段:
- QCommandLineParser lCmdLineParser;
-
- aboutData.setupCommandLine(&lCmdLineParser);
- lCore.populateCommandLineParser(&lCmdLineParser);
设置命令行解析器。
(9)代码片段:
- // first parsing for help-about
- lCmdLineParser.process(app.arguments());
- aboutData.processCommandLine(&lCmdLineParser);
首先解析“’帮助”中的“关于”项。对应的图形界面如下图所示:
(10)代码片段:
QGuiApplication::setFallbackSessionManagementEnabled(false);
QGuiApplication::setFallbackSessionManagementEnabled的介绍如下(引自https://doc.qt.io/qt-5/qguiapplication.html#setFallbackSessionManagementEnabled)
Sets whether QGuiApplication will use fallback session management to enabled.
This function was introduced in Qt 5.6.
(11)代码片段:
- auto disableSessionManagement = [](QSessionManager &sm) {
- sm.setRestartHint(QSessionManager::RestartNever);
- };
QSessionManager::setRestartHint的介绍如下(引自QSessionManager — Qt for Python)
PySide2.QtGui.QSessionManager.setRestartHint(arg__1)¶
Parameters:
arg__1 – RestartHint
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)代码片段:
- QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement);
- 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
- QLabel *label = new QLabel;
- QScrollBar *scrollBar = new QScrollBar;
- 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
- QLabel *label = new QLabel;
- QLineEdit *lineEdit = new QLineEdit;
- QObject::connect(lineEdit, &QLineEdit::textChanged,label, &QLabel::setText);
这种方式有个问题,就是当你的槽函数有两个重载时,它会连接哪个呢?所以需要指定
- // 假设类的槽函数有连个重载,分别为 void Example::func() 、void Example::func(int)
- // 我们需要连接 void Example::func(int) ,则
- 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
- void someFunction();
- QPushButton *button = new QPushButton;
- QObject::connect(button, &QPushButton::clicked, someFunction);
Lambda expressions can also be used:
- QByteArray page = ...;
- QTcpSocket *socket = new QTcpSocket;
- socket->connectToHost("qt-project.org", 80);
- QObject::connect(socket, &QTcpSocket::connected, [=] () {socket->write("GET " + page + "\r\n");});