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

是否可以在沒有接收器實(shí)例的情況下將信號(hào)連接

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

問題描述

是否可以在沒有接收器實(shí)例的情況下將信號(hào)連接到靜態(tài)插槽?

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

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

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

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()));

是否允許執(zhí)行相同的操作但不傳遞實(shí)例變量(例如,當(dāng)一個(gè)類只有靜態(tài)函數(shù)時(shí))?

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();
};

<小時(shí)>

也許 Frank Osterfeld 是對(duì)的,在這種情況下最好使用單例模式,但我仍然很驚訝為什么這個(gè)功能還沒有實(shí)現(xiàn).


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 中你不能:

不,這是不允許的.相反,允許使用作為靜態(tài)函數(shù)的插槽,但為了能夠連接它,您需要一個(gè)實(shí)例.

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();

連接對(duì)象的唯一接口是函數(shù)

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?

檢查項(xiàng)目中的 moc 文件,它會(huì)自己說話.

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

這篇關(guān)于是否可以在沒有接收器實(shí)例的情況下將信號(hào)連接到靜態(tài)插槽?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 午夜欧美一区二区三区在线播放 | 日韩精品一二三 | 成人自拍视频 | 黄a在线播放 | 欧美精品综合在线 | 伊人久久大香线 | 黄色大片免费播放 | 国产在线永久免费 | 视频一区在线观看 | 精品av | 亚洲欧洲中文 | 欧美 中文字幕 | 国产成人黄色 | 欧美综合国产精品久久丁香 | 最新国产精品视频 | 在线观看你懂的网站 | 亚洲一区二区三区四区五区中文 | 国产视频第一页 | 欧美精品久久久 | av中文字幕在线 | 中文字幕在线一区 | 中文字幕中文字幕 | 国产精品毛片一区二区三区 | 综合国产第二页 | 九九99久久 | 伊人一区| 午夜精品视频在线观看 | 二区欧美| 视频一区欧美 | 久草免费在线视频 | 午夜国产一级 | 欧美最猛性xxxxx亚洲精品 | 精品久久久一区 | 成人啊啊啊| 大学生a级毛片免费视频 | 欧美性video| 狠狠狠干 | 久久三区| 欧美极品视频在线观看 | 桃花av在线 | 97影院在线午夜 |