[PyQt] QTableView

lucaberto at libero.it lucaberto at libero.it
Tue Dec 23 10:08:05 GMT 2008


Hello i have made a connection to a db but in my table nothing is shoe, also not the horizontal header why?

# -*- coding: utf-8 -*-

"""
Module implementing MainWindow.
"""

from PyQt4.QtGui import QMainWindow
from PyQt4.QtCore import pyqtSignature
from PyQt4 import *
from PyQt4.QtSql import *
from PyQt4.QtCore import *
from PyQt4 import Qt
import sip
import sys
app = QtGui.QApplication(sys.argv)

from Ui_studio import Ui_MainWindow

class MainWindow(QMainWindow, Ui_MainWindow):
    """
    Class documentation goes here.
    """
    def __init__(self, parent = None):
        """
        Constructor
        """
        QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.db = QSqlDatabase.addDatabase('QMYSQL')
        self.db.setHostName('localhost')
        self.db.setDatabaseName('studio')
        self.db.setUserName('luca72')
        self.db.setPassword('lucajacopotania')
        self.db.setPort(3306)
        self.db.open()
        if not self.db.open():
            QtGui.QMessageBox.warning(None, "Asset Manager",QString("Database Error: %1").arg(self.db.lastError().text()))
            sys.exit(1)
        else :
            self.mio_modello = QSqlTableModel()
            self.mio_modello.setTable('mao')
            self.mio_modello.setEditStrategy(QSqlTableModel.OnManualSubmit)
            #self.mio_modello.select()
            self.mio_modello.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant(QtCore.QObject.tr(self.mio_modello, 'ID')))
            self.mio_modello.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant(QtCore.QObject.tr(self.mio_modello, 'ORARIO')))
            self.mio_modello.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant(QtCore.QObject.tr(self.mio_modello, 'DATA')))
            self.mio_modello.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant(QtCore.QObject.tr(self.mio_modello, 'DOTTORE')))
            self.mio_modello.setHeaderData(4, QtCore.Qt.Horizontal, QtCore.QVariant(QtCore.QObject.tr(self.mio_modello, 'PAZIENTE')))
            self.mio_modello.setHeaderData(5, QtCore.Qt.Horizontal, QtCore.QVariant(QtCore.QObject.tr(self.mio_modello, 'DESCRIZIONE')))
            orari = ['08:00', '08:30', '09:00', '09:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00', '18:30', '19:00', '19:30', '20:00', '20:30', '21:00']
            for a in range(0, 27):
                self.mio_modello.setHeaderData(a, QtCore.Qt.Vertical, QVariant(orari[a]))
            self.mio_modello.select()
            self.mia_tabella = QtGui.QTableView()
            self.mia_tabella.setModel(self.mio_modello)
            self.mia_tabella.setSelectionMode(QtGui.QTableView.SingleSelection)
            self.mia_tabella.setSelectionBehavior(QtGui.QTableView.SelectRows)
            self.mia_tabella.resizeColumnsToContents()

Thanks Luca




More information about the PyQt mailing list