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

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

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

問題描述

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

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

但是當我嘗試編譯它時出現錯誤:

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

錯誤:沒有匹配的函數調用QObject::connect(QSpinBox*&,<未解析的重載函數類型>, 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 開始,提供了幫助函數來選擇所需的重載,因此您可以編寫

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 及更早版本,您需要通過將其轉換為正確的類型來告訴 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. 一個重復兩次類名
  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).

所以我發現自己有時會使用這個 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 中添加了輔助函數來緩解這種情況,以我上面的解決方法為模型.主要的幫手是 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:如果您查看任何重載信號的文檔,現在文檔本身清楚地說明了重載問題的解決方案.例如,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 在此類中被重載.為了使用函數指針語法連接到這個信號,Qt 提供了一個方便的助手來獲取函數指針,如下例所示:

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){ /* ... */ });

這篇關于在 Qt 5 中連接過載的信號和插槽的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 ()?環形?)
主站蜘蛛池模板: 日韩精品免费一区二区在线观看 | 三级黄片毛片 | 久久久国产一区 | 久久久免费电影 | 亚洲精品久久久久中文字幕欢迎你 | 成人一级黄色毛片 | 伦理午夜电影免费观看 | 亚洲免费观看 | 成人av播放 | av网站免费 | 天天躁日日躁aaaa视频 | 五月天激情综合网 | 一区二区三区高清不卡 | 成人18亚洲xxoo | 九九久久免费视频 | 日本精品视频在线 | 日日干夜夜操天天操 | 超碰在线免费av | 一区在线播放 | 在线91| 久久av资源网 | av永久免费 | 精品久久国产老人久久综合 | 男人av网 | 欧美成人免费 | 国产91综合一区在线观看 | 天天操夜夜拍 | 亚洲免费视频一区 | 亚洲国产aⅴ成人精品无吗 亚洲精品久久久一区二区三区 | 正在播放亚洲 | 国产视频在线观看一区二区三区 | 99精品久久久久 | 中文字幕成人网 | 婷婷久久网| 日本三级电影在线免费观看 | 亚洲精品电影在线观看 | 国产精品1区2区3区 中文字幕一区二区三区四区 | 中文字幕成人 | 黄色大片毛片 | 国产精品网址 | 激情六月丁香 |