Domanda

I have a program mainly composed of a QMainWindow. But I added some kind of "plugins", which do some particular things. One of them for example parses thepiratebay to return the latest torrents of a category and put them in a database:

def parse(self):
    bdd = sqlite3.connect("fichiers.sqlite")
    bdd.row_factory = sqlite3.Row

    c = bdd.cursor() # obtention d'un curseur
    c.execute("SELECT * FROM pirate ORDER BY id ASC LIMIT 1")

    try:
        last_title = c.fetchone()['title']
        print("Dernier titre: " + last_title)
    except TypeError:
        last_title = ""

    search = self.t.search('*', category=CATEGORIES.VIDEOS.MOVIES)

    go = True
    j = 0
    for i in range(0, 34):
        print(i)
        search.order(ORDERS.UPLOADED.DES).page(i)

        for torrent in search:
            if torrent.title == last_title:
                print("on sort")
                go = False
                break

            print(torrent.title)
            j += 1
            c.execute("INSERT INTO pirate(title, title_simple, user, magnet_link, \
                       url, created, size) VALUES (?, ?, ?, ?, ?, ?, ?)", \
                       (torrent.title, simpleChar(torrent.title), torrent.user, torrent.magnet_link,
                        str(torrent.url), torrent.created, strByteToOctet(torrent.size)[1]))

        if not go:
            break

    print(j)
    bdd.commit()
    c.close()
    bdd.close()

    self.modele.setTable("pirate")
    self.modele.select()

But when I do that in my Qt program, the rest of the program waits until the function finishes the parsing. The API I use sends requests to the website, so it is sometimes a bit long. So the question is:

How do I parse without waiting the end of this function? I would like the Qt program to start that in a thread, without blocking the rest.

È stato utile?

Soluzione

As you noted yourself, using a custom thread based on QThread requires two things initially:

1) Inheriting from QThread.

2) You need to reimplement the virtual protected run method that the start method will call based on the template method pattern.

That is pretty much it for starter, and then you can start your thread. Once, you go further, you may need to deal with thread pools and so forth, but that is for later.strong text

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top