[SOLVED]PyQT and QTableView

  • Hey everyone, and thank you for reading this in advance.

    I have just started my first "serious" QT project. Here is the setup; I have "drawn" a GUI with the QT Designer, made .py file out of it. Basically, GUI is working fine. In that GUI, I made a QTableView (model based) which is named "tabela".

    On the other hand, I have a small sqlite database containing few names and numbers. What would be a correct way to display that SQLite data into that QListView?

    I have searched the web for a few days, and only thing I got is "this thread":http://stackoverflow.com/questions/8923562/getting-data-from-selected-item-in-qlistview on Stackoverflow. It works by itself, but refuses to work with my custom GUI.

    Thank you for reading, and excuse my english.

  • You'll need to instantiate a QSqlTableModel (or QSqlQueryModel, depending on your exact needs) and set that as the model for the view. But that is basically what's in that tread as well.

    You'll have to be more clear on what you tried exactly, and what didn't work.

  • I would like to use QSqlQueryModel at first. My simplified question is:

    how can I "instruct" the view to be shown in my object called "tabela". It already exists in the gui.py file

    @self.tabela = QtGui.QTableView(self.registar)
    self.tabela.setGeometry(QtCore.QRect(80, 50, 431, 192))

    this is the snippet from main.py

    @class Prozor(QtGui.QMainWindow):
    def init(self,parent=None):
    self.model = QSqlQueryModel(self)#I tried putting it in constructor

    QtCore.QObject.connect(self.ui.dugmeBaza, QtCore.SIGNAL("clicked()"),self.baza)
    #my signal button conected with a function called "baza"

    def baza(self):
    db = QSqlDatabase.addDatabase("QSQLITE")

    self.model.setQuery("select * from predmeti",db)

    #view = QtGui.QTableView() this is what the code looked like
    viev = self.ui.tabela #i changed it to this, and now it works!



    IT WORKS :) I have marked the changes. Still, any advice for my coding? I am at beginner level.

  • Sorry, I don't know much about Python. However, very general:
    If you know English, I find it easier to stick to that for naming my objects and methods. All the objects from the API are named in that language. Using two languages inside a single 'sentence' or 'paragraph' (piece of code) doesn't help understanding, generally speaking. It also makes it easier to get help online (people reading your posts can make sense of your object names, and thus perhaps of what you are trying to do) and even collaborators (in open source projects).

    I once had to reject using a very interesting looking library, because I could not make heads or tails of the API: all object and method names (and documentation) were in Russian.

  • Thank you for your observation and general promptness to answer my question. In the future, I will translate my code to English for forums ;) In general, I find my native language (Serbian) more descriptive, and it helps me distinguish "code" from "object names".

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.