2017-09-25 7 views
1

Qt 5.9에서 SLOT 대신 C++ 키워드를 사용하려고합니다. 그게 가능합니까 (별도의 방법없이)?연결 신호/슬롯에 C++ 키워드 사용

뭔가 같은 : 연결 샘플에서

QImage *image_ptr = new QImage(3, 3, QImage::Format_Indexed8); 
QEventLoop evt; 
QFutureWatcher<QString> watcher; 
QTimer timer(this); 
timer.setSingleShot(true); 
QObject::connect(&watcher, &QFutureWatcher<QString>::finished, &evt, &QEventLoop::quit); 
QObject::connect(&timer, SIGNAL(timeout()), &watcher, SLOT(cancel())); 
QObject::connect(&timer, SIGNAL(timeout()), &evt, SLOT(quit)); 
QObject::connect(&timer, SIGNAL(timeout()), this, (delete image_ptr)); 
QFuture<QString> future = QtConcurrent::run(this,&myClass::myMethod,*image_ptr); 
watcher.setFuture(future); 
timer.start(100); 
evt.exec(); 
+3

람다를 사용하십시오. https://artandlogic.com/2013/09/qt-5-and-c11-lambdas-are-your-friend/ – drescherjm

+1

그 말로는 위험 해 보입니다. 제 말은 동시 실행이 여전히 실행 중이고 이미지를 해제하면 나쁜 일이 발생한다는 것입니다. – drescherjm

답변

1

당신은 람다 식을 사용할 수 있습니다 대신 (새로운 연결 구문에 대한 자세한 내용은 here).

이러한 예에서 수신자은 슬롯의 소유자 (이전 구문을 사용하는 경우)가 전역 변수 또는 매크로가 아니라는 것을 고려하십시오. 또한 람다를 사용할 때 sender() 메서드에 액세스 할 수 없으므로 다른 메서드를 통해 액세스 할 수 있도록 처리해야합니다. 이러한 상황을 해결하려면 람다에서 이러한 변수를 포착해야합니다. 귀하의 경우, 그것은 단지 포인터입니다.

QObject::connect(&timer, &QTimer::timeout, [&image_ptr]() { delete image_ptr; }); 
+0

예, 필요에 따라 작동했습니다. 감사합니다! –

3

람다 식 : 내 코드의 예를 아래에 작동하지 않는

QObject::connect(&timer, SIGNAL(timeout()), this, (delete image_ptr)); 

connect(
    sender, &Sender::valueChanged, 
    [=](const QString &newValue) { receiver->updateValue("senderValue", newValue); } 
);