Results 1 to 4 of 4

Thread: Display records in Sqlite Pyqt4

  1. #1
    Join Date
    Sep 2010
    Posts
    5
    Qt products
    Platforms
    Unix/X11

    Default Display records in Sqlite Pyqt4

    Hi all,

    I'm learning Pyqt4. What I would like to do is to display some records I have in an sqlite database in a TableView. I can't manage to do that although.

    Those are the codes I'm using:

    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. # Form implementation generated from reading ui file 'gui.ui'
    4. #
    5. # Created: Fri Sep 24 11:15:38 2010
    6. # by: PyQt4 UI code generator 4.7.2
    7. #
    8. # WARNING! All changes made in this file will be lost!
    9.  
    10. from PyQt4 import QtCore, QtGui
    11.  
    12. class Ui_MainWindow(object):
    13. def setupUi(self, MainWindow):
    14. MainWindow.setObjectName("MainWindow")
    15. MainWindow.resize(800, 600)
    16. self.centralwidget = QtGui.QWidget(MainWindow)
    17. self.centralwidget.setObjectName("centralwidget")
    18. self.lineEdit = QtGui.QLineEdit(self.centralwidget)
    19. self.lineEdit.setGeometry(QtCore.QRect(352, 16, 441, 91))
    20. self.lineEdit.setObjectName("lineEdit")
    21. self.pushButton = QtGui.QPushButton(self.centralwidget)
    22. self.pushButton.setGeometry(QtCore.QRect(260, 78, 80, 27))
    23. self.pushButton.setObjectName("pushButton")
    24. self.tableView = QtGui.QTableView(self.centralwidget)
    25. self.tableView.setGeometry(QtCore.QRect(2, 120, 791, 411))
    26. self.tableView.setObjectName("tableView")
    27. MainWindow.setCentralWidget(self.centralwidget)
    28. self.menubar = QtGui.QMenuBar(MainWindow)
    29. self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
    30. self.menubar.setObjectName("menubar")
    31. MainWindow.setMenuBar(self.menubar)
    32. self.statusbar = QtGui.QStatusBar(MainWindow)
    33. self.statusbar.setObjectName("statusbar")
    34. MainWindow.setStatusBar(self.statusbar)
    35. self.toolBar = QtGui.QToolBar(MainWindow)
    36. self.toolBar.setObjectName("toolBar")
    37. MainWindow.addToolBar(QtCore.Qt.ToolBarArea(QtCore.Qt.TopToolBarArea), self.toolBar)
    38.  
    39. self.retranslateUi(MainWindow)
    40. QtCore.QMetaObject.connectSlotsByName(MainWindow)
    41.  
    42. def retranslateUi(self, MainWindow):
    43. MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    44. self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Run", None, QtGui.QApplication.UnicodeUTF8))
    45. self.toolBar.setWindowTitle(QtGui.QApplication.translate("MainWindow", "toolBar", None, QtGui.QApplication.UnicodeUTF8))
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. """The user interface for our app"""
    2.  
    3. import os,sys
    4.  
    5. # Import Qt modules
    6. from PyQt4 import QtCore,QtGui
    7. from PyQt4 import QtSql
    8.  
    9. # Import the compiled UI module
    10. from windowUI import Ui_MainWindow
    11. #import PySqliteGUI
    12.  
    13. # Create a class for our main window
    14. class Main(QtGui.QMainWindow):
    15. def __init__(self):
    16. QtGui.QMainWindow.__init__(self)
    17.  
    18. # This is always the same
    19. self.ui=Ui_MainWindow()
    20. self.ui.setupUi(self)
    21.  
    22.  
    23. #My Code (Alessio)
    24.  
    25.  
    26. # table model
    27. # ------------------------------------------------
    28. db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
    29. db.setDatabaseName("/home/alessio/Dropbox/08_Trials/PySqliteQt/grundfos.sqlite")
    30. ok=db.open()
    31. self.model = QtSql.QSqlTableModel()
    32. self.model.setTable("GDK2009")
    33. self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
    34. self.model.select()
    35. print self.model.select()
    36.  
    37.  
    38. # column headers
    39. self.model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant("COLOR_COLOR"))
    40. self.model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant("ANIMAL_COLOR"))
    41. self.model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant("ANIMAL_NAME"))
    42. self.model.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant("ANIMAL_DESCRIPTION"))
    43.  
    44.  
    45. # table view
    46. # ------------------------------------------------
    47. self.tableView = QtGui.QTableView()
    48. self.tableView.setModel(self.model)
    49. self.tableView.setItemDelegate(QtSql.QSqlRelationalDelegate(self.tableView))
    50. self.tableView.setSelectionMode(QtGui.QTableView.SingleSelection)
    51. self.tableView.setSelectionBehavior(QtGui.QTableView.SelectRows)
    52. self.tableView.resizeColumnsToContents()
    53. self.tableView.horizontalHeader().setStretchLastSection(True)
    54.  
    55.  
    56.  
    57. db.close()
    58.  
    59.  
    60.  
    61. def main():
    62.  
    63. # Again, this is boilerplate, it's going to be the same on
    64. # almost every app you write
    65. app = QtGui.QApplication(sys.argv)
    66. window=Main()
    67. window.show()
    68. # It's exec_ because exec is a reserved word in Python
    69. sys.exit(app.exec_())
    70.  
    71. #windowUI.lineEdit.SetText('prova')
    72.  
    73. if __name__ == "__main__":
    74. main()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Display records in Sqlite Pyqt4

    I think you need to set the database in the model before using it?

    Edit: Hmm, no, it's automatically set.
    Can you see if the database could be opened?

  3. #3
    Join Date
    Sep 2010
    Posts
    5
    Qt products
    Platforms
    Unix/X11

    Default Re: Display records in Sqlite Pyqt4

    If I print ok it says true. I don't know other ways to check.

  4. #4
    Join Date
    Sep 2010
    Posts
    5
    Qt products
    Platforms
    Unix/X11

    Default Re: Display records in Sqlite Pyqt4

    Ok, I've been able to solve the problem.

    Now it does a really weird thing, it works for tables with a small amount of records, but it doesn't with bigger data sets (50000 records).

    Ideas why this is happening?

    By the way, the code is:

    Qt Code:
    1. import sys
    2. from PyQt4 import QtCore, QtGui, QtSql
    3. from tr01_form import Ui_MainWindow
    4.  
    5. class Database:
    6. def __init__(self, parent = None):
    7. self.data = QtSql.QSqlDatabase.addDatabase("QSQLITE")
    8. self.data.setDatabaseName("grundfos.sqlite")
    9. self.data.open()
    10.  
    11. class Model(QtSql.QSqlTableModel):
    12. def __init__(self, parent = None):
    13. super(Model, self).__init__(parent)
    14. self.setEditStrategy(QtSql.QSqlTableModel.OnRowChange)
    15. self.setTable("GDK2009")
    16. self.select()
    17.  
    18. class Main(QtGui.QMainWindow):
    19. def __init__(self):
    20. QtGui.QMainWindow.__init__(self)
    21.  
    22. self.db = Database()
    23. self.model = Model(self)
    24. self.ui = Ui_MainWindow()
    25. self.ui.setupUi(self)
    26. self.ui.tableView.setModel(self.model)
    27. self.ui.tableView.showColumn(1)
    28.  
    29. for n in range(45764):
    30. n=n+10
    31. self.ui.tableView.setRowHidden(n, True)
    32. #self.ui.tableView.setColumnHidden(3, True)
    33. #self.ui.tableView.rowAt(2)
    34.  
    35.  
    36.  
    37. def main():
    38. app = QtGui.QApplication(sys.argv)
    39. window = Main()
    40. window.show()
    41. sys.exit(app.exec_())
    42.  
    43. if __name__ == "__main__":
    44. main()
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Updating QAbstractItemModel with new records
    By psih128 in forum Qt Programming
    Replies: 0
    Last Post: 10th May 2010, 01:02
  2. Replies: 2
    Last Post: 25th March 2010, 13:20
  3. SQLite - check if any records are returned
    By amicitas in forum Newbie
    Replies: 1
    Last Post: 28th September 2008, 10:28
  4. QTableWidget Sql Query 25.000 records
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2008, 14:54
  5. Combobox Delegate 25.000 records
    By aekilic in forum Qt Programming
    Replies: 9
    Last Post: 29th July 2008, 12:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.