久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

是否可以在沒有接收器實例的情況下將信號連接

Is it possible to connect a signal to a static slot without a receiver instance?(是否可以在沒有接收器實例的情況下將信號連接到靜態插槽?)
本文介紹了是否可以在沒有接收器實例的情況下將信號連接到靜態插槽?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

是否可以在沒有接收器實例的情況下將信號連接到靜態插槽?

Is it possible to connect a signal to static slot without receiver instance?

像這樣:connect(&object, SIGNAL(some()), STATIC_SLOT(staticFooMember()));

Qt 文檔中有一個帶有 [static slot] 屬性的 QApplication::closeAllWindows() 函數.文檔中有一個使用它的示例:

There is a QApplication::closeAllWindows() function with [static slot] attribute in Qt documentation. And there is an example of using it from the documentation:

exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));

是否允許執行相同的操作但不傳遞實例變量(例如,當一個類只有靜態函數時)?

Is it allowed to do the same action but without passing an instance variable (e.g. when a class has only static functions)?

class Some : public QObject {
    Q_OBJECT
public slots:
    static void foo();
private:
    Some();
};

<小時>

也許 Frank Osterfeld 是對的,在這種情況下最好使用單例模式,但我仍然很驚訝為什么這個功能還沒有實現.


Maybe Frank Osterfeld is right and it is better to use singleton pattern in this case but I am still surprised why this feature has not been implemented yet.

更新:

在 Qt 5 中這是可能的.

推薦答案

QT5 更新:是的,你可以

static void someFunction() {
    qDebug() << "pressed";
}
// ... somewhere else
QObject::connect(button, &QPushButton::clicked, someFunction);

在 QT4 中你不能:

不,這是不允許的.相反,允許使用作為靜態函數的插槽,但為了能夠連接它,您需要一個實例.

No it is not allowed. Rather, it is allowed to use a slot which is a static function, but to be able to connect it you need an instance.

在他們的例子中,

connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));

意味著比他們之前所說的

means than they previously called

QApplication* qApp = QApplication::instance();

連接對象的唯一接口是函數

The only interface for connecting object is the function

bool QObject::connect ( const QObject * sender, const QMetaMethod & signal, const QObject * receiver, const QMetaMethod & method, Qt::ConnectionType type = Qt::AutoConnection )

你打算如何擺脫const QObject *receiver?

檢查項目中的 moc 文件,它會自己說話.

Check the moc files in your project, it speaks by itself.

這篇關于是否可以在沒有接收器實例的情況下將信號連接到靜態插槽?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 国产免费一区二区 | 天天爽综合网 | 亚洲精品久久久一区二区三区 | 国产精品国产三级国产aⅴ原创 | 亚洲一区久久 | 日韩精品三区 | 伊人精品在线 | 亚洲毛片| 一本一道久久a久久精品蜜桃 | 久久精品亚洲精品国产欧美 | 在线色| caoporon| 久久久久国产一区二区三区四区 | 国产精品久久 | 另类专区成人 | jizz在线看片 | 在线观看www视频 | 一区二区在线 | 黄色片在线免费看 | 亚洲国产精品精华素 | 人人干人人干人人干 | 午夜二区 | 精品一区二区久久久久久久网站 | 天天综合干 | 日韩美av | 91免费电影 | www.久久久久久久久 | 久热免费 | 国产精品永久久久久 | 亚洲一区二区三区四区五区午夜 | 欧美日批| 在线精品亚洲欧美日韩国产 | 午夜天堂精品久久久久 | 午夜激情视频 | 欧美久久一区 | 7799精品视频天天看 | 日韩手机在线视频 | 国产无人区一区二区三区 | 亚洲在线 | 日韩一区中文字幕 | 成人在线免费观看 |