I want to build a record from multiple QT widgets and store it in a database. The following script runs without an error but does not store the record. Part of the problem is that I cannot determine the last ID already in the database, but even if I manually set the ID--nothing is written. I've seen examples online using insertRecord, but the QT documentation suggests using insertRow. Please feel free to correct my approach. I'm new to Python and to Qt.
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtSql import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
#Make Database
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('C:/Users/dle/Documents/example1.sqlite')
self.db.open()
self.db.transaction()
self.db.exec_(
'CREATE TABLE t1'
'(id INTEGER PRIMARY KEY, f1 INTEGER NOT NULL, f2 INTEGER NOT NULL, f3 TEXT NOT NULL)'
)
self.db.exec_("INSERT INTO t1 VALUES(1, 10, 20, 'db works fine')")
self.db.commit()
#Create User Interface
self.f1 = QLineEdit()
self.f2 = QLineEdit()
self.f3 = QLineEdit()
self.storeButton = QPushButton("Store")
self.storeButton.clicked.connect(self.doStore)
vlayout = QVBoxLayout()
vlayout.addWidget(self.f1)
vlayout.addWidget(self.f2)
vlayout.addWidget(self.f3)
vlayout.addWidget(self.storeButton)
widget = QWidget()
widget.setLayout(vlayout)
self.setCentralWidget(widget)
def doStore(self):
self.dbModel = QSqlTableModel(self)
self.dbModel.setTable('t1')
ID = self.dbModel.query().lastInsertId() #this returns "None" even though last ID
#in table is 1
thisRecord = QSqlRecord
thisRecord = self.dbModel.record()
thisRecord.setValue(0, ID) # Does not write, even if ID is integer
print ID
thisRecord.setValue(1, int(self.f1.text()))
print int(self.f1.text())
thisRecord.setValue(2, int(self.f2.text()))
print int(self.f2.text())
thisRecord.setValue(3, self.f3.text())
print self.f3.text()
print thisRecord
self.dbModel.insertRecord(ID, thisRecord) # Does not write, even if ID is integer
# Doesn't record field 0 already have ID?
if __name__ == '__main__':
app = QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
If you want to insert a row at the end of the table it is not necessary to indicate the index, since according to the docs:
PySide.QtSql.QSqlTableModel.insertRecord(row, record)
Parameters:
row - PySide.QtCore.int
record – PySide.QtSql.QSqlRecord
Return type:
PySide.QtCore.bool
Inserts the record after row . If row is negative, the record will be
appended to the end. Calls PySide.QtSql.QSqlTableModel.insertRows()
and PySide.QtSql.QSqlTableModel.setRecord() internally.
Returns true if the row could be inserted, otherwise false.
From the above we can conclude that we should only use as row = -1.
self.dbModel.insertRecord(-1, record)
The best thing is to create the model once and not every time the doStore function is called, we must also use the QSqlRecord of the model with the help of the record () function as it comes loaded with the field names.
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('example1.sqlite')
self.db.open()
self.db.transaction()
self.db.exec_(
'CREATE TABLE t1'
'(id INTEGER PRIMARY KEY, f1 INTEGER NOT NULL, f2 INTEGER NOT NULL, f3 TEXT NOT NULL)'
)
self.db.commit()
self.db.exec_("INSERT INTO t1 VALUES(1, 10, 20, 'db works fine')")
#Create User Interface
[...]
self.setCentralWidget(widget)
self.dbModel = QSqlTableModel(self)
self.dbModel.setTable('t1')
def doStore(self):
record = self.dbModel.record()
record.setValue(1, int(self.f1.text()))
record.setValue(2, int(self.f2.text()))
record.setValue(3, self.f3.text())
if not self.dbModel.insertRecord(-1, record):
print(self.db.lastError().text())
Related
I made PyQt5 APP which create SQlite3 DB and show data from it in QTableView widget.
I have a problem with editing rows in a widget. There are 3 buttons "Add", "Change" and "Delete" that should delete, modify and add new rows to the widget, as well as edit the database itself, but the buttons do not work properly.
"Add" button - after clicking add button, when all new data inputted and Enter clicked, all values is gone and "!" symbol is showing. I need press Add button, input new data in cells, click Enter and all data must save in SQL DB and displayed in QTableView widget in live time.
"Change" button - when change clicked and Enter pressed after cell editing, all data gone. All data must be save in real time is SQL DB after change button press.
3)"Delete" button - this button don't delete row. There is no error, no any answer from app.
How to program the buttons correctly ?
Do I need to use a delegate?
Which programming approach is more preferable when working with a graphical interface and SQL database?
I made a reproducible example. At startup, a database with 1 table and 2 rows will be created.
import sys, os, sqlite3
from datetime import datetime
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
from PyQt5.QtCore import *
CONFIG_NAME = 'config.ini'
DB_NAME = 'nsi.db'
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__()
self.window_pref()
self.show_widgets()
def window_pref(self):
self.setWindowTitle('PyQt5 APP')
self.def_width = 800
self.def_height = 400
self.def_size = self.setMinimumSize(self.def_width, self.def_height)
def show_widgets(self):
self.createConnection()
self.fillDB()
self.setupMainWidgets()
def createConnection(self):
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(DB_NAME)
if not db.open():
QMessageBox.warning(self, 'PyQt5 APP',
'Error:{}'.format(db.lastError().text()))
sys.exit(1)
def fillDB(self):
query = QSqlQuery()
query.exec_("""\
CREATE TABLE sprav (
id_nsi INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
nsi_name TEXT UNIQUE NOT NULL,
file_date TEXT NOT NULL,
file_name TEXT NOT NULL)
""")
query.prepare("""\
INSERT INTO sprav (nsi_name, file_date, file_name)VALUES (?, ?, ?)
""")
sample_list = (('nsi1', 'january', 'file1'), ('nsi2', 'may', 'file2'))
for i in sample_list:
query.addBindValue(i[0])
query.addBindValue(i[1])
query.addBindValue(i[2])
query.exec_()
def setupMainWidgets(self):
mw_widget = QWidget()
main_panel = QHBoxLayout(mw_widget)
# SQL Table
self.modelSql = QSqlTableModel()
self.modelSql.setTable('sprav')
self.modelSql.setQuery(QSqlQuery(
'SELECT nsi_name, file_date, file_name FROM sprav'))
self.modelSql.setHeaderData(self.modelSql.fieldIndex('nsi_name'),
Qt.Horizontal, 'Name')
self.modelSql.setHeaderData(self.modelSql.fieldIndex('file_date'),
Qt.Horizontal, 'Date')
self.modelSql.setHeaderData(self.modelSql.fieldIndex('file_name'),
Qt.Horizontal, 'File')
self.modelSql.setEditStrategy(QSqlTableModel.OnFieldChange)
self.modelSql.select()
# QTableView()
self.table_view = QTableView()
self.table_view.setSelectionBehavior(1)
self.table_view.setAlternatingRowColors(True)
self.table_view.setModel(self.modelSql)
self.table_view.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
main_panel.addWidget(self.table_view)
# QVBoxLayout()
right_panel = QVBoxLayout()
line = QFrame()
line.setFrameShape(QFrame.HLine)
self.add_record = QPushButton('Add', self)
self.add_record.clicked.connect(self.addRecord)
self.change_record = QPushButton('Change', self)
self.change_record.clicked.connect(self.changeRecord)
self.delete_record = QPushButton('Delete', self)
self.delete_record.clicked.connect(self.delRecord)
right_panel.addSpacing(20)
right_panel.addWidget(line)
right_panel.addWidget(self.add_record)
right_panel.addWidget(self.change_record)
right_panel.addWidget(self.delete_record)
right_panel.addStretch()
main_panel.addLayout(right_panel)
self.setCentralWidget(mw_widget)
def addRecord(self):
row = self.modelSql.rowCount()
self.modelSql.insertRow(row)
index = self.modelSql.index(row, 0)
self.table_view.setCurrentIndex(index)
self.table_view.edit(index)
def delRecord(self):
cur_item = self.table_view.selectedIndexes()
for index in cur_item:
self.modelSql.removeRow(index.row())
self.modelSql.select()
def changeRecord(self):
self.table_view.edit(self.table_view.currentIndex())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
There are two problems with your code.
First of all, if you use QSqlTableModel, you should not call setQuery():
This function simply calls QSqlQueryModel::setQuery(query). You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query.
This is the main reason for which your row addition/deletion/editing didn't work: the model became partially invalid, and any submission was discarded because columns didn't match the records of the table model, which is very important also because the model requires an incremental key that QSqlTableModel is able to use properly on its own.
Remove the setQuery() from your code, and consider that if you did that only to hide a column, then you just have to hide that column:
self.table_view.setColumnHidden(0, True)
Obviously, you have to keep in mind that all column indexes you will use, will now start from 1, since the model also includes the id:
def addRecord(self):
# ...
index = self.modelSql.index(row, 1)
self.table_view.setCurrentIndex(index)
self.table_view.edit(index)
The other problem was the deletion of rows: even after fixing what described above, the number of rows and its order would have been wrong:
when you cycle through the selectedIndexes() you're getting the same row for each selected item: since you used the SelectRows selection behavior, the for loop would have called removeRow() three times the same row, for each selected row;
removal of indexes should always be in reverse, sorted order; consider if you try to remove row 0 and 1: the first iteration would remove row 0, but at that point the previous row 1 would have become the new row 0, so the next iteration would actually delete the row that previously was the third;
The solution is to have a sorted list of unique row numbers, and cycle through them in reversed order:
def delRecord(self):
# create a set of unique row numbers
rows = set([i.row() for i in self.table_view.selectedIndexes()])
# cycle through them in reversed sorting order
for row in sorted(rows, reverse=True):
self.modelSql.removeRow(row)
self.modelSql.select()
Remember to call select() (I know you did, but better safe than sorry), as explained in the documentation about removeRows():
Deletions are submitted immediately to the database. The model retains a blank row for successfully deleted row until refreshed with select().
Unrelated notes: 1. avoid unnecessary and confusing imports: since you're already importing QtWidgets with wildcard, there's no point for from PyQt5 import QtWidgets; you either import the submodule, or its classes; 2. setMinimumSize returns nothing, so self.def_size will be None; if you want to keep a variable for the default size, use self.def_size = QSize(self.def_width, self.def_height) then self.setMinimumSize(self.def_size); 3. do not use sys.exit() inside a Qt app (and from a QWidget class), instead use QApplication.exit(1); 4. use more verbose names that also clarify their type: you have almost identical names for buttons and functions (add_record and addRecord), which is a poor choice in naming (also considering the different writing style): a better choice would be to name the button like add_record_btn, or addRecordBtn to follow the Qt convention;
I am new to Python scripting and now is trying to design a window interface to save daily expense into EXCEL file.
I have written a QTableWidget to type in the daily expense information but found the error message "AttributeError: 'Window' object has no attribute 'tableWidget'" while saving the data into EXCEL file.
Can anyone give me the hint to move forward?
Below is my code for your reference.
class Window(QtGui.QWidget):
def __init__(self): #Initial a Window frame
super(Window, self).__init__()
self.labels() #insert labels
self.buttons() #insert buttons
self.draglists()
self.table()
.
.
.
def table(self):
table = QtGui.QTableWidget(self)
table.setRowCount(20)
table.setColumnCount(4)
table.resize(450, 300)
table.move(640, 100)
horHeader = ['Date', 'Category', 'Item', 'Expense']
table.setHorizontalHeaderLabels(horHeader)
def savefile(self):
filename = unicode(QtGui.QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)"))
wbk = xlwt.Workbook()
sheet = wbk.add_sheet("sheet", cell_overwrite_ok=True)
self.add2(sheet)
wbk.save(filename)
def add2(self, sheet):
for currentColumn in range(self.tableWidget.columnCount()):
for currentRow in range(self.tableWidget.rowCount()):
try:
teext = str(self.tableWidget.item(currentRow, currentColumn)).text()
sheet.write(currentRow, currentColumn, teext)
except AttributeError:
pass
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Window()
ex.setGeometry (50, 50, 1200, 600)
ex.show()
ex.setWindowTitle("Search Engine for Expense")
sys.exit(app.exec_())
You're referencing something you haven't defined yet. Your class Window inherits from QtGui.QWidget, which doesn't have an attribute called tableWidget, yet you reference an attribute called tableWidget here:
def add2(self, sheet):
for currentColumn in range(self.**tableWidget**.columnCount()):
for currentRow in range(self.**tableWidget**.rowCount()):
try:
teext = str(self.**tableWidget**.item(currentRow, currentColumn)).text()
sheet.write(currentRow, currentColumn, teext)
except AttributeError:
pass
Hence the error. If you want self.tableWidget to reference the table you made in table(self), you'd need to make it an instance attribute at the end of the function like so:
def table(self):
table = QtGui.QTableWidget(self)
table.setRowCount(20)
table.setColumnCount(4)
table.resize(450, 300)
table.move(640, 100)
horHeader = ['Date', 'Category', 'Item', 'Expense']
table.setHorizontalHeaderLabels(horHeader)
self.tableWidget = table
That will allow your other functions to access the table and its attributes, including .columnCount() and .rowCount().
Hope that helps.
I have a QTableWidget, 2 Columns in which the first column contains the name of items while the second column is populated with qcomboboxes (created using cellwidgets) which contains a list of color options (eg. "", RED, BLUE, GREEN)
For example, in the following table, items denoted with * has a variable called color_set, I am trying to achieve as follows:
it will lookup the names of items in the scene
then it will go through a function to check and see if the item
shirt_01 contains a variable called color_set
If the variable exists, say if it is RED, it will then lookup the list of options in the combobox and display RED as the value
If the variable does not exists, it will displays a blank field instead
this function/values populate the table with information before the ui is launched, it is not a button clicked function etc.
Name of items | Color Options
shirt_01* | RED
shirt_02 |
pants* | GREEN
However, instead of getting my ui to output as above, I got the following result:
Name of items | Color Options
shirt_01* | RED
shirt_02 | RED
pants* | GREEN
For some reasons, the shirt_02 seems to got 'tangled' and displayed the same result as shirt_01. And I think it could possibly be due to how my logic was written but I could not figure it out or because I keep tying my output result to self.color_combobox as my code initially checks through the first column then find the matching text in the second column.
But I keep getting error at the line - item_name has no attribute .text()
import maya.cmds as cmds
from PyQt4 import QtGui, QtCore
import json
def get_all_mesh():
all_mesh = cmds.listRelatives(cmds.ls(type = 'mesh'), parent=True)
return all_mesh
def read_json(node_name):
# checks if the node_name exists in the json file
with open('/Desktop/car_colors.json') as data_file:
data = json.load(data_file)
items = set()
for index, name in enumerate(data):
# if the name is in the json, it will states the color
if node_name in name:
for item in (data[name]):
#print "{0} - {1}".format(name, item)
items.add(item)
return items
def attrToPy(objAttr):
stringAttrData = str(cmds.getAttr(objAttr))
loadedData = cPickle.loads(stringAttrData)
return loadedData
class testTableView(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('Color Test')
self.setModal(False)
self.all_mesh = get_all_mesh()
# Build the GUI
self.init_ui()
self.populate_data()
def init_ui(self):
# Table setup
self.mesh_table = QtGui.QTableWidget()
self.mesh_table.setRowCount(len(self.all_mesh))
self.mesh_table.setColumnCount(3)
self.mesh_table.setHorizontalHeaderLabels(['Mesh Found', 'Color for Mesh'])
self.md_insert_color_btn = QtGui.QPushButton('Apply color')
# Layout
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.mesh_table)
self.layout.addWidget(self.md_insert_color_btn)
self.setLayout(self.layout)
def populate_data(self):
geo_name = self.all_mesh
for row_index, geo_item in enumerate(geo_name):
new_item = QtGui.QTableWidgetItem(geo_item)
# Add in each and every mesh found in scene and append them into rows
self.mesh_table.setItem(row_index, 0, new_item)
geo_exclude_num = ''.join(i for i in geo_item if not i.isdigit())
color_list = read_json(geo_exclude_num)
color_list.add("")
# Insert in the color
self.color_combobox = QtGui.QComboBox()
#color_list = get_color()
self.color_combobox.addItems(list(sorted(color_list)))
self.mesh_table.setCellWidget(row_index, 1, self.color_combobox)
self.color_value_to_combobox()
def color_value_to_combobox(self):
obj_nodes = get_all_mesh()
for node in obj_nodes:
# This attribute is stored in the Extra Attributes
objAttr = '%s.pyPickle'%node
storedData = attrToPy(objAttr)
if 'color_set' in storedData:
color_variant = storedData['color_set']
combo_index = self.color_combobox.findText(color_variant, QtCore.Qt.MatchFixedString)
self.color_combobox.setCurrentIndex(0 if combo_index < 0 else combo_index)
# To opent the dialog window
dialog = testTableView()
dialog.show()
Is there anyway in which I can make my ui to check for the list of item in scene, tallies which name and combobox it should go (in terms of which rows/columns)? Otherwise what other ways can i approach?
EDIT: This is the attachment that I did it quick - Link
If you run the UI, you will notice that pCube1 has the color "red" while pCube2 is "blue", however both the comboboxes will turn up as blue instead
Ok, so the problem was in color_value_to_combobox. Instead of setting the combobox for ALL objects, you need to specify what object and combobox the function needs to work on. The major flaw was that you were using self.color_combobox to set with every time, so every object would keep setting the same combobox! It needs to be more generic.
Anyways, I just had to change the end of populate_data and color_value_to_combobox. Now cube1 shows red, and cube2 shows blue.
import maya.cmds as cmds
from PyQt4 import QtGui, QtCore
import json
import cPickle
def get_all_mesh():
all_mesh = cmds.listRelatives(cmds.ls(type = 'mesh'), parent=True)
return all_mesh
def read_json(node_name):
# checks if the node_name exists in the json file
with open('/Desktop/car_colors.json') as data_file:
data = json.load(data_file)
items = set()
for index, name in enumerate(data):
# if the name is in the json, it will states the color
if node_name in name:
for item in (data[name]):
#print "{0} - {1}".format(name, item)
items.add(item)
return items
def attrToPy(objAttr):
stringAttrData = str(cmds.getAttr(objAttr))
loadedData = cPickle.loads(stringAttrData)
return loadedData
class testTableView(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('Color Test')
#self.setModal(False)
self.all_mesh = get_all_mesh()
# Build the GUI
self.init_ui()
self.populate_data()
def init_ui(self):
# Table setup
self.mesh_table = QtGui.QTableWidget()
self.mesh_table.setRowCount(len(self.all_mesh))
self.mesh_table.setColumnCount(3)
self.mesh_table.setHorizontalHeaderLabels(['Mesh Found', 'Color for Mesh'])
self.md_insert_color_btn = QtGui.QPushButton('Apply color')
# Layout
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.mesh_table)
self.layout.addWidget(self.md_insert_color_btn)
self.setLayout(self.layout)
def populate_data(self):
geo_name = self.all_mesh
for row_index, geo_item in enumerate(geo_name):
new_item = QtGui.QTableWidgetItem(geo_item)
# Add in each and every mesh found in scene and append them into rows
self.mesh_table.setItem(row_index, 0, new_item)
geo_exclude_num = ''.join(i for i in geo_item if not i.isdigit())
color_list = read_json(geo_exclude_num)
color_list.add("")
# Insert in the color
color_combobox = QtGui.QComboBox()
#color_list = get_color()
color_combobox.addItems(list(sorted(color_list)))
self.mesh_table.setCellWidget(row_index, 1, color_combobox)
self.color_value_to_combobox(geo_item, color_combobox) # Pass the object and combobox you want to set
# This use to work on all mesh objects, but only edits a single combobox. This is wrong!
def color_value_to_combobox(self, node, combobox):
# This attribute is stored in the Extra Attributes
objAttr = '%s.pyPickle'%node
storedData = attrToPy(objAttr)
if 'color_set' in storedData:
color_variant = storedData['color_set']
combo_index = combobox.findText(color_variant, QtCore.Qt.MatchFixedString)
combobox.setCurrentIndex(0 if combo_index < 0 else combo_index) # Needs to work on the proper combobox!
# To opent the dialog window
dialog = testTableView()
dialog.show()
If the color variable doesn't exist, your code needs to explicitly set the color combo to a blank item:
def populate_data(self):
geo_name = self.all_mesh
for row_index, geo_item in enumerate(geo_name):
new_item = QtGui.QTableWidgetItem(geo_item)
# Add in each and every mesh found in scene and append them into rows
self.mesh_table.setItem(row_index, 0, new_item)
geo_exclude_num = ''.join(i for i in geo_item if not i.isdigit())
color_list = read_json(geo_exclude_num)
color_list.add("")
# Insert in the color
color_combobox = QtGui.QComboBox()
#color_list = get_color()
color_combobox.addItems(list(sorted(color_list)))
self.mesh_table.setCellWidget(row_index, 1, color_combobox)
# This attribute is stored in the Extra Attributes
objAttr = '%s.pyPickle' % geo_item
storedData = attrToPy(objAttr)
if 'color_set' in storedData:
color_variant = storedData['color_set']
else:
color_variant = ''
combo_index = color_combobox.findText(color_variant, QtCore.Qt.MatchFixedString)
color_combobox.setCurrentIndex(0 if combo_index < 0 else combo_index)
(NB: for this to work, you obviously must make sure the first entry in the combo-boxes is a blank item).
If you look at the code you posted, it doesn't show the loop over the rows to put the combo boxes in each cell, but I will assume there is one otherwise it wouldn't work at all.
What you should do is one of two things:
- if you have a separate loop to create your combo boxes then in that loop don't create a combo box if the item does not have colors; then in the second loop you check if the cell has a combo box and only if it does do you configure it.
- Second option is to move the first loop into the rows loop, ie you as loop through each row you determine if the item has colors, and only if it does, do you create the combo box and configure it; if no colors, just leave that sell empty.
I am running python version 2.7.8. I am storing project titles in a sqlite database that I am trying to import into a combobox. When I try and import the project titles from the sqlite database and import them into the combobox I get this error: TypeError: 'unicode' object is not callable
Here is my code:
main.py
from EvDB import EvDB
import wx
from ProjectsPanel import ProjectsPanel
class MyFrame(wx.Frame):
""" We simply derive a new class of Frame. """
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title=title, size=(650,725))
# Create Database Tables
self.db = EvDB(self)
self.db.createTbls()
main = wx.Panel(self)
self.projectsPg = ProjectsPanel(main, -1)
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.mainSizer.Add(self.projectsPg, 1, wx.ALL|wx.EXPAND)
main.SetAutoLayout(True)
main.SetSizer(self.mainSizer)
self.mainSizer.Fit(main)
self.Layout()
self.Show()
self.UserID = 1
rows = self.db.getProjects(self.UserID)
print(rows)
print(str(rows))
self.projectsPg.cb.Value(rows)
app = wx.App(False)
ProjectsPanel.py
import wx
from EvDB import EvDB
import sqlite3 as lite
class ProjectsPanel(wx.Panel):
def __init__(self, parent, ID):
wx.Panel.__init__(self, parent, ID)
self.db = lite.connect('evDB.db')
self.cur = self.db.cursor()
self.db2 = EvDB(self)
sizer = wx.BoxSizer(wx.VERTICAL)
# the combobox Control
self.userProjects = ['Before1','Before2', 'Before3', 'Before4']
self.cb = wx.ComboBox(self, value='New', choices=self.userProjects, style=wx.CB_DROPDOWN)
sizer.Add(self.cb, 0, wx.EXPAND)
# Setting Layouts
self.SetAutoLayout(True)
self.SetSizer(sizer)
sizer.Fit(self)
EvDB.py
import sqlite3 as lite
class EvDB():
def __init__(self, parent):
self.db = lite.connect('evDB.db')
self.cur = self.db.cursor()
def createTbls(self):
self.db.execute('''CREATE TABLE IF NOT EXISTS Projects
(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Title varchar(50) DEFAULT NULL,
DateCreated DATE);''')
self.db.execute('''CREATE TABLE IF NOT EXISTS User_x_Project
(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
UserID INT DEFAULT NULL,
ProjectID INT DEFAULT NULL);''')
def getProjects(self,userID):
self.cur.execute("SELECT Title FROM User_x_Project, Projects WHERE User_x_Project.UserID = "+str(userID)+" AND User_x_Project.ProjectID = Projects.ID;")
rows = self.cur.fetchall()
return rows
the print results: [(u'Tests',), (u'Test1',), (u'Test2',), (u'Test3',)]
How do I add the titles stored in my sqlite database to my combobox?
Thank you for any and all help!
EDIT: Here is my entire traceback
C:\Python27\python.exe C:/Users/xxx/xxx/xxx/main.py
[(u'Tests',), (u'Test1',), (u'Test2',), (u'Test3',)]
[(u'Tests',), (u'Test1',), (u'Test2',), (u'Test3',)]
Traceback (most recent call last):
File "C:/Users/xxx/xxx/xxx/main.py", line 43, in <module>
frame = MyFrame(None, -1, 'Small editor')
File "C:/Users/xxx/xxx/xxx/main.py", line 37, in __init__
self.projectsPg.cb.Value(rows)
TypeError: 'unicode' object is not callable
Process finished with exit code 1
self.projectsPg.cb is a ComboBox object, and Value is a property. If you access the property without assignment it will return a string (or unicode). You can't call it.
If you want to set a value to the combox, use property assignment (ComboBox.Value =), or ComboxBox.SetValue:
In addition to that, the rows returned by Cursor.fetchall is a list of tuples. You need to fetch the first one. (In the following example, I omitted return row count check for brevity)
self.projectsPg.cb.Value = rows[0][0]
# OR
self.projectsPg.cb.SetValue(rows[0][0])
I have a QWidget which contains a QTableView..A find dialog is poped up after pressing Ctrl+F.
I want to select the row (rows) which contain the text entered in find dialog in their first column.
I have this code but the problem is that it does not do the row selecting, please guide me whether i have a good approach for finding matches and selecting the corresponding row in the table. Problem is that the following code can not find matches and select them.
class Widget(QWidget):
def __init__(self,md,parent=None):
QWidget.__init__(self,parent)
layout=QVBoxLayout(self)
# initially construct the visible table
self.table = QTableView()
self.table.horizontalHeader().setStretchLastSection(True) # uncomment this if the last column shall cover the rest
self.table.show()
# set black grid lines
self.setStyleSheet("gridline-color: rgb(39, 42, 49)")
# construct the Qt model belonging to the visible table
model = NvmQtModel(md)
self.table.setModel(model)
self.table.resizeRowsToContents()
self.table.resizeColumnsToContents()
# set the shortcut ctrl+F for find in menu
shortcut = QShortcut(QKeySequence('Ctrl+f'), self)
shortcut.activated.connect(self.handleFind)
# shows and handles the find dialog
def handleFind(self):
findDialog = QDialog()
findDialog.setWindowTitle("Find")
grid = QGridLayout()
findDialog.setLayout(grid)
findLabel = QLabel("Find what", findDialog)
grid.addWidget(findLabel,1,0)
findField = QLineEdit(findDialog)
grid.addWidget(findField,1,1)
findButton = QPushButton("Find", findDialog)
grid.addWidget(findButton,2,1)
findButton.clicked.connect(
lambda: self.find(findField.text()))
print("Hereee1")
findDialog.exec_()
# find function: search in the first column of the table
def find(self, text, column=0):
print("Hereee2")
model = self.table.model()
start = model.index(0, column)
matches = model.match(
start, Qt.DisplayRole,
text, 1, Qt.MatchContains)
print(text)
if matches:
index = matches[0]
# index.row(), index.column()
self.table.selectionModel().select(
index, QtGui.QItemSelectionModel.Select)
print("Hereee3")