PDA

View Full Version : using cut(), copy(), paste()



systemz89
9th December 2007, 00:00
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:


...
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

Gopala Krishna
9th December 2007, 07:52
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().

systemz89
9th December 2007, 09:25
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?

jpn
9th December 2007, 09:44
Let's say you have a class called Window which contains those line edits.

You would begin with declaring a new custom slot:


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:

connect(action_Copy,SIGNAL(triggered()),this,SLOT( copy()));

Finally, you would implement the custom slot. More or less something like this:


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

systemz89
9th December 2007, 09:54
QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(focusWidget());


Thanks. This is what I searched for...

fnmblot
18th December 2007, 14:47
Awesome! I have been trying to find out how to do that for months now!