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

在 Qt 5 中連接過載的信號和插槽

Connecting overloaded signals and slots in Qt 5(在 Qt 5 中連接過載的信號和插槽)
本文介紹了在 Qt 5 中連接過載的信號和插槽的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我無法掌握 Qt 5 中的新信號/插槽語法(使用指向成員函數(shù)的指針),如 新的信號槽語法.我試著改變這個:

I'm having trouble getting to grips with the new signal/slot syntax (using pointer to member function) in Qt 5, as described in New Signal Slot Syntax. I tried changing this:

QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                 slider, SLOT(setValue(int));

為此:

QObject::connect(spinBox, &QSpinBox::valueChanged,
                 slider, &QSlider::setValue);

但是當我嘗試編譯它時出現(xiàn)錯誤:

but I get an error when I try to compile it:

錯誤:沒有匹配的函數(shù)調(diào)用QObject::connect(QSpinBox*&,<未解析的重載函數(shù)類型>, QSlider*&, void(QAbstractSlider::*)(int))

error: no matching function for call to QObject::connect(QSpinBox*&, <unresolved overloaded function type>, QSlider*&, void (QAbstractSlider::*)(int))

我在 Linux 上嘗試過使用 clang 和 gcc,都使用 -std=c++11.

I've tried with clang and gcc on Linux, both with -std=c++11.

我做錯了什么,我該如何解決?

What am I doing wrong, and how can I fix it?

推薦答案

這里的問題是有兩個具有該名稱的信號:QSpinBox::valueChanged(int)QSpinBox::valueChanged(QString)代碼>.從 Qt 5.7 開始,提供了幫助函數(shù)來選擇所需的重載,因此您可以編寫

The problem here is that there are two signals with that name: QSpinBox::valueChanged(int) and QSpinBox::valueChanged(QString). From Qt 5.7, there are helper functions provided to select the desired overload, so you can write

connect(spinbox, qOverload<int>(&QSpinBox::valueChanged),
        slider, &QSlider::setValue);

對于 Qt 5.6 及更早版本,您需要通過將其轉(zhuǎn)換為正確的類型來告訴 Qt 您想要選擇哪一個:

For Qt 5.6 and earlier, you need to tell Qt which one you want to pick, by casting it to the right type:

connect(spinbox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
        slider, &QSlider::setValue);

我知道,它丑陋.但是沒有辦法解決這個問題.今天的教訓是:不要讓信號和槽過載!

I know, it's ugly. But there's no way around this. Today's lesson is: do not overload your signals and slots!

附錄:演員陣容真正令人討厭的是

Addendum: what's really annoying about the cast is that

  1. 一個重復(fù)兩次類名
  2. 即使返回值通常是void(對于信號),也必須指定返回值.
  1. one repeats the class name twice
  2. one has to specify the return value even if it's usually void (for signals).

所以我發(fā)現(xiàn)自己有時會使用這個 C++11 代碼片段:

So I've found myself sometimes using this C++11 snippet:

template<typename... Args> struct SELECT { 
    template<typename C, typename R> 
    static constexpr auto OVERLOAD_OF( R (C::*pmf)(Args...) ) -> decltype(pmf) { 
        return pmf;
    } 
};

用法:

connect(spinbox, SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged), ...)

我個人覺得它不是很有用.我希望當 Creator(或您的 IDE)在自動完成獲取 PMF 的操作時自動插入正確的演員表時,這個問題會自行消失.但與此同時...

I personally find it not really useful. I expect this problem to go away by itself when Creator (or your IDE) will automatically insert the right cast when autocompleting the operation of taking the PMF. But in the meanwhile...

注意:基于 PMF 的連接語法不需要 C++11

Note: the PMF-based connect syntax does not require C++11!

附錄 2:在 Qt 5.7 中添加了輔助函數(shù)來緩解這種情況,以我上面的解決方法為模型.主要的幫手是 qOverload(你我也有 qConstOverloadqNonConstOverload).

Addendum 2: in Qt 5.7 helper functions were added to mitigate this, modelled after my workaround above. The main helper is qOverload (you've also got qConstOverload and qNonConstOverload).

使用示例(來自文檔):

Usage example (from the docs):

struct Foo {
    void overloadedFunction();
    void overloadedFunction(int, QString);
};

// requires C++14
qOverload<>(&Foo:overloadedFunction)
qOverload<int, QString>(&Foo:overloadedFunction)

// same, with C++11
QOverload<>::of(&Foo:overloadedFunction)
QOverload<int, QString>::of(&Foo:overloadedFunction)

<小時>

附錄 3:如果您查看任何重載信號的文檔,現(xiàn)在文檔本身清楚地說明了重載問題的解決方案.例如,https://doc.qt.io/qt-5/qspinbox.html#valueChanged-1 說


Addendum 3: if you look at the documentation of any overloaded signal, now the solution to the overloading problem is clearly stated in the docs themselves. For instance, https://doc.qt.io/qt-5/qspinbox.html#valueChanged-1 says

注意:信號 valueChanged 在此類中被重載.為了使用函數(shù)指針語法連接到這個信號,Qt 提供了一個方便的助手來獲取函數(shù)指針,如下例所示:

Note: Signal valueChanged is overloaded in this class. To connect to this signal by using the function pointer syntax, Qt provides a convenient helper for obtaining the function pointer as shown in this example:

   connect(spinBox, QOverload<const QString &>::of(&QSpinBox::valueChanged),
[=](const QString &text){ /* ... */ });

這篇關(guān)于在 Qt 5 中連接過載的信號和插槽的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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)形?)
主站蜘蛛池模板: 国产福利在线观看 | 国产免费黄色 | 亚洲一区二区久久 | 日本一本草久p | 天天爱天天色 | 免费爱爱网站 | 成人aa| 91你懂的 | 天天躁日日躁狠狠躁av麻豆男男 | 成人欧美一区二区三区黑人孕妇 | 在线免费av网站 | 精品一区二区在线播放 | 亚洲综合色网 | 综合激情网 | 国内精品一区二区三区 | 国产精品免费一区二区三区 | 国产精选av | 一级看片免费视频 | 欧美在线视频免费观看 | 久久国产成人 | 免费视频久久久 | 天天操天天操 | 国产一区免费视频 | 精品无人国产偷自产在线 | 欧美日韩免费在线观看 | 天堂中文在线视频 | 久草黄色 | 亚洲aaa| www四虎| 国产视频在线观看视频 | 黄色网址av | www.日韩 | 国产日韩欧美一区 | 中国久久久 | 国产精品av一区二区 | 中文字幕av久久爽一区 | 伊人国产女 | 国产中文字幕一区二区 | 偷偷操网站 | 98国产精品 | 一级免费片|