jeudi 28 avril 2016

Share SQlite database between PyQT5 windows and dialogs

I'm making an app for my final dissertation. This app imports an excel spreadhseet into a sqlite database and works with it. As I did not intend to make a GUI I used the sqlite3 library from python and made a huge library to handle it. But now that I've made a GUI in PyQT 5 I'm running into problems.

First of all, quick question, I dont know if this is the right way to handle dialogs. The way i have them now is:

class Main_window(QtWidgets.QMainWindow, main_window.Ui_MainWindow):
    def __init__(self....):
        ...
    def showresultswindow(self):
        if results_window != None:
             results_window = Results_Window(self)

class Results_window(QtWidgets.QMainWindow, results_window.Ui_MainWindow):
    def __init__(self, parent):
        ...

It works, dialogs open and work as intended when they only have to read the database. But now I'm making a dialog to write to the database and I can't get it to work.

The app's main window has two QSqlTableModels showing the database. In order to write data to the database, what I do is:

  • The main window class closes the database that was being used with the QtSql library
  • The main window calls a function that opens the database with sqlite3, writes whatever it has to write, and then closes it
  • When the function finishes, the main window class re-opens the database and fills the QSqlTableModels again with the new data.

But when I make the dialog to write data, I can't follow that proccess. As the dialog opens in another thread, if I close the database, create the dialog and then open it the database will be locked by the time the dialog is accepted and data has to be written.

I've thought of making a while loop that keeps running as long as the dialog is open but it hangs the software (infinite while loops tend to do so).

I know the right way of doing the app would be using the qtsql library to handle all the database inputs and outputs, but I'd have to rewrite a long library and I prefer trying another option before doing so. And even doing so I wouldn't know what to do:

  • Pass the QSqlQuery object to the dialog and work with it?
  • Pass the QSqlDatabase object to the dialog?

Thanks a lot for the help!

Aucun commentaire:

Enregistrer un commentaire