using cut(), copy(), paste()
Hello. I have a program which have Edit menu, where I have cut, copy and paste.
I tried to use these menus with the following:
Code:
...
connect(action_Copy,SIGNAL(triggered()),lineEdit_2,SLOT(copy()));
connect(action_Copy,SIGNAL(triggered()),lineEdit_3,SLOT(copy()));
...
Now the copy menu works, but there is over one million connect there... How can I optimise this situation? I thinked about for cycle but I search other, better solution... Please help, thanks
Re: using cut(), copy(), paste()
Hey you will be calling one million "copy()" slots of line edit on each trigger of action_Copy. I think it is wrong (i might be wrong as i don't have any other info from you)
Instead have a custom slot - say on_Copy() to be triggered by action_Copy, and in that obtain the line edit with keyboard focus. After that just call obtainedLineEdit->copy().
Re: using cut(), copy(), paste()
Quote:
Originally Posted by
Gopala Krishna
Hey you will be calling one million "copy()" slots of line edit on each trigger of action_Copy. I think it is wrong (i might be wrong as i don't have any other info from you)
Instead have a custom slot - say on_Copy() to be triggered by action_Copy, and in that obtain the line edit with keyboard focus. After that just call obtainedLineEdit->copy().
But how can I resolve this?
Re: using cut(), copy(), paste()
Let's say you have a class called Window which contains those line edits.
You would begin with declaring a new custom slot:
Code:
class Window : public WhatEver
{
Q_OBJECT // <---
public:
[...]
private slots:
void copy(); // <---
};
Then, instead of connecting to each line edit's copy(), you would connect to this custom slot we declared above:
Code:
connect(action_Copy,SIGNAL(triggered()),this,SLOT(copy()));
Finally, you would implement the custom slot. More or less something like this:
Code:
void Window::copy()
{
// get the last child widget which has focus and
// try to cast it as line edit
QLineEdit* lineEdit
= dynamic_cast<QLineEdit
*>
(focusWidget
());
if (lineEdit)
{
// it was a line edit, perform copy
lineEdit->copy();
}
}
Re: using cut(), copy(), paste()
Quote:
Originally Posted by
jpn
Code:
QLineEdit* lineEdit
= dynamic_cast<QLineEdit
*>
(focusWidget
());
Thanks. This is what I searched for...
Re: using cut(), copy(), paste()
Awesome! I have been trying to find out how to do that for months now!