Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7661193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:33:56+00:00 2026-05-31T13:33:56+00:00

I decided to add a GUI to one of my scripts. The script is

  • 0

I decided to add a GUI to one of my scripts. The script is a simple web scraper. I decided to use a worker thread as downloading and parsing the data can take a while. I decided to use PySide, but my knowledge of Qt in general is quite limited.

As the script is supposed to wait for user input upon coming across a captcha I decided it should wait until a QLineEdit fires returnPressed and then send it’s content to the worker thread so it can send it for validation. That should be better than busy-waiting for the return key to be pressed.

It seems that waiting for a signal isn’t as straight forward as I thought it would be and after searching for a while I came across several solutions similar to this. Signaling across threads and a local event loop in the worker thread make my solution a bit more complicated though.

After tinkering with it for several hours it still won’t work.

What is supposed to happen:

  • Download data until refered to captcha and enter a loop
  • Download captcha and display it to the user, start QEventLoop by calling self.loop.exec_()
  • Exit QEventLoop by calling loop.quit() in a worker threads slot which is connected via self.line_edit.returnPressed.connect(self.worker.stop_waiting) in the main_window class
  • Validate captcha and loop if validation fails, otherwise retry the last url which should be downloadable now, then move on with the next url

What happens:

  • …see above…

  • Exiting QEventLoop doesn’t work. self.loop.isRunning() returns False after calling its exit(). self.isRunning returns True, as such the thread didn’t seem to die under odd circumstances. Still the thread halts at the self.loop.exec_() line. As such the thread is stuck executing the event loop even though the event loop tells me it is not running anymore.

  • The GUI responds as do the slots of the worker thread class. I can see the text beeing send to the worker thread, the status of the event loop and the thread itself, but nothing after the above mentioned line gets executed.

The code is a bit convoluted, as such I add a bit of pseudo-code-python-mix leaving out the unimportant:

class MainWindow(...):
    # couldn't find a way to send the text with the returnPressed signal, so I
    # added a helper signal, seems to work though. Doesn't work in the
    # constructor, might be a PySide bug?
    helper_signal = PySide.QtCore.Signal(str)
    def __init__(self):
        # ...setup...
        self.worker = WorkerThread()
        self.line_edit.returnPressed.connect(self.helper_slot)
        self.helper_signal.connect(self.worker.stop_waiting)

    @PySide.QtCore.Slot()
    def helper_slot(self):
        self.helper_signal.emit(self.line_edit.text())

class WorkerThread(PySide.QtCore.QThread):
    wait_for_input = PySide.QtCore.QEventLoop()

    def run(self):
        # ...download stuff...
        for url in list_of_stuff:
            self.results.append(get(url))

    @PySide.QtCore.Slot(str)
    def stop_waiting(self, text):
        self.solution = text
        # this definitely gets executed upon pressing return
        self.wait_for_input.exit()

    # a wrapper for requests.get to handle captcha
    def get(self, *args, **kwargs):
        result = requests.get(*args, **kwargs)
        while result.history: # redirect means captcha
            # ...parse and extract captcha...
            # ...display captcha to user via not shown signals to main thread...

            # wait until stop_waiting stops this event loop and as such the user
            # has entered something as a solution
            self.wait_for_input.exec_()

            # ...this part never get's executed, unless I remove the event
            # loop...

            post = { # ...whatever data necessary plus solution... }
            # send the solution
            result = requests.post('http://foo.foo/captcha_url'), data=post)
        # no captcha was there, return result
        return result

frame = MainWindow()
frame.show()
frame.worker.start()
app.exec_()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:33:57+00:00Added an answer on May 31, 2026 at 1:33 pm

    The slot is executed inside the thread which created the QThread, and not in the thread that the QThread controls.

    You need to move a QObject to the thread and connect its slot to the signal, and that slot will be executed inside the thread:

    class SignalReceiver(QtCore.QObject):
        def __init__(self):
            self.eventLoop = QEventLoop(self)             
    
        @PySide.QtCore.Slot(str)
        def stop_waiting(self, text):                   
            self.text = text
            eventLoop.exit()
    
        def wait_for_input(self):
            eventLoop.exec()
            return self.text
    
    class MainWindow(...):
         ...
         def __init__(self):
            ...
            self.helper_signal.connect(self.worker.signalReceiver.stop_waiting)
    
    class WorkerThread(PySide.QtCore.QThread): 
        def __init__(self):
            self.signalReceiver = SignalReceiver() 
            # After the following call the slots will be executed in the thread             
            self.signalReceiver.moveToThread(self)    
    
        def get(self,  *args, **kwargs):
            result = requests.get(*args, **kwargs)
            while result.history:
                ...
                self.result = self.signalReceiver.wait_for_input()   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To allow users and site admins to view/add/edit/delete data in my application I decided
I decided to add unit tests to existing project (quite big one). I am
I'm using eclipse Indigo and I'm making GUI, I've decided to use MigLayout cause
My web app seems to be working fine. I decided to add some logging.
I've decided to add an NSNumber attribute to one of my entities in core
Last night I decided to add HTML fragment links in my computer listing page.
I'm trying to wrap my head around reflection, so I decided to add plugin
After developing a WPF application without Source Control, I decided to add the solution
My last company, which used 4.01 DOCTYPE exclusively, decided to add some new functionality
I'm trying to add the facebook connect feature to my site, I decided to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.