Solved connect pointer of member function as QShortCuts slot
-
hi,
I'm trying to write a function that gets a vector of QKeySequence and a pointer to slot function.
i't compiles but when I press the button the slot doesn't activated.here is my code:
class MyClass:public QWidget
{
Q_OBJECT
public:
void connectVecToSlot();
void func(void (MyClass::*slot_func)(),vector<QKeySequence> vec);
public slots:
void do_something();
};the implementation of func:
void MyClass:: func(void (MyClass::*slot_func)(),vector<QKeySequence> vec){
vector<QKeySequence>::iterator it = vec.begin();
for ( ; it != vec.end(); ++it){
new QShortcut(*it,this,SLOT((slot_func)()));
}
}when i call to func:
void MyClass:: connectVecToSlot(){
vector<QKeySequence> vect = getMyVector();//a function that returns a vector of QKeySequence
func(&MyClass::do_something,vect);
} -
OK,
I found here the solution:
http://www.qtcentre.org/threads/8392-How-to-declare-SLOT-as-a-parameter-to-member-function -
@LeaA
Dunno, just a guess, but I don't see anyconnect()
? https://stackoverflow.com/a/45387865/489865 -
@JonB
I'm using Qt 4.7 and the format you show me is in Qt 5.9 -
@LeaA said in connect pointer of member function as QShortCuts slot:
SLOT((slot_func)())
where is your slot called slot_func() defined? And it should be SLOT(slotFunc()).
connect() will give you a return value which will be false in your case and you will get a runtime warning that such a slot does not exist.
Qt4 doesn't support PMF connects. -
it's defined in MyClass.
I'm getting a warning that slot_func unused parameter in func.. maybe because of that the SLOT doesn't activated? -
I don't see any function 'slot_func()' in your class... and the signature for SLOT() is also wrong as I mentioned before. You can't use a pmf in SLOT() signature!
-
@Christian-Ehrlicher
maybe you don't understand my question..
I have many SLOTs in me class and I want connect them to a short cut by using the func function.
So,every SLOT I want connect I want send it to func and in this function to connect it only for not write the same code in many places.. -
OK,
I found here the solution:
http://www.qtcentre.org/threads/8392-How-to-declare-SLOT-as-a-parameter-to-member-function -
There is no pmf in this thread either. And just for saving three params to a connect slot... don't know if it's worth ...