PDA

View Full Version : Emulating Unix on Windows



eichner
14th February 2008, 15:05
I am working on a project where I have to simulate a Unix environment in my Windows application. That is, I want to have the active component automatically change to be the window or control under the cursor. Specifically, I need to have the space bar press the button under the cursor (for all buttons on all windows in my application). My first question is: Is there any way to turn on a "focus when mouse over" policy in QT (either globally or on a per control basis)?

Since I could not find a "focus when mouse over" ability in QT, as a test case, I tried to create MyHoverButton derived from QPushButton. In order to use MyHoverButton I changed QPushButton to MyHoverButton in one of my Dialog.ui files. However, when QT tried to process this file it placed "#include <MyHoverButton.h>" in the ui_dialog.h file. Since, MyHoverButton.h is in my project's directory and not the System or QT directory, the code will not compile. I could change the line in the ui_dialog.h to #include "MyHoverButton.h" however, the next time QT compiles the .ui file, it will place the "#include <MyHoverButton.h>" line back in the file. My second question is: How can I change the class defined in the QT designer from the standard QT control (like QPushButton) to a custom control derived from the standard control in a way that does not break the QT designer?

marcel
14th February 2008, 15:26
Since I could not find a "focus when mouse over" ability in QT, as a test case, I tried to create MyHoverButton derived from QPushButton. In order to use MyHoverButton I changed QPushButton to MyHoverButton in one of my Dialog.ui files. However, when QT tried to process this file it placed "#include <MyHoverButton.h>" in the ui_dialog.h file. Since, MyHoverButton.h is in my project's directory and not the System or QT directory, the code will not compile. I could change the line in the ui_dialog.h to #include "MyHoverButton.h" however, the next time QT compiles the .ui file, it will place the "#include <MyHoverButton.h>" line back in the file. My second question is: How can I change the class defined in the QT designer from the standard QT control (like QPushButton) to a custom control derived from the standard control in a way that does not break the QT designer?

This is the correct way to go.
In Designer, you must promote the QPushButton to your custom widget, then you will be asked for the header. Don't do it manually.

eichner
14th February 2008, 17:03
Thanks for the tip. I did not know about the "Promote" feature of QT. Promoting the QPushButton worked great!! I now have a push button which has focus whenever the mouse is over it!

I would like to do this for ALL widgets in my application. Is there any way to override the QWidget class from which all of the other widgets are derived? Or must I write MyHoverComboBox, MyHoverLineEdit, MyHoverCheckBox, ... classes?

marcel
14th February 2008, 17:05
No direct way.
You have to create a subclass for every widget you want to behave differently.

jpn
14th February 2008, 17:12
I think it could be done either by sublassing QApplication and re-impelementing notify() or installing an event filter on the application object.

eichner
14th February 2008, 19:13
Thanks for the help.

I went ahead and created MyHoverCheckBox, MyHoverComboBox, and MyHoverLineEdit to cover most of the controls. I will address lists and tables later.

The code to do this is pretty simple. I even created a MyHoverDialog derived from QDialog so my Windows application has a real Unix feel to it.

The only remaining problem is ,in Windows, when a dialog is made visible it is automatically given focus. So ... when achild window is created which is not under the cursor the "wrong" window has focus until I move the mouse over one of my controls which changes the "active control". I thought about overriding the "showEvent" method to try and counteract this but, the problem is more complicated then it seems - because the new child window may or may not need focus (depending on whether the current cursor position is within the bounds of the window). And the documentation on the "showEvent" method says that it is called before the dialog is shown. So, I do not know if the diaglog's data (e.g. geometry) is valid.