I have created a window containing a QTreeWidget called StudentTreeWidget, and when double clicking on one of the items in the Tree Widget it should open up another window called StudentEnterprisePassport:
class Student_search(Ui_Student_search):
def __init__(self, database, tableName, parent=None):
QtGui.QWidget.__init__(self)
self.dbu = DB_manager_students.DatabaseUtility(database, tableName)
self.connect(self, QtCore.SIGNAL('itemClicked(QTreeWidgetItem*, int)'), self.handleButton)
self.setupUi(self)
self.UpdateTree()
self.window4 = None
def handleButton(self):
if self.window4 is None:
database = 'EnterprisePassport'
tablename = 'students'
self.window4 = StudentEnterprisePassport(database, tablename)
self.window4.show()
class StudentEnterprisePassport(Ui_StudentEnterprisePassport):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
self.sessionTracker_btn.clicked.connect(self.handleButton)
When running the code no error is outputted, however, nothing happens when I double click an item in the Tree Widget. The QTreeWidget window is being imported from a Ui file created and converted from Qt Designer.
from PyQt4 import QtCore, QtGui
import DB_manager_students, sys
(....)
class Ui_Student_search(QtGui.QWidget):
def __init__(self, database, tableName):
QtGui.QWidget.__init__(self)
self.dbu = DB_manager_students.DatabaseUtility(database, tableName)
self.setupUi(self)
self.UpdateTree()
def setupUi(self, Student_search):
Student_search.setObjectName(_fromUtf8("Student_search"))
Student_search.resize(1299, 856)
self.gridLayout = QtGui.QGridLayout(Student_search)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.verticalLayout_2 = QtGui.QVBoxLayout()
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.StudentTreeWidget = QtGui.QTreeWidget(Student_search)
self.StudentTreeWidget.setObjectName(_fromUtf8("StudentTreeWidget"))
self.verticalLayout_2.addWidget(self.StudentTreeWidget)
self.gridLayout.addLayout(self.verticalLayout_2, 0, 0, 1, 1)
Code to input data into the TreeWidget:
def UpdateTree(self):
col = self.dbu.GetColumns()
table = self.dbu.GetTable()
for c in range(len(col)):
self.StudentTreeWidget.headerItem().setText(c, col[c][0])
self.StudentTreeWidget.clear()
for item in range(len(table)):
QtGui.QTreeWidgetItem(self.StudentTreeWidget)
for value in range(len(table[item])):
self.StudentTreeWidget.topLevelItem(item).setText(value, str(table[item][value]))
if __name__ == '__main__':
db = 'EnterprisePassport'
tableName = 'students'
app = QtGui.QApplication(sys.argv)
ex = Ui_Student_search(db, tableName)
ex.show()
x = app.exec_()
sys.exit(x)
Related
I am trying to add drag and drop functionality to a small application. Getting data from a QlistWidget and Dropping the data on a QTableWidget. I should override the dropEvent of QTableWidget in order to add some other functions when dropping the data. But i have trouble, i think i can not get the text() of the object gotten from the ListWidget. here is the code:
class Table(QtWidgets.QTableWidget):
def __init__(self,r,c, parent=None):
super().__init__(r,c,parent)
self.init_ui()
def init_ui(self):
self.setAcceptDrops(True)
self.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
"""def dragMoveEvent(self, e):
e.setDropAction(QtCore.Qt.MoveAction)
e.accept()
def dragEnterEvent(self,e):
e.accept()"""
def dropEvent(self,e):
data = e.mimeData()
a=e.pos()
row = self.rowAt(a.y())
col = self.columnAt(a.x())
self.setItem(row,col,QtWidgets.QTableWidgetItem(data.text()))
print(row,col)
print(type(data.text()))
print(e.source())
x = data.text()
print(x)
e.accept()
`
The data that is transmitted from a QListWidget through the drag-and-drop is not given through text(), because an item has much more information identified by the roles, in addition you can drag several items. The data is transmitted using the MIME type application/x-qabstractitemmodeldatalist and the solution is to decode it as shown below:
from PyQt5 import QtCore, QtWidgets
class TableWidget(QtWidgets.QTableWidget):
def __init__(self, r,c, parent=None):
super(TableWidget, self).__init__(r,c, parent)
self.setAcceptDrops(True)
self.setDragDropMode(QtWidgets.QAbstractItemView.DropOnly)
def dropEvent(self, event):
md = event.mimeData()
fmt = "application/x-qabstractitemmodeldatalist"
if md.hasFormat(fmt):
encoded = md.data(fmt)
stream = QtCore.QDataStream(encoded, QtCore.QIODevice.ReadOnly)
table_items = []
while not stream.atEnd():
# row and column where it comes from
row = stream.readInt32()
column = stream.readInt32()
map_items = stream.readInt32()
it = QtWidgets.QTableWidgetItem()
for i in range(map_items):
role = stream.readInt32()
value = QtCore.QVariant()
stream >> value
it.setData(role, value)
table_items.append(it)
for it in table_items:
print(it, it.text())
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
list_widget = QtWidgets.QListWidget()
list_widget.setAcceptDrops(False)
list_widget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
list_widget.setDragDropMode(QtWidgets.QAbstractItemView.DragOnly)
for i in range(10):
it = QtWidgets.QListWidgetItem("item-{}".format(i))
list_widget.addItem(it)
table_widget = TableWidget(5, 10)
central_widget = QtWidgets.QWidget()
hlay = QtWidgets.QHBoxLayout(central_widget)
hlay.addWidget(list_widget)
hlay.addWidget(table_widget)
self.setCentralWidget(central_widget)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I have a simple .xlsx file and I load its content to my qtableview. My code likes this:
import xlrd
from PyQt5 import QtCore, QtGui, QtWidgets, uic
form_class = uic.loadUiType("SearchView.ui")[0] # Load the UI
app = None
myWindow = None
dic_messages = dict()
class MyWindowClass(QtWidgets.QMainWindow, form_class):
def __init__(self, fileName, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.gui = form_class
self.fileName = fileName
self.model = QtGui.QStandardItemModel(self)
self.tableView.setModel(self.model)
self.fn_load_messages()
def fn_load_messages(self):
workbook = xlrd.open_workbook('sample.xlsx')
worksheet = workbook.sheet_by_name('sample_sheet')
for i in range(0, worksheet.nrows):
dic_messages[i + 1] = (worksheet.cell(i, 0).value, worksheet.cell(i, 1).value, worksheet.cell(i, 2).value, worksheet.cell(i, 3).value, worksheet.cell(i, 4).value)
i += 1
rowPosition = self.tbl_messages.rowCount()
self.tbl_messages.insertRow(rowPosition)
return
def main():
global app, myWindow
app = QtWidgets.QApplication(sys.argv)
myWindow = MyWindowClass('sample.xlsx')
myWindow.show()
app.exec_()
if __name__ == '__main__':
main()
i want to add a checkbox column in each row and then i want to check or unchecked each row i want. i added below part to my code but the checkboxes are not active:
self.tableView.setItemDelegateForColumn(0 , CheckBoxDelegate(self))
class CheckBoxDelegate(QtWidgets.QStyledItemDelegate):
def paint(self, painter, option, index):
check_box_style_option = QtWidgets.QStyleOptionButton()
check_box_style_option.rect = self.getCheckBoxRect(option)
check_box_style_option.state = QtWidgets.QStyle.State_Enabled
QtWidgets.QApplication.style().drawControl(QtWidgets.QStyle.CE_CheckBox,
check_box_style_option, painter)
def getCheckBoxRect(self, option):
check_box_style_option = QtWidgets.QStyleOptionButton()
check_box_rect = QtWidgets.QApplication.style().subElementRect(QtWidgets.QStyle.SE_CheckBoxIndicator, check_box_style_option, None)
check_box_point = QtCore.QPoint (option.rect.x() +
option.rect.width() / 2 -
check_box_rect.width() / 2,
option.rect.y() +
option.rect.height() / 2 -
check_box_rect.height() / 2)
return QtCore.QRect(check_box_point, check_box_rect.size())
how can I do this?
Thanks.
You don't set it in the view but in the items and model. Like this:
#!/usr/bin/env python
from PyQt5 import QtWidgets, QtGui
class MyWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.tableModel = QtGui.QStandardItemModel(self)
self.tableModel.itemChanged.connect(self.itemChanged)
item = QtGui.QStandardItem("Click me")
item.setCheckable(True)
self.tableModel.appendRow(item)
self.mainLayout = QtWidgets.QVBoxLayout()
self.setLayout(self.mainLayout)
self.tableView = QtWidgets.QTableView()
self.tableView.setModel(self.tableModel)
self.mainLayout.addWidget(self.tableView)
def itemChanged(self, item):
print("Item {!r} checkState: {}".format(item.text(), item.checkState()))
def main():
app = QtWidgets.QApplication([])
win = MyWidget()
win.show()
win.raise_()
app.exec_()
if __name__ == "__main__":
main()
The code below creates a single dialog window with three rows of widgets:
a combo, lineEdit and dateEdit. When combobox shows 'Show LineEdit' I would like lineEdit to be visible and dateEdit to be hidden. When "Show DateEdit" is selected I would like to hide LineEdit and show DateEdit instead. How to achieve it?
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setLayout(QtGui.QVBoxLayout())
for i in range(3):
row = QtGui.QHBoxLayout()
combo = QtGui.QComboBox()
combo.addItems(['Show LineEdit','Show DateEdit'])
combo.activated.connect(self.activated)
row.addWidget(combo)
self.lineEdit = QtGui.QLineEdit()
self.dateEdit = QtGui.QDateEdit()
self.dateEdit.setVisible(False)
row.addWidget(self.lineEdit)
row.addWidget(self.dateEdit)
self.layout().insertLayout(i, row)
def activated(self):
print self.sender()
panel=Dialog()
panel.show()
app.exec_()
The strategy is to create a dictionary that has as a key a combobox and values a dictionary of the corresponding other widget, then use the activated method that can return a text or a number.
from PyQt4 import QtGui
app = QtGui.QApplication([])
class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setLayout(QtGui.QVBoxLayout())
self.widgets = {}
for i in range(3):
row = QtGui.QHBoxLayout()
combo = QtGui.QComboBox()
combo.addItems(['Show LineEdit', 'Show DateEdit'])
combo.activated[str].connect(self.activated)
row.addWidget(combo)
lineEdit = QtGui.QLineEdit()
dateEdit = QtGui.QDateEdit()
self.widgets[combo] = [lineEdit, dateEdit]
self.changeWidget(combo.currentText(), lineEdit, dateEdit)
row.addWidget(lineEdit)
row.addWidget(dateEdit)
self.layout().insertLayout(i, row)
def activated(self, text):
linedit, dateEdit = self.widgets[self.sender()]
self.changeWidget(text, linedit, dateEdit)
def changeWidget(self, text, linedit, dateEdit):
if text == 'Show LineEdit':
linedit.setVisible(True)
dateEdit.setVisible(False)
elif text == 'Show DateEdit':
linedit.setVisible(False)
dateEdit.setVisible(True)
panel = Dialog()
panel.show()
app.exec_()
Inspired by eyllanesc answer posted above but with no dictionary.
from PyQt4 import QtGui
app = QtGui.QApplication([])
class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setLayout(QtGui.QVBoxLayout())
for i in range(3):
row = QtGui.QHBoxLayout()
combo = QtGui.QComboBox()
combo.addItems(['Show LineEdit', 'Show DateEdit'])
combo.activated[str].connect(self.activated)
row.addWidget(combo)
combo.lineEdit = QtGui.QLineEdit()
combo.dateEdit = QtGui.QDateEdit()
row.addWidget(combo.lineEdit)
row.addWidget(combo.dateEdit)
self.layout().insertLayout(i, row)
def activated(self, title):
combo = self.sender()
if title == 'Show LineEdit':
combo.lineEdit.setVisible(True)
combo.dateEdit.setVisible(False)
else:
combo.lineEdit.setVisible(False)
combo.dateEdit.setVisible(True)
panel = Dialog()
panel.show()
app.exec_()
How to update the combobox in subwindow1 from subwindow2.The following code should be working fine but it doesnot update the combobox and doesnot show any errors at the line
AddressBook().updatestock(name)
in the add function.It looks like it creates an instance of the AddressBook.If so how can i rectify it.
import os
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class menudemo(QMainWindow):
def __init__(self, parent=None):
super(menudemo, self).__init__(parent)
self.pilot_widget=AddInventory()
self.drop_widget=AddressBook()
self.centralWidget=QMdiArea(self)
self.setCentralWidget(self.centralWidget)
self.sub1=QMdiSubWindow()
self.sub1.setWidget(self.drop_widget)
self.centralWidget.addSubWindow(self.sub1)
self.sub1.show()
self.sub2=QMdiSubWindow()
self.sub2.setWidget(self.pilot_widget)
self.centralWidget.addSubWindow(self.sub2)
self.sub2.show()
self.setWindowTitle("menu demo")
self.showMaximized()
class AddInventory(QWidget):
def __init__(self, parent=None):
super(AddInventory, self).__init__(parent)
self.statement=""
self.nameLabel = QLabel("Item:")
self.name=QLineEdit()
self.addButton = QPushButton("&Add")
self.addButton.setDefault(True)
self.addButton.clicked.connect(self.add)
mainLayout = QGridLayout()
mainLayout.addWidget(self.nameLabel, 0, 0)
mainLayout.addWidget(self.name,0,1)
mainLayout.addWidget(self.addButton, 0, 3)
self.setLayout(mainLayout)
self.setWindowTitle(" Address Book")
self.setFixedSize(self.sizeHint())
def add(self,text):
name=self.name.text()
AddressBook().updatestock(name)
self.name.clear()
class AddressBook(QWidget):
def __init__(self, parent=None):
super(AddressBook, self).__init__(parent)
nameLabel2 = QLabel("Item:")
self.itemstock=QComboBox(self)
self.itemstock.activated[str].connect(self.updatestock)
mainLayout = QGridLayout()
mainLayout.addWidget(nameLabel2, 0, 0)
mainLayout.addWidget(self.itemstock, 0, 1)
self.setLayout(mainLayout)
self.setWindowTitle("Simple Address Book")
self.setFixedSize(self.sizeHint())
def updatestock(self,name):
print(name)
self.itemstock.addItem(name)
def main():
app = QApplication(sys.argv)
ex = menudemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
It doesn't work, because you're just creating a new instance of AddressBook, you call a method on it.
It is not linked to self.drop_widget
You have to link both widgets using the constructor. You do this in the main
self.pilot_widget=AddInventory()
self.drop_widget=AddressBook()
I would do:
self.drop_widget=AddressBook()
self.pilot_widget=AddInventory(self.drop_widget)
and change AddInventory class parameters
class AddInventory(QWidget):
def __init__(self, drop_widget, parent=None):
self.drop_widget = drop_widget # now it is linked in your class as a member
now, in add:
def add(self,text):
name=self.name.text()
self.drop_widget.updatestock(name)
self.name.clear()
Could someone help me create a recursive function which loops through the treeview QStandardItemModel and collects all items which are 'checked true'
I'm not entirely clear on how to go about doing this myself.
from PySide import QtGui, QtCore
from PySide import QtSvg, QtXml
import sys
class Person:
def __init__(self, name="", children=None):
self.name = name
self.children = children if children else []
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(300, 400)
self.init_ui()
def init_ui(self):
# Setup Tabs Widget
# self.treeview = QtGui.QTreeView()
self.treeview = QtGui.QTreeView()
self.treeview.setHeaderHidden(True)
self.treeview.setUniformRowHeights(True)
self.treeview.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.model = QtGui.QStandardItemModel()
self.treeview.setModel(self.model)
self.action = QtGui.QAction('Print', self)
self.action.setShortcut('F5')
self.action.triggered.connect(self.get_checked)
fileMenu = QtGui.QMenu("&File", self)
fileMenu.addAction(self.action)
self.menuBar().addMenu(fileMenu)
# Setup central widget
self.setCentralWidget(self.treeview)
# populate data
self.populate_people()
self.treeview.expandAll()
def populate_people(self):
parent = Person("Kevin", [
Person("Tom", [Person("Sally"), Person("Susan")]),
Person("Snappy", [Person("John"), Person("Kimmy"),
Person("Joe")]),
Person("Chester", [Person("Danny"), Person("Colleen")])
]
)
self.create_nodes(parent, self.model)
def create_nodes(self, node, parent):
tnode = QtGui.QStandardItem()
tnode.setCheckable(True)
tnode.setData(QtCore.Qt.Unchecked, role=QtCore.Qt.CheckStateRole)
tnode.setData(node.name , role=QtCore.Qt.DisplayRole)
tnode.setData(node, role=QtCore.Qt.UserRole) # store object on item
parent.appendRow(tnode)
for x in node.children:
self.create_nodes(x, tnode)
def get_checked(self):
print "collecting..."
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
It can be done with the model's match method:
def get_checked(self):
model = self.treeview.model()
checked = model.match(
model.index(0, 0), QtCore.Qt.CheckStateRole,
QtCore.Qt.Checked, -1,
QtCore.Qt.MatchExactly | QtCore.Qt.MatchRecursive)
for index in checked:
item = model.itemFromIndex(index)
print(item.text())