Show QDialog and execute code after

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Show QDialog and execute code after

starglider.dev
Hi,
I need after the QDialog is show, to run code and display the output to a QTextEdit.
Now I can only run the code with the QDialog with user input -a QPushButton-
If the code runs inside the
def __init__(self, parent=None):

the QDialog is not shown until it finish the code execution.

I'm using Python 2.7 and PyQt4.

Thank you in advance.

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Show QDialog and execute code after

Kyle Altendorf
On 2018-04-13 07:00, Starglider Developer wrote:

> Hi, I need after the QDialog is show, to run code and display the
> output to a QTextEdit. Now I can only run the code with the QDialog
> with user input -a QPushButton- If the code runs inside the
> def __init__(self, parent=None):

It helps to see the full code.  Also, it's often good to be able to
construct objects without them actually doing things and have the
activities in a separate method.

> the QDialog is not shown until it finish the code execution.

I'll guess you are using QDialog.exec() which blocks until the user
closes the dialog.

http://doc.qt.io/qt-5/qdialog.html#exec

The .show() call doesn't block but you will have to get the result of
the dialog via it's signals or the .result() method.

http://doc.qt.io/qt-5/qwidget.html#show
http://doc.qt.io/qt-5/qdialog.html#result

Or, perhaps you can just move the other code so it is before the .exec()
call for the dialog?  It really depends what all you have going on.

Cheers,
-kyle
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Show QDialog and execute code after

Ricardo Aráoz
In reply to this post by starglider.dev
On 13/04/18 08:00, Starglider Developer wrote:
Hi,
I need after the QDialog is show, to run code and display the output to a QTextEdit.
Now I can only run the code with the QDialog with user input -a QPushButton-
If the code runs inside the
def __init__(self, parent=None):

the QDialog is not shown until it finish the code execution.

I'm using Python 2.7 and PyQt4.

Thank you in advance.


You might try putting the slow code inside a method, then in the __init__() method you instantiate your QTextEdit without data, then use QTimer.singleShot(0, self.yourSlowCode) inside which you'll be actualizing the QTextEdit as you have the data available. I think the singleShot will execute yourSlowCode() as soon as the window's event queue is processed, that means the widget will appear in the screen and take user input. Worth a try.
Check : http://doc.qt.io/qt-5/qtimer.html look for "special case" inside the "Detailed Description".




_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Show QDialog and execute code after

Maurizio Berti
In reply to this post by starglider.dev
A little bit of context/code might help, at least to understand why do you need to run that code and display the output *after* the dialog is shown, and why do you need to do that when the dialog is shown. Also, it is not clear if the code you run takes a lot of time/resources, which might or might not block the UI.

Anyway, in similar cases (when I need to actually show the dialog and do something on/with the interface) I prefer to implement the exec_() method and return the actual exec_() class method at the end.
In this way I don't need to mess around with modality, results and so on, leaving the behavior almost intact. Also, you can set custom arguments to your exec_() implementation, which might come handy.

class MyDialog(QtWidgets.QDialog):
    ...
    def exec_(self):
        self.show()
        data = self.someMethod(someArgument)
        ...
        self.myTextEdit.setPlainText(processedData)
        res = QtWidgets.QDialog.exec_(self)
        if res:
            #you can do something with the result of the exec_()
            if res == self.Yes:
                self.doSomething()
        return res

Obviously, if the code you need to run there requires too many resources, you might think about using a QObject moved to a new QThread and use custom signals to update the data.


Regards,
MaurizioB

2018-04-13 13:00 GMT+02:00 Starglider Developer <[hidden email]>:
Hi,
I need after the QDialog is show, to run code and display the output to a QTextEdit.
Now I can only run the code with the QDialog with user input -a QPushButton-
If the code runs inside the
def __init__(self, parent=None):

the QDialog is not shown until it finish the code execution.

I'm using Python 2.7 and PyQt4.

Thank you in advance.

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt



--
È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Show QDialog and execute code after

starglider.dev

Thank you for your answers.

I making an update app that shows a QTextEdit object and starts (should) start to get information from the system before
ask the user what to do, it does that but if now but only after the function get all the system info and the it draws the QDialog.

It's something like a Debian apt-get where the terminal is showing  what is appending before and after each operation.
 


_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Show QDialog and execute code after

Maurizio Berti
> I making an update app that shows a QTextEdit object and starts (should) start to get information from the system before
> ask the user what to do, it does that but if now but only after the function get all the system info and the it draws the QDialog.
>
> It's something like a Debian apt-get where the terminal is showing  what is appending before and after each operation.

If that's the case, you should use a separate QObject that collects the output of apt-get and emits a signal every time it has something to show. That QObject has to be moved to a new QThread, so that the apt-get process will not block the GUI interaction and painting.

Regards,
Maurizio

--
È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Show QDialog and execute code after

Kyle Altendorf


On April 14, 2018 2:01:36 PM EDT, Maurizio Berti <[hidden email]> wrote:

>> I making an update app that shows a QTextEdit object and starts
>(should)
>start to get information from the system before
>> ask the user what to do, it does that but if now but only after the
>function get all the system info and the it draws the QDialog.
>>
>> It's something like a Debian apt-get where the terminal is showing
>what
>is appending before and after each operation.
>
>If that's the case, you should use a separate QObject that collects the
>output of apt-get and emits a signal every time it has something to
>show.
>That QObject has to be moved to a new QThread, so that the apt-get
>process
>will not block the GUI interaction and painting.

Or avoid threads and use QProcess if it is actually a case of subprocessing. The good answer really depends on the actual long running activity. Could be processEvents() or QProcess or even Twisted or such (or occasionally threading).

Cheers,
-kyle
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Show QDialog and execute code after

Ricardo Aráoz
On 14/04/18 15:46, Kyle Altendorf wrote:

> On April 14, 2018 2:01:36 PM EDT, Maurizio Berti <[hidden email]> wrote:
>>> I making an update app that shows a QTextEdit object and starts
>> (should)
>> start to get information from the system before
>>> ask the user what to do, it does that but if now but only after the
>> function get all the system info and the it draws the QDialog.
>>> It's something like a Debian apt-get where the terminal is showing
>> what
>> is appending before and after each operation.
>>
>> If that's the case, you should use a separate QObject that collects the
>> output of apt-get and emits a signal every time it has something to
>> show.
>> That QObject has to be moved to a new QThread, so that the apt-get
>> process
>> will not block the GUI interaction and painting.
> Or avoid threads and use QProcess if it is actually a case of subprocessing. The good answer really depends on the actual long running activity. Could be processEvents() or QProcess or even Twisted or such (or occasionally threading).
>
> Cheers,
> -kyle
>

Agree, the easiest technique that produces the desired result.

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Show QDialog and execute code after

Barry Scott


> On 14 Apr 2018, at 20:46, Ricardo Araoz <[hidden email]> wrote:
>
>> On 14/04/18 15:46, Kyle Altendorf wrote:
>> On April 14, 2018 2:01:36 PM EDT, Maurizio Berti <[hidden email]> wrote:
>>>> I making an update app that shows a QTextEdit object and starts
>>> (should)
>>> start to get information from the system before
>>>> ask the user what to do, it does that but if now but only after the
>>> function get all the system info and the it draws the QDialog.
>>>> It's something like a Debian apt-get where the terminal is showing
>>> what
>>> is appending before and after each operation.
>>>
>>> If that's the case, you should use a separate QObject that collects the
>>> output of apt-get and emits a signal every time it has something to
>>> show.
>>> That QObject has to be moved to a new QThread, so that the apt-get
>>> process
>>> will not block the GUI interaction and painting.

Is there any advantage to using QThread over python threads?

Barry


>>
>> Cheers,
>> -kyle
>>
>
> Agree, the easiest technique that produces the desired result.
>
> _______________________________________________
> PyQt mailing list    [hidden email]
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Show QDialog and execute code after

Kyle Altendorf


On April 15, 2018 3:55:14 AM EDT, Barry <[hidden email]> wrote:

>
>
>> On 14 Apr 2018, at 20:46, Ricardo Araoz <[hidden email]> wrote:
>>
>>> On 14/04/18 15:46, Kyle Altendorf wrote:
>>> On April 14, 2018 2:01:36 PM EDT, Maurizio Berti
><[hidden email]> wrote:
>>>>> I making an update app that shows a QTextEdit object and starts
>>>> (should)
>>>> start to get information from the system before
>>>>> ask the user what to do, it does that but if now but only after
>the
>>>> function get all the system info and the it draws the QDialog.
>>>>> It's something like a Debian apt-get where the terminal is showing
>>>> what
>>>> is appending before and after each operation.
>>>>
>>>> If that's the case, you should use a separate QObject that collects
>the
>>>> output of apt-get and emits a signal every time it has something to
>>>> show.
>>>> That QObject has to be moved to a new QThread, so that the apt-get
>>>> process
>>>> will not block the GUI interaction and painting.
>
>Is there any advantage to using QThread over python threads?

They get you an event loop in the thread (so long as you use them properly for that) which is required to receive signals (along with not blocking the event loop, just as you shouldn't in the main thread).

Mostly I look for options other than threads. There are various for various needs between Qt and Python. This includes async/inlineCallbacks functions which allow you to write async code without breaking it up and connecting with signals and slots which is a pain when sequencing stuff.

Cheers,
-kyle
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Show QDialog and execute code after

Barry Scott


On 15 Apr 2018, at 13:20, Kyle Altendorf <[hidden email]> wrote:



On April 15, 2018 3:55:14 AM EDT, Barry <[hidden email]> wrote:


On 14 Apr 2018, at 20:46, Ricardo Araoz <[hidden email]> wrote:

On 14/04/18 15:46, Kyle Altendorf wrote:
On April 14, 2018 2:01:36 PM EDT, Maurizio Berti
<[hidden email]> wrote:
I making an update app that shows a QTextEdit object and starts
(should)
start to get information from the system before
ask the user what to do, it does that but if now but only after
the
function get all the system info and the it draws the QDialog.
It's something like a Debian apt-get where the terminal is showing
what
is appending before and after each operation.

If that's the case, you should use a separate QObject that collects
the
output of apt-get and emits a signal every time it has something to
show.
That QObject has to be moved to a new QThread, so that the apt-get
process
will not block the GUI interaction and painting.

Is there any advantage to using QThread over python threads?

They get you an event loop in the thread (so long as you use them properly for that) which is required to receive signals (along with not blocking the event loop, just as you shouldn't in the main thread).

That is useful to know. I have always been sending signals from the background thread and not thought of going the other way.


Mostly I look for options other than threads. There are various for various needs between Qt and Python. This includes async/inlineCallbacks functions which allow you to write async code without breaking it up and connecting with signals and slots which is a pain when sequencing stuff.

Threads has it drawbacks as does async. As with all things pick the best solution from your available design choices.

Barry


_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt