Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Connecting MySql Database to PySide/QT QTableView?
Forum Updated to NodeBB v4.3 + New Features

Connecting MySql Database to PySide/QT QTableView?

Scheduled Pinned Locked Moved Language Bindings
8 Posts 2 Posters 8.5k 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.
  • J Offline
    J Offline
    jdimmock
    wrote on last edited by
    #1

    I've been racking my brain trying to figure this out and think I am missing something fundamental.
    I have a UI made with QT Designer that I'm trying to connect to a MySQL database with PySide. I can already populate the database with Python, but can't figure out how to hook the QTableView object (name 'chart_tableview') into my database ('testdb').

    How do I connect that to the Python script and how can I populate it with the database cells?

    Thanks!

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

      Hi and welcome to devnet,

      Did you take a look at the "QtSql module":http://qt-project.org/doc/qt-4.8/qtsql.html ?

      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
      • J Offline
        J Offline
        jdimmock
        wrote on last edited by
        #3

        Thanks!
        I have tried a few examples I could find online using QtSql, but couldn't get it to work right within Python. I'm new to using PySide & QT, so I feel like I'm missing something that's easy, but I couldn't find anything online that worked right...

        Going through the QSQLDatabase page, it appears that to connect to the database with QT I'd use
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost");
        db.setDatabaseName("testdb");
        db.setUserName("username");
        db.setPassword("password");
        bool ok = db.open();

        how would I then link the QListView to a SQL query in that database?

        Thanks again!

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

          Did you take a look at the module examples ?

          Also check QSqlTableModel

          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
          • J Offline
            J Offline
            jdimmock
            wrote on last edited by
            #5

            Those two pages helped A LOT!

            I haven't gotten it quite working, but I feel like I'm close. I put the following into the script:

            @db = QtSql.QSqlDatabase.addDatabase("QMYSQL");
            db.setHostName("localhost");
            db.setDatabaseName("testdb");
            db.setUserName("testuser");
            db.setPassword("dbpassword");
            db.open();

            model = QtSql.QSqlTableModel()
            model.setTable("Writers")
            model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
            model.select()
            model.removeColumn(0) # don't show the ID
            model.setHeaderData(0, QtCore.Qt.Horizontal, "Header 1")
            model.setHeaderData(1, QtCore.Qt.Horizontal, "Header 2")

            view = QTableView()
            view.setModel(model)
            view.show()@

            I've gotten it down to the last error I believe:
            @ model.setHeaderData(0, QtCore.Qt.Horizontal, "Name")
            TypeError: 'PySide.QtSql.QSqlQueryModel.setHeaderData' called with wrong argument types:
            PySide.QtSql.QSqlQueryModel.setHeaderData(int, Orientation, str)
            Supported signatures:
            PySide.QtSql.QSqlQueryModel.setHeaderData(int, PySide.QtCore.Qt.Orientation, QVariant, int = Qt.EditRole)@

            If I comment out the setHeaderData lines, nothing populates in the desired QTableView (or anywhere for that matter). I'm still figuring out where to put the 'chart_listview' object name in the code.

            Thank you for steering me in the right direction!

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

              You should hide column 0 on the view, removing it in the model will remove the content of the database.

              It seems that your string is not converted, either add the tr() call (it's always good to have your code ready for translation) or QLatin1String()

              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
              • J Offline
                J Offline
                jdimmock
                wrote on last edited by
                #7

                I couldn't find a PySide hideColumn() equivalent for QSqlTableModel, only for vanilla QT-- is it named something else? Either way, I've commented it out for now until I can get the db hooked into the GUI.

                I got the error resolved from my last post-- so it's down to connecting the QSqlTableModel to the QT Designer file. It looks like to do that I'll need to use the findChild() function, correct?
                I'll toil for a bit before I ask for more help-- either way I'll post what I figure out here.

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

                  It's not on the model, it's on the QTableView.

                  You have to code the connection yourself e.g. in your widget constructor

                  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