PDA

View Full Version : QTextEdit hasFocus() not working on linux



vermarajeev
20th December 2006, 11:50
Hi guys,
First let me tell that the problem is on fedora (linux) but works fine on windows.

Please find the attached file for source code. There is a textedit.pro and I have created an ./textEdit incase if the program doesnt run...

I have two QTextEdits displayed on QMainWindow. I have also provided a menuItem "Insert" on the QMainWindow. When you run the program Initially the cursor will be fucossed to first QTextEdit( above ). Now when I click on "insert" menuItem a dockwindow is displayed with some image. The cursor is disappeared from first QTextEdit(on linux). I dont know why??

Now what I want is when I click on dockwindow(on Image), If the focus is on first QTextEdit the focus should go to second QTextEdit(below) and vice versa (i.e switch to other)

Is there any problem with setFocus as given in the code.

The code works fine on windows but on linux the focus is disappearing because of which the switching is not happening...

Please help me

Details about file attached.

Contains

test.h //for textEdit
test.cpp //for textEdit
chemheaderfooteroption.cpp //for dockwindow
chemheaderfooteroption.h //for dockwindow

wysota
20th December 2006, 12:03
The code works fine on windows but on linux the focus is disappearing because of which the switching is not happening...

AFAIK the behaviour is perfectly normal and is a result of different focus handling policies on both platforms. There is a difference between an active window and a focused window, so when you click on some other window making it active, the focus in moved with it on Linux but not on Windows. Qt just follows the platform convention here.

vermarajeev
20th December 2006, 12:21
AFAIK the behaviour is perfectly normal and is a result of different focus handling policies on both platforms. There is a difference between an active window and a focused window, so when you click on some other window making it active, the focus in moved with it on Linux but not on Windows. Qt just follows the platform convention here.

So, how should I solve the above problem on linux. I want the focus to be there on TextEdit when the dock window becomes active....

Or is there any other thing I can do....

Please help me

thankx in advance

wysota
20th December 2006, 13:07
Maybe you don't need that focus after all? Maybe it is enough to track which of the text edits had the focus last? Connecting to the focusChanged() signal of QApplication and storing a pointer to the active widget (if it is one of your text edits) should do the thing nicely. I remember I used such a trick once and it was working ok.

vermarajeev
21st December 2006, 05:24
Maybe you don't need that focus after all? Maybe it is enough to track which of the text edits had the focus last? Connecting to the focusChanged() signal of QApplication and storing a pointer to the active widget (if it is one of your text edits) should do the thing nicely. I remember I used such a trick once and it was working ok.

I cannot find focusChanged() in Qt3.3.5

wysota
21st December 2006, 09:44
Right... it's from Qt4. In Qt3 you can achieve the same by applying event filters to both text edits and catching focusIn events.

vermarajeev
21st December 2006, 13:37
Right... it's from Qt4. In Qt3 you can achieve the same by applying event filters to both text edits and catching focusIn events.

I just tried as you said.
I have done like this


firstTextEdit->installEventFilter( this ); //this indicates my QMainwindow
secondTextEdit->installEventFilter( this );

then in

bool MainWindow::eventFilter( QObject *obj, QEvent *e )
{
if ( obj == firstTextEdit && e->type() == QEvent::FocusIn )
_first = true;
else
_first = false;
}
//_first is member variable which tells that the previous focusIn was of firstTextEdit
Then I check the boolean value in the slot that I have defined

void MainWindow::switch()
{
if(_first)
second->setFocus();
else
first->setFocus();
}
But still the problem is that the cursor is not visible. When I check this condition
if(_first)
second->setFocus();
else
first->setFocus();

if _first is true then the focus should be on second but since the doxkwindow is active the cursor is not visible.

Please give me some more suggestions wysota...

Thankx in advance

wysota
21st December 2006, 13:51
Check if your event filter is triggered and with what arguments. Also don't try to force the focus this way, it won't work. Instead just make the action on a proper widget based on the last focused text edit. You might also try using setActiveWindow (or something simmilar, I don't remember the exact name now) too.

vermarajeev
22nd December 2006, 03:27
Check if your event filter is triggered and with what arguments. Also don't try to force the focus this way, it won't work. Instead just make the action on a proper widget based on the last focused text edit. You might also try using setActiveWindow (or something simmilar, I don't remember the exact name now) too.

Hi wysota,
Thankx for your understanding

As per your comments
"Also don't try to force the focus this way, it won't work. Instead just make the action on a proper widget based on the last focused text edit. You might also try using setActiveWindow (or something simmilar, I don't remember the exact name now) too."

Please help me to implement this. I will be thankful to you if you can provide some code example.

Thankx

GreyGeek
25th December 2006, 16:47
I decided to do away with QT3 support in my QT4.2.2 installation so I compiled 4.2.2 without QT3 support. When I compiled my app all my "gotFocus()", "lostFocus()" and related triggers and events stopped working.

QT Support says that QT4.2.2 doesn't have got those events on controls. Dropping them was a BIG Mistake, IMO. I reconfigured and compiled QT 4.2.2 WITH QT3 support so my apps would compile and run properly again.

vermarajeev
27th December 2006, 03:27
Hi wyosta,
Eagerly waiting for your reply. I'm just stuck here and is in need of your suggestions. Can you please help to solve this probelm. I'll be really thankful to you.

Waiting for a reply

Thankx

wysota
27th December 2006, 09:51
Example attached. Click on one of the text edits, then click on the empty QWidget to make the text edit loose focus, then click on the push button.

vermarajeev
28th December 2006, 08:55
Example attached. Click on one of the text edits, then click on the empty QWidget to make the text edit loose focus, then click on the push button.

Hi wysota,
Thankx a lot, it works...What mistake I was doing is in the eventFilter I was just returning true, hence the focusIn event was ate up....But as the solution you have provided it returns false....

I have got the output but still I not completely got about return TRUE and return FALSe, I read Qt Assistant and it tells that if you want to stop the event from further processing return TRUE and if not return FALSE...

Can you clear about that to me.....

Any way thankx a lot....You are owesome :))

wysota
28th December 2006, 11:05
That's right. But in this case you don't want to stop event processing - you just want to monitor for occurence of the event, so you need to return false here.