PyQt populate QTableWidget from excel - python

im trying to populate QTableWidget with some data from excel file, i want to add row to my QTable only if ID is on the listID, and i get no data in QTable cells
https://drive.google.com/file/d/0B_PFK3V2Ij4tSko4emplYmNuN1E/view?usp=sharing here an excel file, it don't contain any formatin or formula, just empty rows and columns, because file is generated automaticaly
here is my code
wb = openpyxl.load_workbook(os.path.join(os.getcwd(), file), read_only=True)
ws = wb.active
headers = []
for item in ws[4]:
headers.append(item.value)
headers.pop(0)
listID = []
for index in range(self.listWidgetID.count()):
listID.append(self.listWidgetID.item(index).text())
data = ws.iter_rows(row_offset=5, column_offset=1)
row_increment = 0
self.tableWidgetDATA.setRowCount(1)
self.tableWidgetDATA.setColumnCount(len(headers))
self.tableWidgetDATA.setHorizontalHeaderLabels(headers)
for x, rows in enumerate(data):
if str(rows[0].value) in listID:
for y, cell in enumerate(rows):
item = QTableWidgetItem(str(cell.value))
self.tableWidgetDATA.setItem(x, y, item)
row_increment = row_increment + 1
self.tableWidgetDATA.setRowCount(row_increment)

To add elements to the QTableWidget, the position must be setRowCount(), in your case you update after adding, so it will not be saved, Another error is that you do not verify if the data is valid.
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.tableWidgetDATA = QTableWidget(self)
self.listWidgetID = QListWidget(self)
self.setLayout(QVBoxLayout())
self.layout().addWidget(self.listWidgetID)
self.layout().addWidget(self.tableWidgetDATA)
self.listWidgetID.addItems(["113894", "113906", "113895"])
file = "Livro2.xlsx"
wb = openpyxl.load_workbook(os.path.join(os.getcwd(), file), read_only=True)
ws = wb.active
headers = [item.value for item in ws[4] if item.value is not None]
listID = [self.listWidgetID.item(i).text() for i in range(self.listWidgetID.count())]
data = ws.iter_rows(row_offset=5, column_offset=1)
self.tableWidgetDATA.setColumnCount(len(headers))
self.tableWidgetDATA.setHorizontalHeaderLabels(headers)
for x, rows in enumerate(data):
if rows[0].value is not None:
if str(rows[0].value) in listID:
self.tableWidgetDATA.setRowCount(self.tableWidgetDATA.rowCount()+1)
for y, cell in enumerate(rows):
val = cell.value
if val is not None:
item = QTableWidgetItem(str(val))
self.tableWidgetDATA.setItem(self.tableWidgetDATA.rowCount()-1, y, item)
Output:

Related

Refresh/Re-open Qdialog with QTableWidget inside

I have a program that is "divided" in 6 classes, the first class is the Main Window where I can choose where I'd like to go (I have 4 choices, a page to visualize all the data, one to modify and the other 2 to visualize data with filters), each of theese pages is a QDialog that contains a QTableWidget, this table is connected to a SQL Server database and shows data from a specific table (to be honest it shows data from 2 tables).
My problem is:
In the 2 class ("modificaviaggi") I connected some signals to some action, first of all I connected the itemChanged signal to a method that stores the items written by the user and saves them in the SQL DB, the signal signal I used is the clicked signal, I connected this signal to a method that verifies if the cell clicked it's the 8 or 9, in case it's 8 or 9 it shows another QDialog (class "articoli") where the user can insert some data regarding weight of every product, this weight data are stored in a second table in the SQL Server DB, I use this table to get the weight for every product when I show my table.
So I get all the data from the table SQL "Viaggi" and then I verify if there are some values in the SQL table "Pesi" for each row of the table "Viaggi" using the row ID.
Now the complicated part is that when the user click the columns 8 or 9 it shows another Qdialog, I show this Qdialog everytime the user clicks the columns 8 or 9, then the user can write data on the table and those data will be stored in the table "Pesi", thanks to the variable "lastID" declared on the "modificaviggi" class I know for which row the user clicked the columns 8 or 9, thanks to that I store the values in the SQL table and for each value I have their row ID, now that the situation is more clear the real question is: How can "Re-load" the QTableWidget "tabella_registrazioni" inside the class "modificaviaggi" everytime the user clicks the QPushButton "get_back_button"? Because in the class "articoli" the user modifies/adds values for each row in the QTableWidget "tabella_registrazioni" but I can't reload the data inside here's my code :
c = conn.cursor()
d = conndue.cursor()
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__() # chiamare le inherited classi con metodo __init__
uic.loadUi('pagina_scelta.ui', self) # caricare file.ui
self.show() # mostrarlo
bottonefinestramodifica = self.findChild(QtWidgets.QPushButton, 'bottone_modifica')
bottonefinestramodifica.clicked.connect(self.finestramodifica)
def finestramodifica(self):
self.finestramodifica = modificaviaggi()
self.finestramodifica.show()
class MyDelegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
return
class modificaviaggi(QtWidgets.QDialog):
lastid = [0]
recall = [2]
def finestraarticoli(self):
self.finestraarticoli = articoli()
self.finestraarticoli.show()
def chiusura(self):
self.accept()
def __init__(self):
super(modificaviaggi, self).__init__()
uic.loadUi('modifica_reg.ui', self)
bottonetorna = self.bottone_torna
bottonetorna.clicked.connect(self.chiusura)
self.reloading()
self.datitabella()
def reloading(self):
if 1 == 1:
if self.recall[0] == 1:
self.datitabella()
self.recall[0] == 2
def datitabella(self):
delegate = MyDelegate(self)
tablerow = 0
conteggio = c.execute('SELECT COUNT(ID) FROM Viaggi').fetchone()[0]
print(conteggio)
customers = [" "]
self.tabella_registrazioni.blockSignals(True)
self.customers_combo(customers)
self.tabella_registrazioni.setRowCount(conteggio)
for row in c.execute("SELECT * FROM Viaggi "):
rigavalore = 0
self.tabella_registrazioni.setRowCount(conteggio)
for r in range(0, 19):
self.tabella_registrazioni.setItem(tablerow, rigavalore, QtWidgets.QTableWidgetItem(str(row[rigavalore])))
rigavalore+= 1
tablerow+= 1
self.combocreation(customers)
self.combocreation(customers)
self.tabella_registrazioni.blockSignals(False)
self.tabella_registrazioni.itemChanged.connect(self.changeIcon)
self.tabella_registrazioni.setItemDelegateForColumn(0, delegate)
self.tabella_registrazioni.clicked.connect(self.pressed)
self.combo_customers.activated.connect(self.customer_combo_selection)
righe = self.tabella_registrazioni.rowCount()
tablerow1= 0
for i in range(0, righe):
idrow = self.tabella_registrazioni.item(i, 0).text()
netweight = c.execute("SELECT SUM(Peso_netto) FROM Pesi WHERE ID_registrazione = ?", idrow)
if netweight != None or not netweight.isspace():
netweight = float(str(netweight.fetchone()[0]))
else:
print("Nessun peso")
grossweight = c.execute("SELECT SUM(Peso_lordo) FROM Pesi WHERE ID_registrazione = ?", idrow)
if grossweight != None or not grossweight.isspace():
grossweight = float(str(grossweight.fetchone()[0]))
self.tabella_registrazioni.setItem(tablerow1, 8, QtWidgets.QTableWidgetItem(str(netweight)))
self.tabella_registrazioni.setItem(tablerow1, 9, QtWidgets.QTableWidgetItem(str(grossweight)))
tablerow1+= 1
def combocreation(self, customers):
self.combo_customers = QComboBox()
self.combo_customers.setEditable(True)
def customers_combo(self, customers):
for cu in d.execute("SELECT ANDESCRI FROM VALL_CONTI WHERE ANTIPCON = 'C' "):
customers.append(cu[0].strip())
def changeIcon(self, item):
row = item.row()
col = item.column()
zero = self.tabella_registrazioni.item(row, 0).text()
uno = self.tabella_registrazioni.item(row, 1).text()
custcode = self.tabella_registrazioni.item(row, 3).text()
custname = self.tabella_registrazioni.item(row, 4).text()
c.execute('UPDATE Viaggi SET Codice_cliente = ?, Ragione_sociale = ? WHERE ID = ?',
(custcode, custname, zero))
c.commit()
def pressed(self, item):
row = item.row()
col = item.column()
customers = [" "]
self.lastid[0] = self.tabella_registrazioni.item(row, 0).text()
if col == 8 or col == 9:
self.finestraarticoli()
self.customers_combo(customers)
self.combocreation(customers)
textcell = self.tabella_registrazioni.item(row, col).text()
self.combo_customers.setCurrentText(textcell)
self.combo_customers.activated.connect(self.customer_combo_selection)
if col == 3:
self.tabella_registrazioni.setCellWidget(row, 3, self.combo_customers)
def customer_combo_selection(self):
customer_name = self.combo_customers.currentText()
row = self.rrow
self.tabella_registrazioni.setItem(self.rrow, self.ccolumn, QtWidgets.QTableWidgetItem(str(customer_name)))
id = self.tabella_registrazioni.item(row, 0).text()
c.execute("UPDATE Viaggi SET Ragione_Sociale=? WHERE ID = ?", (customer_name, id))
c.commit()
class articoli(QtWidgets.QDialog):
def changevariable(self):
modificaviaggi.recall[0] = 1
self.accept()
def __init__(self):
super(articoli, self).__init__()
uic.loadUi('Inserimento_articoli.ui', self)
self.get_back_button.clicked.connect(self.changevariable)
self.tabellapesi()
def tabellapesi(self):
self.IDpressed = modificaviaggi.lastid
tablerow = 0
self.tabella_articoli.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
self.conteggio = int(str(c.execute("SELECT COUNT(ID_registrazione) FROM Pesi WHERE ID_registrazione = ? ", self.IDpressed[0]).fetchone()[0]))
for row in c.execute("SELECT * FROM Pesi WHERE ID_registrazione = ? ORDER BY ID ", self.IDpressed[0]):
rigavalore = 0
self.tabella_articoli.setRowCount(self.conteggio)
for r in range(0, 6):
self.tabella_articoli.setItem(tablerow, rigavalore, QtWidgets.QTableWidgetItem(str(row[rigavalore])))
rigavalore+= 1
tablerow+= 1
def insert(self):
rows = self.tabella_articoli.rowCount()
tablerow = 0
for row in range (0, rows):
articlecode = self.tabella_articoli.item(tablerow, 1).text()
articledesc = self.tabella_articoli.item(tablerow, 2).text()
netweight = float(self.tabella_articoli.item(tablerow, 3).text())
grossweight = float(self.tabella_articoli.item(tablerow, 4).text())
price = float(self.tabella_articoli.item(tablerow, 5).text())
if self.tabella_articoli.item(tablerow, 0) == None:
IDweight = ""
else:
IDweight = int(self.tabella_articoli.item(tablerow, 0).text())
queryinsert = "INSERT INTO Pesi ( Codice_articolo, Descrizione_articolo, Peso_netto, Peso_lordo, ID_registrazione, Prezzo) VALUES (?,?,?,?,?,?)"
values = [articlecode, articledesc, netweight, grossweight, self.IDpressed[0], price]
queryupdate = "UPDATE Pesi SET Peso_lordo = ?, Prezzo = ?, Peso_netto = ? , Codice_articolo = ?, Descrizione_articolo = ? WHERE ID = ? "
valuesupdate = [grossweight, price, netweight, articlecode, articledesc, IDweight]
exist = str(c.execute("IF EXISTS (SELECT * FROM CARICO_VIAGGI..Pesi WHERE ID = ?) BEGIN SELECT 1 END ELSE BEGIN SELECT 2 END", (IDweight)).fetchone()[0])
if exist == "1":
c.execute(queryupdate, valuesupdate)
c.commit()
print(type(IDweight))
elif exist == "2":
c.execute(queryinsert, values)
c.commit()
else:
print("Impossibile salvare gli articoli, riprovare")
tablerow+= 1
self.tabella_articoli.clearContents()
tablerow = 0
self.conteggiox = int(str(c.execute("SELECT COUNT(ID_registrazione) FROM Pesi WHERE ID_registrazione = ? ", self.IDpressed[0]).fetchone()[0]))
for row in c.execute("SELECT * FROM Pesi WHERE ID_registrazione = ? ORDER BY ID", self.IDpressed[0]):
rigavalore = 0
self.tabella_articoli.setRowCount(self.conteggiox)
for r in range(0, 6):
self.tabella_articoli.setItem(tablerow, rigavalore, QtWidgets.QTableWidgetItem(str(row[rigavalore])))
rigavalore+= 1
tablerow+= 1
app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()
One solution could be to set up signals that would update the table as the user makes changes so there would be no need to refresh at all.
For example:
Set up a signal in your dialog class that will be emitted whenever the user changes the data. Then in the original dialog with the table you want to update, connect to that signal after initializing the dialog.
here is a minimal example:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
def populate_table(table):
for i in range(10):
table.insertRow(table.rowCount())
for j in range(2):
item = QTableWidgetItem(type=0)
item.setText(f"({i}, {j})")
table.setItem(i,j,item)
class Dialog2(QDialog):
tableInfoChanged = pyqtSignal([int, int, str])
def __init__(self,parent=None):
super().__init__(parent=parent)
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.table = QTableWidget()
self.table.setColumnCount(2)
self.layout.addWidget(self.table)
populate_table(self.table)
self.table.cellChanged.connect(self.emitChangedInfo)
def emitChangedInfo(self, row, col):
text = self.table.item(row, col).text()
self.tableInfoChanged.emit(row, col, text)
class Dialog1(QDialog):
def __init__(self,parent=None):
super().__init__(parent=parent)
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.table = QTableWidget()
self.table.setColumnCount(2)
self.button = QPushButton("Push to open dialog2")
self.layout.addWidget(self.table)
self.layout.addWidget(self.button)
populate_table(self.table)
self.button.clicked.connect(self.openDialog2)
def updateTable(self, row, col, text):
self.table.item(row,col).setText(text)
def openDialog2(self):
self.dialog2 = Dialog2()
self.dialog2.tableInfoChanged.connect(self.updateTable)
self.dialog2.exec()
app = QApplication(sys.argv)
window = Dialog1()
window.show()
sys.exit(app.exec_())
I have tested this code and should function as is.

How to move items from one QTreeWidget to another QTreeWidget and keep track of information?

I have a GUI where user selects gas components from list and moves it to 'Chosen' and another button takes text from 'Chosen' that has columns: Gas Component, Molecular Weight, Mol%. The first two columns get information from a dictionary that i've created, and last column is user input.
When button is clicked and all values are filled in 'Chosen', it will ask user for a number, n = 1-6 , it will then take rows with n highest mol% in 'Chosen' and create n rows in Results and add Gas Component text with n highest mol% values, to first column in 'Results'
I am currently using dictionaries to keep track of information.
def calculategas(self):
#makes sure dictionaries are clear for any errors on rerunning button
self.sortedmol.clear()
self.componentDic1.clear()
self.componentDic2.clear()
self.componentDic3.clear()
self.mmDict.clear()
self.mfDict.clear()
self.mDict.clear()
self.massFracDict.clear()
self.molarmassDict.clear()
self.item_.clear()
self.lookup.clear()
root = self.chosen.invisibleRootItem()
child_count = root.childCount()
for i in range(child_count):
item = root.child(i)
#Takes text from self.chosen QTreeWidget (Top-right)
component = item.text(0)
molWeight = item.text(1)
componentMol = float(item.text(2))
#creates dictionary of items in self.chosen
self.componentDic1[component] = componentMol
self.componentDic2[molWeight] = componentMol
#Sorts dictionaries above from highest to lowest
self.sortedmol = dict(sorted(self.componentDic1.items(), key=operator.itemgetter(1),
reverse=True)) # Sorted component list - largest to smallest mol%
self.sortedmolar = dict(sorted(self.componentDic2.items(), key=operator.itemgetter(1),
reverse=True)) # Sorted molar mass list - largest to smallest mol%
# change values of self.sortedmol with keys of self.sortedmolar
self.lookup = {v:k for k, v in self.sortedmol.items()}
self.componentDic3 = {self.lookup[v]: float(k) for k, v in self.sortedmolar.items()}
##Copies so original doesn't change
self.mmDict = self.sortedmol.copy()
self.mfDict = self.mmDict.copy()
self.mDict = self.componentDic3.copy()
###Calculations
self.molarmassDict = {k: round(v * self.mmDict[k] / 100, 3) for k, v in self.mDict.items() if
k in self.mmDict}
summolmDict = round(sum(self.molarmassDict.values()), 3)
self.massFracDict = {k: round(self.molarmassDict[k] / summolmDict, 3) for k, v in self.molarmassDict.items()
if
k in self.molarmassDict}
componentNum, ok = QInputDialog.getText(None, 'Number of components', 'How many components do you wish to use?')
if (ok):
#Remove any items in result QTreeWidget
current_item = self.result.invisibleRootItem()
children = []
for child in range(current_item.childCount()):
children.append(current_item.child(child))
for child in children:
current_item.removeChild(child)
#Adds rows to self.result QTreeWidget
for i in range(int(componentNum)):
self.item_[i] = QtWidgets.QTreeWidgetItem(self.result)
self.item_[i].setFlags(
QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
if len(self.sortedmol) > int(componentNum): # takes only # of components user wants
##Adds the number of components user inputs with highest mol% to self.result
root = self.result.invisibleRootItem()
child_count = root.childCount()
for i in range(child_count):
item = root.child(i)
item.setText(0, str(list(self.massFracDict)[i])) # update first column with dictionary keys
else:
###This section will change
root = self.result.invisibleRootItem()
child_count = root.childCount()
for i in range(child_count):
item = root.child(i)
item.setText(0, str(list(self.massFracDict)[i])) # update first column with dictionary keys
Currently everything works except that when there is a duplicate value in mol% or molecular weight it will tend to skip it.
Dictionary self.sortedmol:
Keys = Gas Component text
Values = mol% text
Dictionary self.sortedmolar:
Keys = Molecular Weight text
Values = mol% text
Problems:
If two components have same molecular weight, it will ignore it
If two components have same mol%, it will ignore it
Overall goal: Add rows with n highest mol% in 'Chosen' to 'Result and keep values for later calculations.
Question: Is there any way to fix this error, or use another way to get same desired results?
Step1:
Step2:
If you want to get the n rows where the mol% are the highest then you must use a QSortFilterProxyModel to sort, and another to filter.
from PyQt5 import QtCore, QtGui, QtWidgets
ListRole = QtCore.Qt.UserRole
VisibleRole = QtCore.Qt.UserRole + 1
class TopProxyModel(QtCore.QSortFilterProxyModel):
#property
def number(self):
if not hasattr(self, "_number"):
self._number = -1
return self._number
#number.setter
def number(self, number):
self._number = number
self.invalidateFilter()
def filterAcceptsRow(self, sourceRow, sourceParent):
if self.number < 0:
ix = self.sourceModel().index(sourceRow, 2)
self.sourceModel().setData(ix, False, VisibleRole)
return True
return sourceRow < self.number
class BlankDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super().initStyleOption(option, index)
if not index.data(VisibleRole):
option.text = ""
def setModelData(self, editor, model, index):
sm = model
ix = index
while hasattr(sm, "sourceModel"):
ix = sm.mapToSource(ix)
sm = sm.sourceModel()
sm.setData(ix, editor.value(), QtCore.Qt.DisplayRole)
if not sm.data(ix, VisibleRole):
sm.setData(ix, True, VisibleRole)
class ReadOnlyDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
return None
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
datas = [
("IsonButane", 58.12, 13),
("IsonPentane", 75.12, 3),
("Methane", 16.04, 5),
("Nitrogen", 28.01, 5),
("Hexane", 86.17, 5),
("Hydrogen", 2.02, 13),
("Hydrogen Sulfide", 34.08, 2),
]
add_button = QtWidgets.QPushButton(">>>", clicked=self.add_row)
remove_button = QtWidgets.QPushButton("<<<", clicked=self.remove_row)
select_button = QtWidgets.QPushButton("Calculate", clicked=self.select)
sp = select_button.sizePolicy()
sp.setHorizontalPolicy(QtWidgets.QSizePolicy.Maximum)
select_button.setSizePolicy(sp)
self.listwidget = QtWidgets.QListWidget(
selectionMode=QtWidgets.QAbstractItemView.MultiSelection
)
for data in datas:
item = QtWidgets.QListWidgetItem(data[0])
item.setData(ListRole, data)
self.listwidget.addItem(item)
self.tree_widget = QtWidgets.QTreeWidget(
columnCount=3,
indentation=0,
selectionMode=QtWidgets.QAbstractItemView.MultiSelection,
)
for i, T in enumerate(
(ReadOnlyDelegate, ReadOnlyDelegate, BlankDelegate)
):
delegate = T(self.tree_widget)
self.tree_widget.setItemDelegateForColumn(2, delegate)
self.tree_widget.setHeaderLabels(
["Gas Component", "Molecular Weight", "Mol%"]
)
self.tree_view = QtWidgets.QTreeView(indentation=0)
proxy_sort = QtCore.QSortFilterProxyModel(self)
proxy_sort.setSourceModel(self.tree_widget.model())
proxy_sort.sort(2, QtCore.Qt.DescendingOrder)
self.proxy_top = TopProxyModel(self)
self.proxy_top.number = 0
self.proxy_top.setSourceModel(proxy_sort)
self.tree_view.setModel(self.proxy_top)
lay = QtWidgets.QGridLayout(self)
lay.addWidget(QtWidgets.QLabel("<b>Available Gases:</b>"), 0, 0)
lay.addWidget(self.listwidget)
vlay = QtWidgets.QVBoxLayout()
vlay.addStretch()
vlay.addWidget(add_button)
vlay.addWidget(remove_button)
vlay.addStretch()
lay.addLayout(vlay, 1, 1)
lay.addWidget(QtWidgets.QLabel("<b>Chosen Gases</b>"), 0, 2)
lay.addWidget(self.tree_widget, 1, 2)
lay.addWidget(select_button, 2, 2, alignment=QtCore.Qt.AlignCenter)
lay.addWidget(QtWidgets.QLabel("<b>Result:</b>"), 3, 2)
lay.addWidget(self.tree_view, 4, 2)
#QtCore.pyqtSlot()
def add_row(self):
for item in self.listwidget.selectedItems():
data = item.data(ListRole)
text = item.text()
if self.tree_widget.findItems(text, QtCore.Qt.MatchExactly):
continue
it = self.listwidget.takeItem(self.listwidget.row(item))
item = QtWidgets.QTreeWidgetItem()
self.tree_widget.addTopLevelItem(item)
item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable)
for i, e in enumerate(data):
item.setData(i, QtCore.Qt.DisplayRole, e)
#QtCore.pyqtSlot()
def remove_row(self):
rows = [
self.tree_widget.indexOfTopLevelItem(item)
for item in self.tree_widget.selectedItems()
]
for row in sorted(rows, reverse=True):
item = self.tree_widget.takeTopLevelItem(row)
data = []
for i in range(self.tree_widget.columnCount()):
data.append(item.data(i, QtCore.Qt.DisplayRole))
it = QtWidgets.QListWidgetItem(data[0])
it.setData(ListRole, data)
self.listwidget.addItem(it)
if item is not None:
del item
#QtCore.pyqtSlot()
def select(self):
last_number = max(self.proxy_top.number, 0)
number, ok = QtWidgets.QInputDialog.getInt(
None,
"Number of components",
"How many components do you wish to use?",
last_number,
min=-1,
max=self.tree_widget.topLevelItemCount(),
)
if ok:
self.proxy_top.number = number
for i in range(self.tree_widget.topLevelItemCount()):
it = self.tree_widget.topLevelItem(i)
it.setData(2, VisibleRole, False)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

How to place radio button at each row in qtablewidget and allow only one row to select from group of rows?

Below is my table :
In the above table, i have placed the Radiobutton at each row.
In the table, column 'field' has same values in the rows i.e startdate is present in two rows and validity is also repeating in rows.
What i want is to only select only one row consisting validity from the repeating values using radio button. if user selects another value,it must deselect previously selected value.
Below is my code:
class Window(QtGui.QWidget):
def __init__(self, data):
QtGui.QWidget.__init__(self)
self.data=data
rows=len(data)
data1=list()
for d in data:
#print d
#print "ddddddddddddddd",type(d)
d=d.replace('null','""')
d=eval(d)
#print type(d)
data1.append(d)
self.data1=data1
key_list=['file','field','content','refined_content','page_number']
data2=list()
for line in data1:
#print line
d = OrderedDict((k, line[k]) for k in key_list)
data2.append(d)
#print data2
data1=data2
#print data1
keys = ['file','field','content','refined_content','page_number']
keys=list(keys)
columns=len(keys)+1
keys.append("checkbox")
self.table = QtGui.QTableWidget(rows, columns, self)
self.table.setHorizontalHeaderLabels(keys)
self.keys=keys
#print "total columns",columns
#print "total rows",rows
#columns=str(columns)
for column in range(columns-1):
for row in range(rows):
qwidget = QtGui.QWidget()
checkbox = QtGui.QRadioButton()
checkbox.setChecked(False)
qhboxlayout = QtGui.QHBoxLayout(qwidget)
qhboxlayout.addWidget(checkbox)
qhboxlayout.setAlignment(Qt.AlignCenter)
qhboxlayout.setContentsMargins(0, 0, 0, 0)
dic=data1[row]
#print dic
index=int(column)
#print "index",index
value = dic.values()[index]
#print "current row",row
#print "current column",column
self.table.setItem(row, column, QtGui.QTableWidgetItem(str(value)))
#self.table.setItem(row, columns+1, QtGui.QTableWidgetItem(str(row)))
self.table.setCellWidget(row,columns-1 , qwidget)
layout = QtGui.QVBoxLayout(self)
self.button = QtGui.QPushButton()
self.button.setObjectName("loadButton")
self.button.setFixedWidth(100)
self.button.setFixedHeight(30)
self.button.setStyleSheet('color: white;background-color:rgb(0, 76, 153);border-style: outset;border-width: 2px;border-color: rgb(0, 76, 153);border-radius: 10px;alignment:left;')
self.button.setText("Submit")
layout.addWidget(self.table)
layout.addWidget(self.button)
self.button.clicked.connect(self.ButtonClicked)
def ButtonClicked(self,c):
c=self.table.columnCount()
#print "c",c
#print "keys",self.keys
checked_list = []
for i in range(self.table.rowCount()):
#checked_list = []
dic={}
if self.table.cellWidget(i, c-1).findChild(type(QtGui.QRadioButton())).isChecked():
for j in range(c-1):
st=str(self.table.item(i, j).text())
#print j,st
dic[self.keys[j]]=st
#checked_list.append(st)
#print "ddiic",dic
checked_list.append(dic)
print checked_list

How can I print data from my database while in QTableWidget?

How can I print my data from my database (sqlite) into a notepad /word document from my table in my GUI (using the same formatting). Here is my code for the table which is represented on my gui.
class Table(QtGui.QDialog):
def __init__(self):
super(Table, self).__init__()
with sqlite3.connect('database.db') as db:
cursor=db.cursor()
cursor.execute('select* from Receipt Order BY ReceiptID ASC')
title = [cn[0] for cn in cursor.description]
rows = [cn[0] for cn in cursor.description]
cur=cursor.fetchall()
layout = QtGui.QGridLayout()
self.table = QtGui.QTableWidget()
self.setGeometry(500,500,500,400)
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
self.label2=QtGui.QLabel(self)
self.label2.setPixmap(QtGui.QPixmap('receipt_pic.jpg'))
self.label2.setGeometry(0,0,500,400)
self.table.setColumnCount(2)
self.table.setHorizontalHeaderLabels(title)
for i,row in enumerate(cur):
self.table.insertRow(self.table.rowCount())
for j,val in enumerate(row):
self.table.setItem(i, j, QtGui.QTableWidgetItem(str(val)))
layout.addWidget(self.table, 0, 0)
self.setLayout(layout)
self.setWindowTitle('Receipt Table')
I want to be able to click a button which copies this information (which would appear as a table with filled columns and rows) into a seperate notepad file / or any text document (where I can send the table to the printer to be printed).
It would probably be easier to just print the table directly, rather than use an intermediary file.
To do this, you can use a QTextDocument to create a printable representation of the table, and then use the built-in print dialogs to do the rest.
So, first add some buttons:
...
layout.addWidget(self.table, 0, 0, 1, 2)
self.buttonPrint = QtGui.QPushButton('Print', self)
self.buttonPrint.clicked.connect(self.handlePrint)
self.buttonPreview = QtGui.QPushButton('Preview', self)
self.buttonPreview.clicked.connect(self.handlePreview)
layout.addWidget(self.buttonPrint, 1, 0)
layout.addWidget(self.buttonPreview, 1, 1)
self.setLayout(layout)
...
then some handlers for the print and print-preview dialogs:
def handlePrint(self):
dialog = QtGui.QPrintDialog()
if dialog.exec_() == QtGui.QDialog.Accepted:
self.handlePaintRequest(dialog.printer())
def handlePreview(self):
dialog = QtGui.QPrintPreviewDialog()
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
and finally some methods for creating the document and printing it:
def handlePaintRequest(self, printer):
document = self.makeTableDocument()
document.print_(printer)
def makeTableDocument(self):
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
rows = self.table.rowCount()
columns = self.table.columnCount()
table = cursor.insertTable(rows + 1, columns)
format = table.format()
format.setHeaderRowCount(1)
table.setFormat(format)
format = cursor.blockCharFormat()
format.setFontWeight(QtGui.QFont.Bold)
for column in range(columns):
cursor.setCharFormat(format)
cursor.insertText(
self.table.horizontalHeaderItem(column).text())
cursor.movePosition(QtGui.QTextCursor.NextCell)
for row in range(rows):
for column in range(columns):
cursor.insertText(
self.table.item(row, column).text())
cursor.movePosition(QtGui.QTextCursor.NextCell)
return document
If you prefer to save to a file, you can use QTextDocument.toHtml to dump the table as html.
The printed results are quite basic, but if you want something more fancy, you can always build the document using html/css.
I use this function
def LoadDatabase(self):
self.banco = sqlite3.connect ( 'Vendas.db' )
self.cursor = banco.cursor ( )
query = "SELECT * FROM Produtos"
result = self.banco.execute ( query )
self.listaprodutos.setRowCount ( 0 )
for row_number, row_data in enumerate ( result ):
self.listaprodutos.insertRow ( row_number )
for colum_number, data in enumerate ( row_data ):
self.listaprodutos.setItem ( row_number, colum_number, QtWidgets.QTableWidgetItem ( str ( data ) ) )

Why set_model method in PyGtk duplicate the value of the first column at the third column?

I'm using PyGtk to display some string information in a treeview.
Here goes my code:
def create_table(self):
self.mainbox = gtk.ScrolledWindow()
self.mainbox.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.window.add( self.mainbox)
model = gtk.ListStore( str, str, str)
self.treeview = gtk.TreeView(model=None)
col = gtk.TreeViewColumn( "Element")
self.treeview.append_column( col)
cell = gtk.CellRendererText()
col.pack_start( cell, expand=False)
col.set_attributes( cell, text=0)
col = gtk.TreeViewColumn( "Test")
self.treeview.append_column( col)
cell = gtk.CellRendererSpin()
col.pack_start( cell, expand=False)
col.set_attributes( cell, text=1)
col = gtk.TreeViewColumn( "Command")
self.treeview.append_column( col)
cell = gtk.CellRendererSpin()
col.pack_start( cell, expand=False)
col.set_attributes( cell, text=0)
cell = gtk.CellRendererCombo()
self.mainbox.add( self.treeview)
self.mainbox.set_size_request( 500, 260)
self.mainbox.show()
self.vbox.pack_start(self.mainbox, expand=False, fill=True, padding=0)
Then, I created a method to fill the treeview after an event button. The call:
def populate_treeview_button(self):
button = gtk.Button(label='Populate Table')
button.connect("clicked", self.create_model)
self.vbox.pack_start(button, expand=False, fill=True, padding=0)
And the method (I receive a list of dicts at table_information attribute where the key is an element(string) and the value a list with 2 strings):
def create_model(self, beats_me_param):
model = gtk.ListStore( str, str, str)
elements = []
tests = []
commands = []
table_information = self.get_organized_table()
for i in table_information:
for dicts in i:
for element in dicts.keys():
elements.append(element)
for i in table_information:
for dicts in i:
for value in dicts.values():
tests.append(value[0])
commands.append(value[1])
for i in range(len(elements)):
model.append([elements[i], tests[i], commands[i]])
self.treeview.set_model(model)
When I saw the result at my treeview, I got at third column the same value of the first column and of course they are different. Image bellow:
I changed the order of elements in the "append" moment and same occurs, the value changes but the changed value is repeated at the third column. What's going wrong?
The problem was because I set the same index to the third and first column.
col = gtk.TreeViewColumn( "Command")
self.treeview.append_column( col)
cell = gtk.CellRendererSpin()
col.pack_start( cell, expand=False)
col.set_attributes( cell, text=2)
The right way.

Categories

Resources