Overwrite lost focus event to save and display model's lastError - python

How do you overwrite a QWidget's focusOutEvent to save/submit and display the QSqlTablemodel's lastError, after losing focus? I want to inform the user if there was any problems saving the model and have him correct or discard the changes.
I tried implementing the code below on a QWidget, which is a tab in tabControl but it is not executing when I close the tab or when navigating to child/sub tabs
def focusOutEvent(self,event):
print("Losing focus now")
self.model.submitAll()
print(self.model.lastError().text())

The focusOutEvent generally requires the focusPolicy to be set to something other than the default of Qt::NoFocus in order to receive focus events.
The policy is Qt::TabFocus if the widget accepts keyboard focus by
tabbing, Qt::ClickFocus if the widget accepts focus by clicking,
Qt::StrongFocus if it accepts both, and Qt::NoFocus (the default) if
it does not accept focus at all.
Refer to http://doc.qt.io/archives/qt-4.8/qwidget.html#focusOutEvent & http://doc.qt.io/archives/qt-4.8/qwidget.html#focusPolicy-prop for more information on this.

Related

How to set focus to the old window on button click in PyQt5 Python?

My Current Project is to create a On Screen Keyboard for my personal usage with my personal functionality. I made gui and primary function in PyQt5 with Python. I managed to type the letter on button click with pyautogui.write() method. But the problem is, where I want to type there is no focus. suppose I want to write on chrome's address bar or any other input field on my monitor. when I click on button to type a letter, chrome lost focus. I want to set focus to the old window while press on any button. I searched on google about this but didn't found any answer. How can i set focus to old window? or is there any better way to type on focus lost state?
You should not try to "set the focus back", as it would be almost impossible to know what window had the focus before (and a new window might raise in the meantime). What you should actually do is to prevent your window to get focus at all, thus avoiding it to steal focus from the others.
In order to achieve this, you must set the appropriate window flag (or initialize the widget with it using the flags keyword argument), which for this is Qt.WindowDoesNotAcceptFocus.
Note that you might also want to set the Qt.WindowStaysOnTopHint in order to always keep your window above the others:
class MyKeyboard(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowFlags(
QtCore.Qt.WindowDoesNotAcceptFocus
| QtCore.Qt.WindowStaysOnTopHint
)

PyQt5 allow selection of QListView items while editing

I am trying to get click events (ultimately left, right and double click) to "pass through" an editor widget to the underlying QListView so that selections can happen. Figured Event Filters were probably the way to go, but I am a little confused as to where the eventFilter(object,event) function and the installEventFilter() call need to go in order for that to happen.
In my case, I am using a custom delegate class to draw my data in my QListView and am using an editor to update the model based upon cursor positions. I want this to just be constantly active, so I made my QListView active the editor upon entering an item.
dataView=QListView(self)
dataView.setGeometry(self.rect())
dataView.setViewMode(1)
dataView.setMovement(0)
dataView.setSelectionMode(QAbstractItemView.ExtendedSelection)
dataView.setMouseTracking(True)
dataView.entered.connect(dataView.edit)
delegate=CustomDelegate(self)
dataModel=QStandardItemModel(self)
dataView.setModel(dataModel)
dataView.setItemDelegate(delegate)
The downside to this is that while your mouse is on an item, it is now covered by the editor widget which I believe is collecting mouse click data thus blocking clicks from selecting the items within the QListView.
In my delegate, I have it create the editor as so and connect signals to update my data (frames) in my model to change the way the delegate displays and to close the editor
def createEditor(self,parent,option,index):
editor=TestEditor(parent)
editor.editingFinished.connect(self.deleteEditor)
editor.updateFrame.connect(self.updateFrames)
return editor
Where would I create my event filter? In my main where I am generating my QListView? Within a custom QListView? Within my Delegate? Or within my editor widget? And then where would I call the installEventFilter()?
It turns out in my case, the QListView wasn't responding to selection clicks because when you call the edit() function, the state of the viewer changes to "QAbstractItemView.EditingState" and in this state, apparently selection is disabled. I just subclassed QListView and added a beginEdit function.
class CustomList(QListView):
def __init__(self,parent=None):
super().__init__(parent)
def beginEdit(self,index):
state=self.state()
self.edit(index)
self.setState(state)
And then just connected this instead of edit.
dataView=CustomList(self)
dataView.entered.connect(dataView.beginEdit)

Turn QPushButton on and off

I have an app in PyQt with a few buttons to shift between modes of the application.
I have the clicked() signals of the buttons linked to the appropriate methods. My problem is that there are other ways to change modes (for instance, loading settings will automatically move the user to their default mode), and I'm using the QPushButtons as indicators of the "active mode". This was previously accomplished by having two icons for each button, one for the button being off and the other for the button being on. It's all been designed in QtDesigner, so clicking on one button turns it on (and turns the other buttons off) and changes the icons appropriately. And when the mouse is released, that button stays on.
The button stays on until another button is pressed.
I'm trying to figure out how I can change a button from "on" to "off" without the user actually pressing the button, so I can change modes appropriately.
The Qt docs make reference to the property I'm looking for but I can't find any more details than the fact that these states exist:
The most important modes or states are:
Available or not (grayed out, disabled).
Standard push button, toggling push button or menu button.
On or off (only for toggling push buttons).
Default or normal. The default button in a dialog can generally be "clicked" using the
Enter or Return key.
Auto-repeat or not.
Pressed down or not.
(http://qt-project.org/doc/qt-5/QPushButton.html)
To be more specific, I'm looking for a way to see the state of a QPushButton; to see whether it is "On" or "Off", and I'm looking for a way to change that state.
EDIT: I found the appropriate method QPushButton.isOn() but the problem is that it's in Qt3. (I'm using Qt5, where this method no longer exists). Clearly it's obsolete now, would anyone happen to know what replaced it?
http://doc.qt.digia.com/3.2/qpushbutton.html#isOn
QPushButton inherits from QAbstractButton and and therefore has the following methods: isChecked, setChecked and isCheckable, setCheckable. This way you can convert the button into a toggle button and ask/set the state. There is also setAutoRepeat and autoRepeat which controls the auto repeat programmatically.
Especially instead of isOn use isChecked.
Furthermore it also inherits from QWidget which has methods isEnabled and setEnabled. With this you can activate/de-active the button which is shown by graying out the button as well as by prohibiting clicks on the button.
Basically just study the documentation for QAbstractButton and QWidget to see how you can programmatically interact with a QPushButton to enable/disable it.

how to disable the window close button in pygame?

In a pygame application window, the minimize, resize and close buttons are present. Is there a way to disable the close(X) button?
I don't think there is, because some window managers don't give you the ability to remove the close button. But you can write an event handler such that the close button does whatever you want, including nothing.
Why do you want to prevent the user from closing? If it's just a matter that you would rather provide an in-game "quit" button that confirms and/or saves before quitting, you can perform the same task when the user hits the close button.
Just for the record, another option would be to pass the following argument to the set_mode() method call:
pygame.display.set_mode(..., flags = pygame.NOFRAME)
This however makes the whole frame go away, including the top strip to move the window around and the other buttons, such as minimize, so it's rather overkill for just getting rid of the X button.

Change the focus from one Text widget to another

I'm new to Python and I'm trying to create a simple GUI using Tkinter.
So often in many user interfaces, hitting the tab button will change the focus from one Text widget to another. Whenever I'm in a Text widget, tab only indents the text cursor.
Does anyone know if this is configurable?
This is very easy to do with Tkinter.
There are a couple of things that have to happen to make this work. First, you need to make sure that the standard behavior doesn't happen. That is, you don't want tab to both insert a tab and move focus to the next widget. By default events are processed by a specific widget prior to where the standard behavior occurs (typically in class bindings). Tk has a simple built-in mechanism to stop events from further processing.
Second, you need to make sure you send focus to the appropriate widget. There is built-in support for determining what the next widget is.
For example:
def focus_next_window(event):
event.widget.tk_focusNext().focus()
return("break")
text_widget=Text(...)
text_widget.bind("<Tab>", focus_next_window)
Important points about this code:
The method tk_focusNext() returns the next widget in the keyboard traversal hierarchy.
the method focus() sets the focus to that widget
returning "break" is critical in that it prevents the class binding from firing. It is this class binding that inserts the tab character, which you don't want.
If you want this behavior for all text widgets in an application you can use the bind_class() method instead of bind() to make this binding affect all text widgets.
You can also have the binding send focus to a very specific widget but I recommend sticking with the default traversal order, then make sure the traversal order is correct.
It is really simple in PyQt4 simply use this one single line below and you will be able to change focus by pressing tab button:
self.textEdit.setTabChangesFocus(True)
The focus traversal is somewhat customizable, usually letting the X windows manager handle it (with focus follows mouse, or click). According to the manual it should be possible to bind an event to the key press event, for tab presses, and triggering a focusNext event in those cases.

Categories