I'm trying to use the beforeInsert() signal emitted by the QSqlRelationalTableModel in PyQt. However, even after I make edits to the model, the slot connected to the signal does't run. When exactly is the beforeInsert() signal emitted? Could someone show a working example of the use of beforeInsert(), or beforeUpdate() for that matter?
The documentation says "This signal is emitted by insertRowIntoTable() before a new row is inserted into the currently active database table". So it sounds like the signal is only emitted when insertRowIntoTable() is called, and not for any other type of model edit.
Related
I'm writing an app in Python with the PySide library. I have a QTableWidget that gets updated about every second. The thing is, I want to be able to change the data manually, and I thought that if I could find out whether or not the user is changing the data in the cell, then I could just prevent the program from updating this cell. Otherwise I get "kicked out" of the cell at each update.
Is this a good idea? Should I try something else and why?
How can I achieve my goal?
Many thanks
EDIT :
I know there exists an itemChanged signal, but what I'd really like to know is if there is a way to tell when the user is writing a new value in the cell, in order not to kick them out while editing.
In Qt Document:
void QTableWidget::itemChanged(QTableWidgetItem * item)
This signal is emitted whenever the data of item has changed.
Hope this will help you.
Edit:
QTableWidget uses a default itemdelegate(QItemDelegate instance) which has createEditor method and closeEditor signal.
You can reimplement createEditor which means edit starts, and connect the signal closeEditor which means the edit ends.
This may be the correct way.
Generally, you would handle this situation with the use of QItemDelegates, which allow you to control what cells are editable by the user, what types of controls they are given to edit the cells, and you can catch the data they input and validate or manipulate it before saving it to the model.
QItemDelegates only control edits being made using the view interface. If the table is being updated programmatically, the changes won't be sent to the QItemDelegate.
Here is an example of a QItemDelegate for a QTableWidget
I'm building a Gtk app in Python (Lutris, available on Github) where I emit a custom signal in a part of the app and I would like to connect to that signal in a totally different part of the app where the receiver doesn't have a reference to the widget that emitted the signal.
I was looking for a way to have a signal where any widget of the application could connect to it even without a reference to the widget. Is this possible to do with GObject? Or do I need to use something else, maybe DBus?
After researching a bit more on the issue I found a solution that would work.
On the receiving widget, instead of connect, GObject.add_emission_hook can be used to have something like:
GObject.add_emission_hook(SenderType, 'signal-name', on_signal_callback)
Unlike conventional callbacks, this one must return True otherwise it will only work once.
I am building an app that, when the user hits a 'run' button, generates a table of buttons.
Because this process takes a while, I want to add a popup or progress bar to alert the user that the function is running and not frozen. To do this I decided to create a popup and call my function using threading so that the screen will be updated when the function starts (as opposed to once it is done).
mythread = threading.Thread(target=run_function)
mythread.start()
The trouble is that when I call my function from the above code it works very strangely: the columns of my table are the wrong width, some of my buttons are arbitrarily empty, and others have the wrong fill color. To fix this, all I need to do is to remove the threading operation and simply call run_function()
Any idea why this is happening?
I am new to Python, so it is likely some dumb mistake, but I have no idea. What is different between a process running as a thread and its default operation?
Disclaimer: I haven't worked with Kivy.
Not every framework works well with multithreading.
But most of the GUI frameworks have an event loop which is responsible for managing user events (mouse clicks, keyboard) and queued drawing operations (widgets).
In your case if don't want the UI to be freezed, you should regularly give control to your framework's event loop.
I guess kivy.base.EventLoopBase.dispatch_input is what you need to call to show an added widget or to handle user events.
I'm currently porting a small application from PyGTK to PySide which sits in your systray and periodically checks a server for updates.
When I initially wrote this, I used a custom state model to represent its behaviour:
init: initial state
check: check server for updates
disconnected: no connection to server (wait for some time to try again)
error: server reported an error (user needs to check settings before proceeding)
idle: wait for POLLING INTERVAL seconds before checking again
quit
I since discovered that Qt has QStateMachine, which seems perfect for this type of structure. However, I was not able to apply the examples satisfyingly to my problem!
In particular:
In the context of QStateMachine, what is the difference between Signal and QEvent?
How do I define a conditional transition, i.e. on error go to... ?
Should program logic happen in Transition.onTransition() or in QState.onEnter()?
Any pointers are appreciated muchly!
Ok. Pyside.QtCore.Signal is Signals & Slots derivative.
As for your questions
I'd say there is no difference in QEvent and Signal in context of QStateMachine (although QEvent and Signals & Slots are totally different concepts). Depending on your needs you can trigger transition with QEvent or Signal. See QAbstactTransition for the list of out of the box transitions:
Inherited by: QSignalTransition, QEventTransition,
QMouseEventTransition, QKeyEventTransition
Again depending on what happens inside your application your error may be either signal from QObject or you can send (post) custom QEvent. You'll need to implement your custom QEvent and/or custom QEventTransition to trigger transition only on your events.
And again it depends:) Transition is the glue. It has knowledge about source and destination states. So I'd put only preparatory code inside onTransition() and state initialization code inside onEnter(). Also it seems wrong for me to put code that changes state inside onTransition() like in example you've shown:
def onTransition(self, e):
x = e.arguments()[0]
fac = self.fact.fac
self.fact.fac = x * fac
self.fact.x = x - 1
but as you can see it works well.
NB: If you have iOS experience then UIStoryboardSegue is analogue for transition. Mainly it is used to pass data between UIView's i.e. UI states.
Hi in my application I have several gui developped with QT4.7, in some of them I use QToolButton for interaction, everything work nicely, except that when I click over each of the QToolButton, the slot linked to his triggered signal is called twice,
I have no more Ideas about a olution to that, can you help me please, thanks in advance
Thank you, I solved, when clicked the QtoolButton send two different triggered signal, then my slot ran twice, it was enough to add decorator before the definition of the slot
#pyqtSignature("")