[PyQt] QTableView as pick list
Dave Kelly
dave at theprioryinn.co.uk
Tue Dec 1 14:41:17 GMT 2015
I am trying to write a data driven application in PYQT4 with PostgreSQL.
This is new to me but it seems like it should be straightforward. I am sure
that this is a newbie question but I can't seem to find the answer from my
friend Google.
My problem is I have set up a QtableView which populates fine and looks
good. Column 2 contains a primary key for a record. In the manor of a
classic pick list, I would like the user to click on it and open a form to
edit the record. I can't seem to figure out how to get the value in the
hidden column of the clicked on row to use in the WHERE clause to set the
model for the form to open. Code is below.
Thanks for any help!
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import sys
from ci_co_table import *
from guest_info_form import *
class Ci_Co(QMainWindow):
"""Check in and check out module"""
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
global hotdate
qry_params = QSqlQuery()
qry_params.exec_("select param_date from params where id = 1")
param_date = 0
while qry_params.next():
hotdate = unicode(qry_params.value(param_date).toString())
print(hotdate)
self.ui.hotdate_lbl.setText(hotdate)
self.populate_mainform()
def populate_mainform(self):
arrivals_model = QSqlQueryModel()
arrivals_model.setQuery("select rooms.room, bookings.label,
bookings.booking_id from bookings LEFT JOIN rooms ON bookings.room_id =
rooms.hls_code where arrival_date = '{0}' and checked_in is Null and
cancelled is Null order by to_number(rooms.room, text(99))".format(hotdate))
self.arrivals_view = self.ui.arrivals
self.arrivals_view.setModel(arrivals_model)
self.arrivals_view.setColumnHidden(2, True)
self.arrivals_view.setSelectionBehavior(QTableView.SelectRows)
self.arrivals_view.setSortingEnabled(True)
QObject.connect(self.arrivals_view, SIGNAL("clicked(QModelIndex)"),
self.ci_clicked)
def ci_clicked(self, QModelIndex):
self.gst_form = Guest_form(self)
self.gst_form.show()
guest_model = QSqlQueryModel()
guest_model.setQuery("select * from bookings LEFT JOIN rooms ON
bookings.room_id = rooms.hls_code WHERE id = '{0}'".format(""))
#map widgets here
class Guest_form(QDialog, Ui_guest_info_form):
def __init__(self, parent=None):
#super(Guest_form, self).__init__(parent)
QDialog.__init__(self, parent)
self.setupUi(self)
if __name__=="__main__":
app=QtGui.QApplication(sys.argv)
#Database stuff
db = QSqlDatabase.addDatabase("QPSQL");
db.setHostName("my_host_name")
db.setDatabaseName("my_db");
db.setUserName("me");
db.setPassword("my_password");
if (db.open()==False):
QMessageBox.critical(None, "Database Error", db.lastError().text())
myapp = Ci_Co()
myapp.show()
sys.exit(app.exec_())
Dave Kelly
The Priory Inn <http://theprioryinn.co.uk/>
01666 502 251
London Road, Tetbury, GL8 8JJ
"Dining in a 30 mile food zone"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20151201/d618f613/attachment.html>
More information about the PyQt
mailing list