Having three classes. Class Table QTableWidget and a signal and a function to get row numbers with click event. which transmit and send to two other classes. This part of script works as promising.
class Table(QtWidgets.QWidget):
rownumber = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(Table, self).__init__(parent)
self.tableWidget = QtWidgets.QTableWidget(0, 3)
self.tableWidget.cellClicked.connect(self.cellClick)
def cellClick(self, row, column):
self.rownumber.emit(row)
def currentrow(self):
return self.tableWidget.currentRow()
Second Combo class with QComboBox and a signal and two functions to send and transfer information to third class, that value in QComboBox is changed. To check if the signal is emitting, printing Signal emitted . This part of script seems also working fine, since print code is executed.
class Combo(QtWidgets.QWidget):
Toupdatedata = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(Combo, self).__init__(parent)
self.combo = QtWidgets.QComboBox()
self.combo.addItems(["1","2","3","4"])
self.combo.activated.connect(self.setdatastrength)
#QtCore.pyqtSlot(int)
def setdatastrength(self, index):
value = self.combo[index]
self.dataupdate()
def dataupdate(self):
self.Toupdatedata.emit()
print('Signal emitted')
def get_data(self):
return tuple([self.combo.currentText()])
Third class Tab with QTabWidget and two functions and a connection, first function recieves numbers of row from first class Table, second function should and supposed to run and execute, when index of combobox from second class Combo changes. Checking to see if it works printing a string text I am executing. But it does not work as expected?! When values of QComboBox changes, function updatedata() never runs!
class Tab(QtWidgets.QWidget):
def __init__(self, parent=None):
super( Tab, self).__init__()
self.tab = QtWidgets.QTabWidget()
self.Table = Table()
self.combo = [Combo(), Combo()]
self.datacombo = []
self.Row = 0
self.tab.addTab( self.Table, 'Tables')
self.Table.rownumber.connect(self.rowselected_tables)
self.combo[self.Row].Toupdatedata.connect(self.updatedata)
# Alternative calling function currentrow.
# self.combo[self.Table.currentrow()].Toupdatedata.connect(self.updatedata)
#QtCore.pyqtSlot(int)
def rowselected_tables(self, row):
self.Row = row
if row > 0:
self.Tab.addTab(self.combo[row], 'Combo')
a1 = self.combo[row].get_data()
self.datacombo[row] = a1
#QtCore.pyqtSlot()
def updatedata(self):
self.datacombo[self.Table.currentrow()] = self.combo[self.Table.currentrow()].get_data()
print('I am executing', self.datagroup)
I wonder, what's it I'm doing wrong?
UPDATE:
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
class Table(QtWidgets.QWidget):
rownumber = QtCore.pyqtSignal(int)
rowCount = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(Table, self).__init__(parent)
self.tableWidget = QtWidgets.QTableWidget(3, 3)
self.lay = QtWidgets.QHBoxLayout(self)
self.lay.addWidget(self.tableWidget)
self.tableWidget.cellClicked.connect(self.cellClick)
def cellClick(self, row, column):
self.rownumber.emit(row)
def currentrow(self):
return self.tableWidget.currentRow()
def getrow(self):
count = self.tableWidget.rowCount()
self.rowCount.emit(count)
class Combo(QtWidgets.QWidget):
Toupdatedata = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(Combo, self).__init__(parent)
self.combo = QtWidgets.QComboBox()
self.combo.addItems(["1","2","3","4"])
self.hbox = QtWidgets.QHBoxLayout()
self.con = QtWidgets.QLabel("Number: ")
self.hbox.addWidget(self.con)
self.hbox.addWidget(self.combo)
self.setLayout(self.hbox)
self.combo.activated.connect(self.setdatastrength)
#QtCore.pyqtSlot(int)
def setdatastrength(self, index):
self.dataupdate()
def dataupdate(self):
self.Toupdatedata.emit()
print('Signal emitted')
def get_data(self):
return tuple([self.combo.currentText()])
class Tab(QtWidgets.QWidget):
def __init__(self, parent=None):
super( Tab, self).__init__()
self.tab = QtWidgets.QTabWidget()
self.Table = Table()
self.combo = []
self.datacombo = []
self.Row = 0
self.tab.addTab( self.Table, 'Tables')
self.Table.rowCount.connect(self.addrow)
self.Table.getrow()
self.Table.rownumber.connect(self.rowselected_tables)
self.combo[self.Row].Toupdatedata.connect(self.updatedata)
self.lay = QtWidgets.QHBoxLayout(self)
self.lay.addWidget(self.tab)
# Alternative calling function currentrow.
# self.combo[self.Table.currentrow()].Toupdatedata.connect(self.updatedata)
#QtCore.pyqtSlot(int)
def addrow(self, count):
for row in range(count):
self.combo.append(Combo())
self.datacombo.append(Combo().get_data())
#QtCore.pyqtSlot(int)
def rowselected_tables(self, row):
self.Row = row
if row > 0:
while self.tab.count() > 1:
self.tab.removeTab( self.tab.count()-1 )
self.tab.addTab(self.combo[row], 'Combo')
a1 = self.combo[row].get_data()
self.datacombo[row] = a1
else:
for n in [1]:
self.tab.removeTab( n )
#QtCore.pyqtSlot()
def updatedata(self):
self.datacombo[self.Table.currentrow()] = self.combo[self.Table.currentrow()].get_data()
print('I am executing', self.datagroup)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Tab()
w.show()
sys.exit(app.exec_())
In my opinion, you are connecting the signal self.combo[self.Row].Toupdatedata.connect (self.updatedata) in the wrong place.
I noted the text where the changes were made. Try it:
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
class Table(QtWidgets.QWidget):
rownumber = QtCore.pyqtSignal(int)
rowCount = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(Table, self).__init__(parent)
self.tableWidget = QtWidgets.QTableWidget(3, 3)
self.lay = QtWidgets.QHBoxLayout(self)
self.lay.addWidget(self.tableWidget)
self.tableWidget.cellClicked.connect(self.cellClick)
def cellClick(self, row, column):
self.rownumber.emit(row)
def currentrow(self):
return self.tableWidget.currentRow()
def getrow(self):
count = self.tableWidget.rowCount()
self.rowCount.emit(count)
class Combo(QtWidgets.QWidget):
Toupdatedata = QtCore.pyqtSignal(int) # + int
def __init__(self, rowTable, parent=None): # + rowTable
super(Combo, self).__init__(parent)
self.rowTable = rowTable # +
## print(rowTable, '------')
self.combo = QtWidgets.QComboBox()
self.combo.addItems(["item1", "item2", "item3", "item4"])
self.hbox = QtWidgets.QHBoxLayout()
self.con = QtWidgets.QLabel("Number: ")
self.hbox.addWidget(self.con)
self.hbox.addWidget(self.combo)
self.setLayout(self.hbox)
self.combo.activated.connect(self.setdatastrength)
#QtCore.pyqtSlot(int)
def setdatastrength(self, index):
self.dataupdate()
def dataupdate(self):
print('+ Signal emitted ->', self.rowTable)
self.Toupdatedata.emit(self.rowTable) # + self.rowTable
def get_data(self):
return tuple([self.combo.currentText()])
class Tab(QtWidgets.QWidget):
def __init__(self, parent=None):
super( Tab, self).__init__()
self.tab = QtWidgets.QTabWidget()
self.Table = Table()
self.combo = []
self.datacombo = []
self.Row = 0
self.tab.addTab( self.Table, 'Tables')
self.Table.rowCount.connect(self.addrow)
self.Table.getrow()
self.Table.rownumber.connect(self.rowselected_tables)
#- self.combo[self.Row].Toupdatedata.connect(self.updatedata)
self.lay = QtWidgets.QHBoxLayout(self)
self.lay.addWidget(self.tab)
# Alternative calling function currentrow.
# ? self.combo[self.Table.currentrow()].Toupdatedata.connect(self.updatedata)
#QtCore.pyqtSlot(int)
def addrow(self, count):
for row in range(count):
cb = Combo(row) # + row
self.combo.append(cb)
self.datacombo.append(cb.get_data()[0]) # get_data()[0]
self.combo[row].Toupdatedata.connect(lambda rowTable=row: self.updatedata(rowTable)) # <===== !!!
#QtCore.pyqtSlot(int)
def rowselected_tables(self, row):
self.Row = row
if row > 0:
while self.tab.count() > 1:
self.tab.removeTab( self.tab.count()-1 )
self.tab.addTab(self.combo[row], 'Combo')
a1 = self.combo[row].get_data()[0] # + [0]
self.datacombo[row] = a1
else:
for n in [1]:
self.tab.removeTab( n )
#QtCore.pyqtSlot()
def updatedata(self, rowTable): # + rowTable
# self.datacombo[self.Table.currentrow()] = self.combo[self.Table.currentrow()].get_data()
self.datacombo[rowTable] = self.combo[rowTable].get_data()[0] # [0]
print('I am executing', self.datacombo ) # ? self.datagroup
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Tab()
w.show()
sys.exit(app.exec_())
Related
I have a number of QTreeWidget. Here, there are two trees.
the left one has "a" , "b".
I want to drag this item into the right tree.
I have no error but the item become empty.
How should I do for dragging the left data to the right tree?
and why?
data is this.
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x00\x02\x00a\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x00\x02\x00b'
from PySide import QtCore
from PySide import QtGui
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=None)
self.sequoia = Sequoia()
self.baobab = Baobab()
self.c_widget = QtGui.QWidget()
h_boxlayout = QtGui.QHBoxLayout()
h_boxlayout.addWidget(self.sequoia, 30)
h_boxlayout.addWidget(self.baobab, 70)
self.c_widget.setLayout(h_boxlayout)
self.setCentralWidget(self.c_widget)
class Sequoia(QtGui.QTreeWidget):
def __init__(self, parent=None):
super(Sequoia, self).__init__(parent=None)
self.setColumnCount(2)
self.setAcceptDrops(True)
self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
self.sampleitem = QtGui.QTreeWidgetItem()
self.sampleitem.setText(0, "a")
self.sampleitem.setText(1, "b")
self.addTopLevelItem(self.sampleitem)
class Baobab(QtGui.QTreeWidget):
def __init__(self, parent=None):
super(Baobab, self).__init__(parent=None)
self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
self.setColumnCount(2)
def dragEnterEvent(self, event):
if event.mimeData().hasFormat('application/x-qabstractitemmodeldatalist'):
event.accept()
return QtGui.QTreeWidget.dragEnterEvent(self, event)
def dragMoveEvent(self, event):
if event.mimeData().hasFormat('application/x-qabstractitemmodeldatalist') and not isinstance(event, QtGui.QDropEvent):
event.accept()
return QtGui.QTreeWidget.dragMoveEvent(self, event)
def dropEvent(self, event):
if event.mimeData().hasFormat('application/x-qabstractitemmodeldatalist'):
bytearray = event.mimeData().data('application/x-qabstractitemmodeldatalist')
datastream = QtCore.QDataStream(bytearray, QtCore.QIODevice.ReadOnly)
print(3306, bytearray.data())
item = QtGui.QTreeWidgetItem()
item.setFlags(QtCore.Qt.ItemFlag.ItemIsEditable|QtCore.Qt.ItemFlag.ItemIsEnabled|QtCore.Qt.ItemFlag.ItemIsSelectable|QtCore.Qt.ItemIsDragEnabled|QtCore.Qt.ItemIsDropEnabled)
item.read(datastream)
self.addTopLevelItem(item)
def main():
try:
QtGui.QApplication([])
except Exception as e:
print(e)
mw = MainWindow()
mw.show()
sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
main()
It is not necessary to implement your own drag-and-drop method between in QTreeWidget, you just have to configure it correctly:
from PySide import QtCore, QtGui
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=None)
self.sequoia = Sequoia()
self.baobab = Baobab()
self.c_widget = QtGui.QWidget()
h_boxlayout = QtGui.QHBoxLayout(self.c_widget)
self.setCentralWidget(self.c_widget)
h_boxlayout.addWidget(self.sequoia, 30)
h_boxlayout.addWidget(self.baobab, 70)
class Sequoia(QtGui.QTreeWidget):
def __init__(self, parent=None):
super(Sequoia, self).__init__(parent=None)
self.setColumnCount(2)
self.setDefaultDropAction(QtCore.Qt.CopyAction)
self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
self.setAcceptDrops(True)
self.sampleitem = QtGui.QTreeWidgetItem()
self.sampleitem.setText(0, "a")
self.sampleitem.setText(1, "b")
self.addTopLevelItem(self.sampleitem)
class Baobab(QtGui.QTreeWidget):
def __init__(self, parent=None):
super(Baobab, self).__init__(parent=None)
self.setColumnCount(2)
self.setAcceptDrops(True)
def main():
app = QtGui.QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
If you still want to implement it manually then if we use your perspective a possible solution is:
def dropEvent(self, event):
if event.mimeData().hasFormat(
"application/x-qabstractitemmodeldatalist"
):
ba = event.mimeData().data(
"application/x-qabstractitemmodeldatalist"
)
ds = QtCore.QDataStream(
ba, QtCore.QIODevice.ReadOnly
)
i = 0
item = QtGui.QTreeWidgetItem()
while not ds.atEnd():
row = ds.readInt32()
column = ds.readInt32()
map_items = ds.readInt32()
self.addTopLevelItem(item)
for _ in range(map_items):
role = ds.readInt32()
value = ds.readQVariant()
item.setData(i, role, value)
i = (i + 1) % self.columnCount()
But the above is forced, a better solution is to use the dropMimeData method of the model:
def dropEvent(self, event):
if event.mimeData().hasFormat(
"application/x-qabstractitemmodeldatalist"
):
parent = self.indexAt(event.pos())
self.model().dropMimeData(
event.mimeData(), event.dropAction(), 0, 0, parent
)
I need auto completion in a table. So far, I could make it work that I get the same list for the entire table.
However, I need a dynamic list for each cell. How can I get update the list when I move to a new position in the cell?
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class mainWindow(QMainWindow):
def __init__(self, parent = None):
super(mainWindow, self).__init__(parent)
self.initUI()
def initUI(self):
self.center_window = centerWindow(parent=self)
self.setCentralWidget(self.center_window)
class centerWindow(QWidget):
def __init__(self, parent=None):
super(centerWindow, self).__init__(parent)
table = QTableWidget()
table.setItemDelegate(TableItemCompleter())
table.setRowCount(5)
table.setColumnCount(1)
vbox = QVBoxLayout(self)
vbox.addWidget(table)
self.setLayout(vbox)
class TableItemCompleter(QStyledItemDelegate):
def __init__(self, parent = None):
super(TableItemCompleter, self).__init__(parent)
def createEditor(self, parent, styleOption, index):
editor = QLineEdit(parent)
completion_ls = ['aaa', 'bbb', 'ccc']
autoComplete = QCompleter(completion_ls)
editor.setCompleter(autoComplete)
return editor
if __name__ == '__main__':
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
else:
print('QApplication instance already exists: %s' % str(app))
ex = mainWindow()
ex.show()
sys.exit(app.exec_())
To make it more clear. Instead of having the completion_ls list in the TableItemCompleter, I'd like to add something like this:
table.setItem(row, column) #add my new auto completion list
QCompleter can be established a model that uses as a source for the autocomplete, we could pass the QModelIndex model that provides the method createEditor(self, parent, option, index) through index.model() but the problem is that you can only take a column and is not what is desired.
Then we must do a conversion of the tablemodel to a listmodel, and then we must filter the repeated elements so that the completer shows unique elements, one way to do them is through proxies, classes that inherit from QAbstractProxyModel, the scheme is as follows:
TableModel ----> ReadTable2ListProxyModel ----> DuplicateFilterProxyModel
En la siguiente sección muestros las clases:
class ReadTable2ListProxyModel(QIdentityProxyModel):
def columnCount(self, parent=QModelIndex()):
return 1
def rowCount(self, parent=QModelIndex()):
return self.sourceModel().rowCount() * self.sourceModel().columnCount()
def mapFromSource(self, sourceIndex):
if sourceIndex.isValid() and sourceIndex.column() == 0\
and sourceIndex.row() < self.rowCount():
r = sourceIndex.row()
c = sourceIndex.column()
row = sourceIndex.model().columnCount() * c + r
return self.index(row, 0)
return QModelIndex()
def mapToSource(self, proxyIndex):
r = proxyIndex.row() / self.sourceModel().columnCount()
c = proxyIndex.row() % self.sourceModel().columnCount()
return self.sourceModel().index(r, c)
def index(self, row, column, parent=QModelIndex()):
return self.createIndex(row, column)
class DuplicateFilterProxyModel(QSortFilterProxyModel):
def setSourceModel(self, model):
model.dataChanged.connect(lambda: self.invalidate())
QSortFilterProxyModel.setSourceModel(self, model)
def filterAcceptsRow(self, row, parent):
value = self.sourceModel().index(row, self.filterKeyColumn())\
.data(self.filterRole())
if value is None:
return False
if row == 0:
return True
for i in reversed(range(0, row)):
val = self.sourceModel().index(i, self.filterKeyColumn())\
.data(self.filterRole())
if val == value:
return False
return True
Then the conversion is established in the delegate:
class TableItemCompleter(QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QLineEdit(parent)
completer = QCompleter(parent)
proxy1 = ReadTable2ListProxyModel(parent)
proxy2 = DuplicateFilterProxyModel(parent)
proxy1.setSourceModel(index.model())
proxy2.setSourceModel(proxy1)
completer.setModel(proxy2)
editor.setCompleter(completer)
return editor
In the following link you will find an example, and in the following image the operation is illustrated, in the first widget the table is observed, in the second the conversion to list and in the third the list eliminating duplicate elements.
If you want each item to have a list what can be done is to store the list in each item through a role that is not in use as Qt.UserRole through the setData() method, and in the delegate through the method data() of the QModelIndex:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import random
class TableItemCompleter(QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QLineEdit(parent)
completion_ls = index.data(Qt.UserRole) # get list
completer = QCompleter(completion_ls, parent)
editor.setCompleter(completer)
return editor
class Widget(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
lay = QHBoxLayout(self)
tv = QTableWidget(3, 4, self)
lay.addWidget(tv)
l = ["AA", "AB", "AC", "AD", "BA", "BB", "BC"]
for i in range(tv.rowCount()):
for j in range(tv.columnCount()):
it = QTableWidgetItem(f"{i},{j}")
tv.setItem(i, j, it)
it.setData(Qt.UserRole, random.sample(l, 3)) # set list
tv.setItemDelegate(TableItemCompleter(tv))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
How can I change the text displayed in the second column of the rows selected in the treeview when the user hits 'Edit' in the UI? I'm using python and pyside but I'm not clear on how to do this.
What I want to happen is: When user clicks Edit in the UI i would like it to change the text of the selected Treeview Rows second columns. You can just change the text to say 'Hello' or something simple.
import sys
from PySide import QtGui, QtCore
class SortModel(QtGui.QSortFilterProxyModel):
def __init__(self, *args, **kwargs):
super(SortModel, self).__init__(*args, **kwargs)
def lessThan(self, left, right):
leftData = self.sourceModel().data(left)
rightData = self.sourceModel().data(right)
if leftData:
leftData = leftData.lower()
if rightData:
rightData = rightData.lower()
print('L:', leftData, 'R:', rightData)
return leftData < rightData
class Browser(QtGui.QDialog):
def __init__(self, parent=None):
super(Browser, self).__init__(parent)
self.initUI()
def initUI(self):
self.resize(200, 300)
self.setWindowTitle('Assets')
self.setModal(True)
self.results = ""
self.uiItems = QtGui.QTreeView()
self.uiItems.setAlternatingRowColors(True)
self.uiItems.setSortingEnabled(True)
self.uiItems.sortByColumn(0, QtCore.Qt.AscendingOrder)
self.uiItems.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.uiItems.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)
self.uiItems.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
self.uiItems.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self._model = self.create_model(self)
self._spmodel = SortModel(self)
self._spmodel.setSourceModel(self._model)
self._spmodel.setDynamicSortFilter(False)
self.uiItems.setModel(self._spmodel)
self.uiEdit = QtGui.QPushButton('Edit')
grid = QtGui.QGridLayout()
grid.setContentsMargins(0, 0, 0, 0)
grid.addWidget(self.uiItems, 0, 0)
grid.addWidget(self.uiEdit, 1, 0)
self.setLayout(grid)
self.uiItems.doubleClicked.connect(self.doubleClickedItem)
self.show()
def doubleClickedItem(self, idx):
name = idx.data(role=QtCore.Qt.DisplayRole)
model = idx.model()
model.setData(idx, 'great', role=QtCore.Qt.DisplayRole)
def create_model(self, parent):
items = [
'Cookie dough',
'Hummus',
'Spaghetti',
'Dal makhani',
'Chocolate whipped cream'
]
model = QtGui.QStandardItemModel()
model.setHorizontalHeaderLabels(['Name', 'Great'])
for item in items:
root = []
parentNode = QtGui.QStandardItem(item)
root.append(parentNode)
# add child row with 2 columns
for i in range(3):
row = []
col1 = QtGui.QStandardItem()
col1.setData('COLUMN 1', role=QtCore.Qt.DisplayRole)
row.append(col1)
col2 = QtGui.QStandardItem()
col2.setData('COLUMN 2', role=QtCore.Qt.DisplayRole)
row.append(col2)
parentNode.appendRow(row)
model.appendRow(root)
return model
def showEvent(self, event):
geom = self.frameGeometry()
geom.moveCenter(QtGui.QCursor.pos())
self.setGeometry(geom)
super(Browser, self).showEvent(event)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
# self.hide()
self.close()
event.accept()
else:
super(Browser, self).keyPressEvent(event)
def main():
app = QtGui.QApplication(sys.argv)
ex = Browser()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Connect the button’s clicked signal, retrieve the selected items via model.selectedIndexes, iterate over them and only handle those with .column()== 1 as in your doubleClickedItem function.
You can also change the SelectionBehavior to only select single items instead of complete rows.
If i click on any of the edit buttons, how do I get the contents of cells in the same row?
class EditButtonsWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(EditButtonsWidget,self).__init__(parent)
btnsave = QtGui.QPushButton('Save')
btnedit = QtGui.QPushButton('edit')
btndelete = QtGui.QPushButton('delete')
layout = QtGui.QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.setSpacing(0)
layout.addWidget(btnsave)
layout.addWidget(btnedit)
layout.addWidget(btndelete)
self.setLayout(layout)
class MainForm(QDialog):
def __init__(self,parent=None):
super(MainForm,self).__init__(parent)
self.resize(400,200)
self.tableWidget = QTableWidget()
layout = QVBoxLayout()
layout.addWidget( self.tableWidget)
self.setLayout(layout)
self.init_main_form()
def bb(self):
button = QtGui.QApplication.focusWidget()
index = self.tableWidget.indexAt(button.pos())
if index.isValid():
print(index.row(), index.column())
def init_main_form(self):
data =[['a','b','c','d'],['z','y','x','w']]
self.tableWidget.setColumnCount(5)
for row in data:
inx = data.index(row)
self.tableWidget.insertRow(inx)
self.tableWidget.setItem(inx,0,QTableWidgetItem(str(row[0])))
self.tableWidget.setItem(inx,1,QTableWidgetItem(str(row[1])))
self.tableWidget.setItem(inx,2,QTableWidgetItem(str(row[2])))
self.tableWidget.setItem(inx,3,QTableWidgetItem(str(row[3])))
self.tableWidget.setCellWidget(inx,4,EditButtonsWidget())
def main():
app = QApplication(sys.argv)
main_form = MainForm()
main_form.show()
app.exec_()
if __name__ == '__main__':
main()
Implemented signals and slots and now you will get idea how you can handle it
And mixing imports is not really a good idea
from PyQt4 import QtCore, QtGui
import sys
class EditButtonsWidget(QtGui.QWidget):
editCalled = QtCore.pyqtSignal(str)
def __init__(self, row, col, parent=None,):
super(EditButtonsWidget,self).__init__(parent)
self.row = row
self.col = col
self.parent = parent
btnsave = QtGui.QPushButton('Save')
btnedit = QtGui.QPushButton('edit')
btndelete = QtGui.QPushButton('delete')
layout = QtGui.QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.setSpacing(0)
layout.addWidget(btnsave)
layout.addWidget(btnedit)
layout.addWidget(btndelete)
self.setLayout(layout)
btnedit.clicked.connect(self.getAllCellVal)
#QtCore.pyqtSlot()
def getAllCellVal(self):
itmVal = {}
for col in range(0, 4):
itm = self.parent.item(self.row, col).text()
itmVal[col] = str(itm)
if itmVal:
self.editCalled.emit(str(itmVal))
class MainForm(QtGui.QDialog):
def __init__(self,parent=None):
super(MainForm,self).__init__(parent)
self.resize(400,200)
self.tableWidget = QtGui.QTableWidget()
layout = QtGui.QVBoxLayout()
layout.addWidget( self.tableWidget)
self.setLayout(layout)
self.init_main_form()
def bb(self):
button = QtGui.QApplication.focusWidget()
index = self.tableWidget.indexAt(button.pos())
if index.isValid():
print(index.row(), index.column())
def printEditVal(self, values):
print values
def init_main_form(self):
data =[['a','b','c','d'],['z','y','x','w']]
self.tableWidget.setColumnCount(5)
for row in data:
inx = data.index(row)
self.tableWidget.insertRow(inx)
self.tableWidget.setItem(inx,0,QtGui.QTableWidgetItem(str(row[0])))
self.tableWidget.setItem(inx,1,QtGui.QTableWidgetItem(str(row[1])))
self.tableWidget.setItem(inx,2,QtGui.QTableWidgetItem(str(row[2])))
self.tableWidget.setItem(inx,3,QtGui.QTableWidgetItem(str(row[3])))
buttonWid = EditButtonsWidget(inx,4, self.tableWidget)
buttonWid.editCalled.connect(self.printEditVal)
self.tableWidget.setCellWidget(inx,4,buttonWid)
def main():
app = QtGui.QApplication(sys.argv)
main_form = MainForm()
main_form.show()
app.exec_()
if __name__ == '__main__':
main()
I am trying to build a program with two tabs. In Tab1 I select point coordinates (x,y) from an image into values self.a. Besides the image I also have some other UI in Tab1 (i.e. a table). Now, I want to pass the values self.a to Tab2 (without inheriting all the other stuff). Keep in mind that self.a can be constantly updated when a new point is clicked.
from PySide import QtGui, QtCore
import pandas as pd
import pyqtgraph as pg
import numpy as np
QVariant = lambda value=None: value
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
v_global_layout = QtGui.QVBoxLayout()
v_global_layout.addWidget(TabDialog())
v_global_layout.setAlignment(QtCore.Qt.AlignTop)
self.setLayout(v_global_layout)
class TabDialog(QtGui.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
tab_widget = QtGui.QTabWidget()
tab_widget.addTab(Tab1(), "1")
tab_widget.addTab(Tab2(), "2")
main_layout = QtGui.QVBoxLayout()
main_layout.addWidget(tab_widget)
self.setLayout(main_layout)
class Tab1(QtGui.QTabWidget):
def __init__(self):
super().__init__()
layout = QtGui.QHBoxLayout()
self.fig = pg.PlotWidget(name='Example: Selecting scatter points')
self.plot_area = self.fig.plotItem
self.a = pg.ScatterPlotItem(pxMode=False)
spots = []
for i in range(10):
for j in range(10):
spots.append({'pos': (1*i, 1*j), 'size': 1, 'pen': {'color': 'w', 'width': 2},
'brush': pg.intColor(i*10+j, 100)})
self.a.addPoints(spots)
self.plot_area.addItem(self.a)
self.a.dataModel = DataFrameModel()
self.a.dataTable = QtGui.QTableView()
self.a.dataTable.setModel(self.a.dataModel)
layout.addWidget(self.a.dataTable)
layout.addWidget(self.fig)
self.setLayout(layout)
self.a.array = np.zeros((0, 2))
def clicked(self, points):
for p in points:
p.setPen('b', width=2)
position = p.viewPos()
self.array = np.append(self.array, np.array([[position.x(), position.y()]]), axis=0)
c = range(len(self.array))
c = list(map(str, c))
self.dataModel.signalUpdate(self.array, columns=c)
self.dataModel.printValues() # also: print(self.array)
self.a.sigClicked.connect(clicked)
class Tab2(QtGui.QTabWidget):
def __init__(self):
super().__init__()
layout = QtGui.QHBoxLayout()
##### Here I want to use Tab1.a and not inherit all the other stuff(layout) #####
#print("values = ", Tab1.a.array) # a should change when a new point is selected in Tab1
#####################################
self.setLayout(layout)
class DataFrameModel(QtCore.QAbstractTableModel):
""" data model for a DataFrame class """
def __init__(self):
super(DataFrameModel, self).__init__()
self.df = pd.DataFrame()
def signalUpdate(self, dataIn, columns):
self.df = pd.DataFrame(dataIn, columns)
self.layoutChanged.emit()
def printValues(self):
print("DataFrame values:\n", self.df.values)
def values(self):
return self.df.values
#------------- table display functions -----------------
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
if role != QtCore.Qt.DisplayRole:
return QVariant()
if orientation == QtCore.Qt.Horizontal:
try:
return self.df.columns.tolist()[section]
except (IndexError, ):
return QVariant()
elif orientation == QtCore.Qt.Vertical:
try:
# return self.df.index.tolist()
return self.df.index.tolist()[section]
except (IndexError, ):
return QVariant()
def data(self, index, role=QtCore.Qt.DisplayRole):
if role != QtCore.Qt.DisplayRole:
return QVariant()
if not index.isValid():
return QVariant()
return QVariant(str(self.df.ix[index.row(), index.column()]))
def rowCount(self, index=QtCore.QModelIndex()):
return self.df.shape[0]
def columnCount(self, index=QtCore.QModelIndex()):
return self.df.shape[1]
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
main_window = Widget()
main_window.setGeometry(100, 100, 640, 480)
main_window.show()
sys.exit(app.exec_())
In this case, you can just use signals to get the job done.
Here you are trying to access Tab1.a like a static property, when it is not one. Ideally, we should try and decouple the different widgets. We should try and keep the dependency between them to a minimum and treat each of them as ignorant and unaware of each other. The TabDialog can be the one that knows about each of these widgets and the connections between them (In this case, Tab1 and Tab2). And hence, the TabDialog can take the responsibility of communication between these widgets.
To do this, we have the two tabs as properties of the TabDialog class like so:
# Have the tabs as this dialog's class properties
self.tab1 = Tab1(image)
self.tab2 = Tab2()
tab_widget.addTab(self.tab1, "1")
tab_widget.addTab(self.tab2, "2")
In the class Tab2, let us assume that the value you want to map with Tab1.a is points_from_tab1_a:
class Tab2(QtGui.QTabWidget):
def __init__(self):
super().__init__()
layout = QtGui.QHBoxLayout()
self.points_from_tab1_a = []
self.setLayout(layout)
Now, in TabDialog, we connect the sigClicked signal of tab1.a to a method that updates tab2.points_from_tab1_a:
self.tab1.a.sigClicked.connect(self.pointChanged)
def pointChanged(self, points):
tab2.points_from_tab1_a = tab1.a
And that should do the trick. So, your full code snippet, after these changes, would look like:
from PySide import QtGui, QtCore
import pandas as pd
import pyqtgraph as pg
import numpy as np
QVariant = lambda value=None: value
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
v_global_layout = QtGui.QVBoxLayout()
v_global_layout.addWidget(TabDialog())
v_global_layout.setAlignment(QtCore.Qt.AlignTop)
self.setLayout(v_global_layout)
class TabDialog(QtGui.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
tab_widget = QtGui.QTabWidget()
# Have the tabs as this dialog's class properties
self.tab1 = Tab1(image)
self.tab2 = Tab2()
tab_widget.addTab(self.tab1, "1")
tab_widget.addTab(self.tab2, "2")
self.tab1.a.sigClicked.connect(self.pointChanged)
main_layout = QtGui.QVBoxLayout()
main_layout.addWidget(tab_widget)
self.setLayout(main_layout)
def pointChanged(self, points):
tab2.points_from_tab1_a = tab1.a
class Tab1(QtGui.QTabWidget):
def __init__(self):
super().__init__()
layout = QtGui.QHBoxLayout()
self.fig = pg.PlotWidget(name='Example: Selecting scatter points')
self.plot_area = self.fig.plotItem
self.a = pg.ScatterPlotItem(pxMode=False)
spots = []
for i in range(10):
for j in range(10):
spots.append({'pos': (1*i, 1*j), 'size': 1, 'pen': {'color': 'w', 'width': 2},
'brush': pg.intColor(i*10+j, 100)})
self.a.addPoints(spots)
self.plot_area.addItem(self.a)
self.a.dataModel = DataFrameModel()
self.a.dataTable = QtGui.QTableView()
self.a.dataTable.setModel(self.a.dataModel)
layout.addWidget(self.a.dataTable)
layout.addWidget(self.fig)
self.setLayout(layout)
self.a.array = np.zeros((0, 2))
def clicked(self, points):
for p in points:
p.setPen('b', width=2)
position = p.viewPos()
self.array = np.append(self.array, np.array([[position.x(), position.y()]]), axis=0)
c = range(len(self.array))
c = list(map(str, c))
self.dataModel.signalUpdate(self.array, columns=c)
self.dataModel.printValues() # also: print(self.array)
self.a.sigClicked.connect(clicked)
class Tab2(QtGui.QTabWidget):
def __init__(self):
super().__init__()
layout = QtGui.QHBoxLayout()
self.points_from_tab1_a = []
##### Here I want to use Tab1.a and not inherit all the other stuff(layout) #####
#print("values = ", Tab1.a.array) # a should change when a new point is selected in Tab1
#####################################
self.setLayout(layout)
class DataFrameModel(QtCore.QAbstractTableModel):
""" data model for a DataFrame class """
def __init__(self):
super(DataFrameModel, self).__init__()
self.df = pd.DataFrame()
def signalUpdate(self, dataIn, columns):
self.df = pd.DataFrame(dataIn, columns)
self.layoutChanged.emit()
def printValues(self):
print("DataFrame values:\n", self.df.values)
def values(self):
return self.df.values
#------------- table display functions -----------------
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
if role != QtCore.Qt.DisplayRole:
return QVariant()
if orientation == QtCore.Qt.Horizontal:
try:
return self.df.columns.tolist()[section]
except (IndexError, ):
return QVariant()
elif orientation == QtCore.Qt.Vertical:
try:
# return self.df.index.tolist()
return self.df.index.tolist()[section]
except (IndexError, ):
return QVariant()
def data(self, index, role=QtCore.Qt.DisplayRole):
if role != QtCore.Qt.DisplayRole:
return QVariant()
if not index.isValid():
return QVariant()
return QVariant(str(self.df.ix[index.row(), index.column()]))
def rowCount(self, index=QtCore.QModelIndex()):
return self.df.shape[0]
def columnCount(self, index=QtCore.QModelIndex()):
return self.df.shape[1]
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
main_window = Widget()
main_window.setGeometry(100, 100, 640, 480)
main_window.show()
sys.exit(app.exec_())
Feel free to change it to suit your needs, using the signals and slots concept. Hope this was useful.