Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to call function with arguments using QThread [PySide6]
Forum Update on Tuesday, May 27th 2025

How to call function with arguments using QThread [PySide6]

Scheduled Pinned Locked Moved Unsolved Qt for Python
pysideqt for pythonpython
3 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andresfe
    wrote on last edited by
    #1

    Is there any way to call QThread::create using PySide6? I want to call a function which uses some arguments in a new Thread. Is subclassing QThread the only way to do this right know?
    Something like this:

    class Threader(QThread):
        def __init__(self, func, parent=None):
            super().__init__(parent)
            self.func = func
            self.arg = None
                      
            def run(self):
                self.func(self.arg)
    

    Then calling threader.arg before calling threader.start(). I feel like there has to be another way.

    Thanks!

    JonBJ 1 Reply Last reply
    0
    • A Andresfe

      Is there any way to call QThread::create using PySide6? I want to call a function which uses some arguments in a new Thread. Is subclassing QThread the only way to do this right know?
      Something like this:

      class Threader(QThread):
          def __init__(self, func, parent=None):
              super().__init__(parent)
              self.func = func
              self.arg = None
                        
              def run(self):
                  self.func(self.arg)
      

      Then calling threader.arg before calling threader.start(). I feel like there has to be another way.

      Thanks!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Andresfe
      I do not know about Qt6 and/or PySide6. But at Qt 5.10 this was introduced:

      static template <typename Function, typename Args> QThread *QThread::create(Function &&f, Args &&... args)

      to allow just that --- the passing of arbitrary arguments to a created QThread. Can you access that?

      [EDIT: Hmm, I see it is missing at https://doc.qt.io/qtforpython/PySide6/QtCore/QThread.html#static-functions]

      Otherwise before that I think one had to do just as you say --- sub-class and store arguments for run() (e.g. as per accepted solutuon at https://stackoverflow.com/a/7864748/489865).

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        I would ask this question on the PySide mailing list. The method is also not available in PySide2 as it seems.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved