Programing

슬롯에 인수 전달

crosscheck 2020. 10. 28. 07:43
반응형

슬롯에 인수 전달


QAction 및 QMenu를 사용하여 mouseReleaseEvent를 재정의하고 싶습니다.

connect(action1, SIGNAL(triggered()), this, SLOT(onStepIncreased()));

connect(action5, SIGNAL(triggered()), this, SLOT(onStepIncreased()));

connect(action10, SIGNAL(triggered()), this, SLOT(onStepIncreased()));

connect(action25, SIGNAL(triggered()), this, SLOT(onStepIncreased()));

connect(action50, SIGNAL(triggered()), this, SLOT(onStepIncreased()));

따라서 슬롯에 인수를 전달하고 싶습니다 onStepIncreased(1,5,10,25,50이라고 상상할 수 있음). 내가 어떻게 할 수 있는지 알아?


QSignalMapper를 사용하십시오 . 이렇게 :

QSignalMapper* signalMapper = new QSignalMapper (this) ;
connect (action1, SIGNAL(triggered()), signalMapper, SLOT(map())) ;
connect (action5, SIGNAL(triggered()), signalMapper, SLOT(map())) ;
connect (action10, SIGNAL(triggered()), signalMapper, SLOT(map())) ;
connect (action25, SIGNAL(triggered()), signalMapper, SLOT(map())) ;
connect (action50, SIGNAL(triggered()), signalMapper, SLOT(map())) ;

signalMapper -> setMapping (action1, 1) ;
signalMapper -> setMapping (action5, 5) ;
signalMapper -> setMapping (action10, 10) ;
signalMapper -> setMapping (action25, 25) ;
signalMapper -> setMapping (action50, 50) ;

connect (signalMapper, SIGNAL(mapped(int)), this, SLOT(onStepIncreased(int))) ;

Qt 5 및 C ++ 11 컴파일러를 사용하여 이러한 작업을 수행하는 관용적 인 방법은 다음과 같은 기능을 제공하는 것입니다 connect.

connect(action1,  &QAction::triggered, this, [this]{ onStepIncreased(1); });
connect(action5,  &QAction::triggered, this, [this]{ onStepIncreased(5); });
connect(action10, &QAction::triggered, this, [this]{ onStepIncreased(10); });
connect(action25, &QAction::triggered, this, [this]{ onStepIncreased(25); });
connect(action50, &QAction::triggered, this, [this]{ onStepIncreased(50); });

의 세 번째 인수 connect는 명목상 선택 사항입니다. 펑터가 실행될 스레드 컨텍스트를 설정하는 데 사용됩니다. 펑터가 QObject인스턴스를 사용할 때 항상 필요 합니다. 펑터가 여러 QObject인스턴스를 사용하는 경우 수명을 관리하는 공통 부모가 있어야하며 펑 터는 해당 부모를 참조하거나 객체가 펑터보다 오래 지속되도록해야합니다.

Windows에서는 MSVC2012 이상에서 작동합니다.


QObject::sender()함수는 슬롯에 신호를 보낸 객체에 대한 포인터를 반환합니다. 이를 사용하여 어떤 작업이 트리거되었는지 확인할 수 있습니다.


m_increase 멤버 변수로 QAction을 하위 클래스로 만들 수 있습니다.
triggered () 신호를 새 QAction 하위 클래스의 슬롯에 연결하고 올바른 매개 변수를 사용하여 새 신호 (예 : triggered (int number))를 내 보냅니다.
예 :

class MyAction:public QAction  
{  
public:  
    MyAction(int increase, ...)  
        :QAction(...), m_increase(increase)
    {  
        connect(this, SIGNAL(triggered()), this, SLOT(onTriggered()));  
    }  
protected Q_SLOTS:  
    void onTriggered()  
    {  
        emit triggered(m_increase);  
    }  

Q_SIGNALS:
    void triggered(int increase);   

private:  
    int m_increase;  
};

QVector<QAction*> W(100);
 W[1]= action1;
 W[5]= action5;
 W[10]= action10;
 W[25]= action25;
 W[50]= action50;

for (int i=0; i<100; ++i)
{
  QSignalMapper* signalmapper = new QSignalMapper();
  connect (W[i], SIGNAL(triggered()), signalmapper, SLOT(map())) ;
  signalmapper ->setMapping (W[i], i);
  connect (signalmapper , SIGNAL(mapped(int)), this, SLOT(onStepIncreased(int)));
} 

참고 URL : https://stackoverflow.com/questions/5153157/passing-an-argument-to-a-slot

반응형