Hi,
I'm a newbie but so far I have been able to compile PythonQt (I had
help) into a Qt 5.1 C++ app. I'm able to create Qt dialogs etc.. all
without data.
I was also able to import psycopg2 (module to access Postgres) and the
data is retrieved but I can't get the data into a Qt Dialog/Widget (i.e.
QTableWidget). I'd like a little help if you have time. BTW: this is
going to great if I can get it running with data. Thanks in advance - Johnf
the code:
from PythonQt.QtGui import
from PythonQt.QtCore import
import psycopg2
import psycopg2.extras
import pprint
class ItemTypes(object):
pass
class TableWidget(QTableWidget):
def init(self, parent):
QTableWidget.init(self, parent)
self.initData()
def initData(self):
conn_string = "host='192.168.1.133' port = '5433'
for i in range(0,rows):
for j in range(0,cols):
item = QTableWidgetItem(1)
#item.setText(unicode(ds[i][colNames[j]]))
item.setText("Test")
#item.setVisible(true)
self.setItem(i,j,item)
print "i: ", i, " j: ", j, " ", self.item(i,j).text()
Since PythonQt does not handle the ownership transfer to C++, you have to keep all your items in an extra Python list which lives as long as you want to show the data in the table. PySide and PyQt both handle ownership passing for C++ objects, but PythonQt does not. So if you create a C++ object in PythonQt, it is owned by Python and destroyed when Python looses the reference.
For QObjects this is different, because PythonQt can use the parent() to detect a changed ownership, so ownership passing works for QObjects.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks - for the response. I was able to do the following:
table = QTableView(group)
model = QSqlQueryModel()
model.setQuery("SELECT * FROM public.addr")
print model.query()
table.setModel(model)
box.addWidget(table)
The above is displaying the data correctly. If I understand what you are saying - the data (not being a QObject) was out of scope using my old code - right?
I also do not understand why the above code worked. I read your past responses and it was strongly suggested that models were not working.
BTW I want to thank those (maybe only you) for doing this work. Python is my favorite language and adding it into an exist c++ application will make my life so much happier that I can't thank you enough!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes, if non QObjects are create from PythonQt, they are owned by Python and not C++.
So if you create a QTreeWidgetItem, QListViewItem or QTableViewItem, you have to store their references in Python.
Regarding models, the models work fine (e.g. the standard item model), but creating your own derived item models in PythonQt does not (yet) work, because I don't wrap their protected virtual methods yet.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I'm a newbie but so far I have been able to compile PythonQt (I had
help) into a Qt 5.1 C++ app. I'm able to create Qt dialogs etc.. all
without data.
I was also able to import psycopg2 (module to access Postgres) and the
data is retrieved but I can't get the data into a Qt Dialog/Widget (i.e.
QTableWidget). I'd like a little help if you have time. BTW: this is
going to great if I can get it running with data. Thanks in advance - Johnf
the code:
from PythonQt.QtGui import
from PythonQt.QtCore import
import psycopg2
import psycopg2.extras
import pprint
class ItemTypes(object):
pass
class TableWidget(QTableWidget):
def init(self, parent):
QTableWidget.init(self, parent)
self.initData()
dbname='postbooks_demo_42_beta' user='admin' password='admin'"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("SELECT * FROM public.addr")
ds = cursor.fetchall()
print ds
rows = len(ds)
self.setRowCount(rows)
if rows > 0:
cols = len(ds[0])
self.setColumnCount(cols)
colNames = ds[0].keys()
self.setHorizontalHeaderLabels(colNames)
group = QGroupBox()
box = QVBoxLayout(group)
push1 = QPushButton(group)
box.addWidget(push1)
push2 = QPushButton(group)
box.addWidget(push2)
check = QCheckBox(group)
check.text = 'check me'
group.title = 'my title'
push1.text = 'press me'
push2.text = 'press me2'
box.addWidget(check)
table = TableWidget(group)
table.setRowCount(10)
table.setColumnCount(3)
table.setItem(0, 0, QTableWidgetItem("Hello"))
box.addWidget(table)
group.show()
Since PythonQt does not handle the ownership transfer to C++, you have to keep all your items in an extra Python list which lives as long as you want to show the data in the table. PySide and PyQt both handle ownership passing for C++ objects, but PythonQt does not. So if you create a C++ object in PythonQt, it is owned by Python and destroyed when Python looses the reference.
For QObjects this is different, because PythonQt can use the parent() to detect a changed ownership, so ownership passing works for QObjects.
Thanks - for the response. I was able to do the following:
table = QTableView(group)
model = QSqlQueryModel()
model.setQuery("SELECT * FROM public.addr")
print model.query()
table.setModel(model)
box.addWidget(table)
The above is displaying the data correctly. If I understand what you are saying - the data (not being a QObject) was out of scope using my old code - right?
I also do not understand why the above code worked. I read your past responses and it was strongly suggested that models were not working.
BTW I want to thank those (maybe only you) for doing this work. Python is my favorite language and adding it into an exist c++ application will make my life so much happier that I can't thank you enough!
Yes, if non QObjects are create from PythonQt, they are owned by Python and not C++.
So if you create a QTreeWidgetItem, QListViewItem or QTableViewItem, you have to store their references in Python.
Regarding models, the models work fine (e.g. the standard item model), but creating your own derived item models in PythonQt does not (yet) work, because I don't wrap their protected virtual methods yet.
And thanks for the compliment!