PDA

View Full Version : PRIMARY Selections



hayati
11th September 2006, 08:54
Linux has three Selection mechanism for copy and past operations: PRIMARY, SECONDARY, CLIPBOARD.

How can i get both CLIPBOARD and PRIMARY selections to set a QLineEdit's text.

when a text is selected in any window then PRIMARY Selection occurs. And the text is implicitly exists in PRIMARY Selection buffer. When user clicks middle mouse button text is pasted to focused window. I need the text as soon as it appears/changes in PRIMARY Selection buffer.

For example double clicking a text highligths the text, how can i get this text automatically into a qwidget(after double clicking). I wrote similar operation via QClipboard but it needs to explicit copy (menu->copy or Ctrl+C) on most of editors and text shown windows.
If i use QClipboard it runs perfectly (because it automatically copies the selection to QClipboard i think) on QT Assistant's text but Open office word processor or Mozilla selections needs Ctrl+C.

wysota
11th September 2006, 09:42
QClipboard *clip = QApplication::clipboard();
clip->setText(lineEdit->text(), QClipboard::Clipboard); // copy to global clipboard
clip->setText(lineEdit->text(), QClipboard::Selection); // copy to selection clipboard
Is that what you want?

hayati
11th September 2006, 14:37
yes it has similar functionality. but operation is reverse. i.e. selection buffer to lineEdit. Getting selection buffer at any time is not the issue.


//constructor
m_pClipBoard = QApplication::clipboard();
connect(m_pClipBoard, SIGNAL(selectionChanged()), this, SLOT(someFunc()));

//some Func
QString m_sKeyword = m_pClipBoard->text(QClipboard::Selection)

the problem is Selection buffer changes asynchroniously and from another application's window (ex:Mozilla, Open Office Writer etc.). And QT documentation says that:


X11 also has the concept of ownership; if you change the selection within a window, X11 will only notify the owner and the previous owner of the change, i.e. it will not notify all applications that the selection or clipboard data changed.
Lastly, the X11 clipboard is event driven, i.e. the clipboard will not function properly if the event loop is not running. Similarly, it is recommended that the contents of the clipboard are stored or retrieved in direct response to user-input events, e.g. mouse button or key presses and releases. You should not store or retrieve the clipboard contents in response to timer or non-user-input events.

so signals (selectionChanged() and dataChanged)) are not sent to my application. then i can't get any selectionChange() signal. But if i check the Selection buffer in a endless while loop i see the changes but it wastes much cpu. this behaviour is also against qt's advise.

Any solution/suggestion to do this without using a timer, endless loops.

wysota
11th September 2006, 15:56
Use a timer. That's the only way - a timer with one, two second timeout. You won't need a better resolution anyway. I just have a question -- what do you need it for?

hayati
11th September 2006, 16:45
i'm writing a dictionary just behaves like babylon. (Actually it has finished with this functionality) As babylon translates the highlighted-text to target language i need the highlighted text somehow. Unfortunately Acrobat files does not seem to be used as easy.

i noticed something, the text editor Kate is working perfectly even if i don't add any code (like timer). i think Kate forwards the signal to all windows perharps. the same behaviour can be seen on QT Assistant's help text. Weird!!??

yes you're right. timer is the most efficient solution.

Thanks
Regards

wysota
11th September 2006, 17:47
It's not Kate or Assistant. It is X11's responsibility to map selections to clipboard selections. It should work with all Qt widgets out of the box too. Maybe it is Acrobat which causes the trouble? Maybe it uses some kind of artificial selection?

hayati
11th September 2006, 18:56
right i've just tested it with other qt applications then it worked. But unfortunately mozilla, open office, and most of the other programs are not qt based (i think so). because they don't work while qt based applications work. i haven't tested it with acrobat, but even babylon does not work acrobat reader in windows :(

but i'm sure it doesn't work with non-qt widgets/applications.

wysota
11th September 2006, 19:09
but i'm sure it doesn't work with non-qt widgets/applications.
Hmm... it works fine for me... I tested kate->OO, OO->Firefox, Firefox->kate and OO->kate, all worked well.

hayati
12th September 2006, 11:32
yes they speak each other with no problem. OO-->kate, mozilla-->OO middle click past operations are managed by X11. even my qt widget responses paste from selection buffer with middle mouse button click. this is ok.
But non qt applications don't emit selectionChanged() signal except to owner and previous owner of selection-buffer/clipboard.
So my application pastes the selection if you click to the lineEdit with middle mouse button as usual. But i want not to click middle mouse button on my lineEdit. As soon as change in selection buffer occurs than my lineEdit should get the selection in the buffer.
it should act like babylon, in other words; my qt app's lineEdit widget should get selection buffer changes regardless from any other application - qt or nonqt. It should not need to get the selection buffer via middle button click.

the point is qt applications( kate, qt assistant) emits selectionChanged() to my application even if my application isn't the owner and previous owner. But mozilla, OO and most of the other programs don't emit this signal. they emit this signal only to owner and prev owner. this statement is standard of X11 as stated qt docs.

wysota
12th September 2006, 12:13
So my application pastes the selection if you click to the lineEdit with middle mouse button as usual. But i want not to click middle mouse button on my lineEdit. As soon as change in selection buffer occurs than my lineEdit should get the selection in the buffer.
it should act like babylon, in other words; my qt app's lineEdit widget should get selection buffer changes regardless from any other application - qt or nonqt. It should not need to get the selection buffer via middle button click.

Strange requirement, but ok. Use a timer as already suggested. In the timeout slot simply check if the content of the selection changed since last check and act accordingly.