I am having problems with the Qt method to update a DataFrame if it has a specific element modified by the user in the GUI.
For example, when I run the following code, I get a 10 by 3 DataFrame with random values displayed. If I try to change any cell to value 400, I double click, type 400 and then press enter. When I print the DataFrame, the value is still the old value. I would like the DataFrame cell to update on user changing the value.
Many thanks!
import sys
import numpy as np
import pandas as pd
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QColor
from PyQt5.QtCore import pyqtSlot, Qt, QTimer
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(700, 100, 350, 380)
self.createTable()
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.button = QPushButton('Print DataFrame', self)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.button.clicked.connect(self.print_my_df)
self.tableWidget.doubleClicked.connect(self.on_click_table)
self.show()
def createTable(self):
self.tableWidget = QTableWidget()
self.df_rows = 10
self.df_cols = 3
self.df = pd.DataFrame(np.random.randn(self.df_rows, self.df_cols))
self.tableWidget.setRowCount(self.df_rows)
self.tableWidget.setColumnCount(self.df_cols)
for i in range(self.df_rows):
for j in range(self.df_cols):
x = '{:.3f}'.format(self.df.iloc[i, j])
self.tableWidget.setItem(i, j, QTableWidgetItem(x))
#pyqtSlot()
def print_my_df(self):
print(self.df)
#pyqtSlot()
def on_click_table(self):
for currentQTableWidgetItem in self.tableWidget.selectedItems():
print((currentQTableWidgetItem.row(), currentQTableWidgetItem.column()))
self.print_my_df()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
QTableWidget does not know about the existence of the DataFrame so it is not updating it. We must update it for this we use the cellChanged signal that gives us the row and column, then we use the item() method that returns the QTableWidgetItem given the column and row, then we use the text() method of QTableWidgetItem.
The data that is placed in the items in the user's edition can be of any type for example a text and this would generate an error since the DataFrame only accepts numerical values for this we must provide an input that validates for this we place a QLineEdit with a QDoubleValidator.
class FloatDelegate(QItemDelegate):
def __init__(self, parent=None):
QItemDelegate.__init__(self, parent=parent)
def createEditor(self, parent, option, index):
editor = QLineEdit(parent)
editor.setValidator(QDoubleValidator())
return editor
class TableWidget(QTableWidget):
def __init__(self, df, parent=None):
QTableWidget.__init__(self, parent)
self.df = df
nRows = len(self.df.index)
nColumns = len(self.df.columns)
self.setRowCount(nRows)
self.setColumnCount(nColumns)
self.setItemDelegate(FloatDelegate())
for i in range(self.rowCount()):
for j in range(self.columnCount()):
x = '{:.3f}'.format(self.df.iloc[i, j])
self.setItem(i, j, QTableWidgetItem(x))
self.cellChanged.connect(self.onCellChanged)
#pyqtSlot(int, int)
def onCellChanged(self, row, column):
text = self.item(row, column).text()
number = float(text)
self.df.set_value(row, column, number)
Example:
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(700, 100, 350, 380)
df_rows = 10
df_cols = 3
df = pd.DataFrame(np.random.randn(df_rows, df_cols))
self.tableWidget = TableWidget(df, self)
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.button = QPushButton('Print DataFrame', self)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.button.clicked.connect(self.print_my_df)
#pyqtSlot()
def print_my_df(self):
print(self.tableWidget.df)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
Related
I have recently created a widget with Qpaint, which I want to pass value to it, at the same time force the Qpaint Widget to draw from input values. The idea is to define a data value from a Qdialog and pass it to main widget, and pass the value to Qpaint Widget class. I would like to have, when user clicks on the button 'Getting values' a dialog widget would appear and insert some int values, then pass it to main Widget. from there pass value to correct class Paint. Which would draw and display the result. I tried with Qlabel, to assign value first to Qlabel or QlineEdit,
class Button(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Button, self).__init__(parent)
---------
self.value = QtWidgets.QLabel()
--------
Then inside the paint class call the value or text of those. then assign it to Qpaint event. But seems does not work.'
class Paint(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Paint, self).__init__(parent)
self.button = Button()
self.Value = self.button.value
---------
painter.drawRect(100,100,250,250) <----- instead of value 250 having self.Value
The code Main.py
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from datainput import *
class Foo(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Foo, self).__init__(parent)
self.setGeometry(QtCore.QRect(200, 100, 800, 800))
self.button = Button()
self.paint = Paint()
self.lay = QtWidgets.QVBoxLayout()
self.lay.addWidget(self.paint)
self.lay.addWidget(self.button)
self.setLayout(self.lay)
class Paint(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Paint, self).__init__(parent)
self.button = Button()
self.Value = self.button.value
self.setBackgroundRole(QtGui.QPalette.Base)
self.setAutoFillBackground(True)
def paintEvent(self, event):
self.pen = QtGui.QPen()
self.brush = QtGui.QBrush( QtCore.Qt.gray, QtCore.Qt.Dense7Pattern)
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(self.pen)
painter.setBrush(self.brush)
painter.drawRect(100,100,250,250)
painter.setBrush(QtGui.QBrush())
class Button(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Button, self).__init__(parent)
getbutton = QtWidgets.QPushButton('Getting values')
Alay = QtWidgets.QVBoxLayout(self)
Alay.addWidget(getbutton)
self.value = QtWidgets.QLabel()
getbutton.clicked.connect(self.getbuttonfunc)
def getbuttonfunc(self):
subwindow=Dinput()
subwindow.setWindowModality(QtCore.Qt.ApplicationModal)
if subwindow.exec_() == QtWidgets.QDialog.Accepted:
self._output = subwindow.valueEdit.text()
return self.value.setText(self._output)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Foo()
w.show()
sys.exit(app.exec_())
Input Qdialog code, datainput.py
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Dinput(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Dinput, self).__init__(parent)
valuelabel = QtWidgets.QLabel('Input: ')
self.valueEdit = QtWidgets.QLineEdit()
buttonBox = QtWidgets.QDialogButtonBox()
buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
buttonBox.accepted.connect(self.accept)
buttonBox.rejected.connect(self.close)
self.Alay = QtWidgets.QHBoxLayout()
self.Alay.addWidget(valuelabel)
self.Alay.addWidget(self.valueEdit)
self.Blay = QtWidgets.QVBoxLayout()
self.Blay.addLayout(self.Alay)
self.Blay.addWidget(buttonBox)
self.setLayout(self.Blay)
def closeEvent(self, event):
super(Dinput, self).closeEvent(event)
def accept(self):
super(Dinput, self).accept()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Dinput()
w.show()
sys.exit(app.exec_())
Visualization
I appreciate any help. Thankssss
datainput is irrelevant, your task is only to obtain a number so for space question I will not use it and instead I will use QInputDialog::getInt(). Going to the problem, the strategy in these cases where the value can be obtained at any time is to notify the change to the other view through a signal, in the slot that receives the value is to update a variable that stores the value and call update so that it calls when necessary to paintEvent, and in the paintEvent use the variable that stores the value.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Foo(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Foo, self).__init__(parent)
self.setGeometry(QtCore.QRect(200, 100, 800, 800))
self.button = Button()
self.paint = Paint()
self.button.valueChanged.connect(self.paint.set_size_square)
self.lay = QtWidgets.QVBoxLayout(self)
self.lay.addWidget(self.paint)
self.lay.addWidget(self.button)
class Paint(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Paint, self).__init__(parent)
self.setBackgroundRole(QtGui.QPalette.Base)
self.setAutoFillBackground(True)
self._size_square = 250
#QtCore.pyqtSlot(int)
def set_size_square(self, v):
self._size_square = v
self.update()
def paintEvent(self, event):
pen = QtGui.QPen()
brush = QtGui.QBrush( QtCore.Qt.gray, QtCore.Qt.Dense7Pattern)
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(pen)
painter.setBrush(brush)
r = QtCore.QRect(QtCore.QPoint(100, 100), self._size_square*QtCore.QSize(1, 1))
painter.drawRect(r)
class Button(QtWidgets.QWidget):
valueChanged = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(Button, self).__init__(parent)
getbutton = QtWidgets.QPushButton('Getting values')
Alay = QtWidgets.QVBoxLayout(self)
Alay.addWidget(getbutton)
self.value = QtWidgets.QLabel()
getbutton.clicked.connect(self.getbuttonfunc)
#QtCore.pyqtSlot()
def getbuttonfunc(self):
number, ok = QtWidgets.QInputDialog.getInt(self, self.tr("Set Number"),
self.tr("Input:"), 1, 1)
if ok:
self.valueChanged.emit(number)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Foo()
w.show()
sys.exit(app.exec_())
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 tab widget that contains table widgets for each tab, and I also have a dock widget with options. One of the options is the column count, and I want to change it as soon as the column count spin box value changes. But when switching to a different tab, I'd like the spin box value (and all other options) to reset / switch to that specific tab's settings.
My question is how to best do this and still have the options as a dock widget. I could store all settings as variables for each tab widget and then change the value each time a new tab is opened, I guess, but maybe there is a better solution.
from PyQt5 import QtWidgets, QtCore
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__()
self.__setup__()
def __setup__(self):
self.resize(400, 400)
tabWidget = TabWidget(self)
self.setCentralWidget(tabWidget)
options = Options(self)
optionsDock = QtWidgets.QDockWidget()
optionsDock.setWidget(options)
optionsDock.setWindowTitle("Options")
self.addDockWidget(QtCore.Qt.TopDockWidgetArea, optionsDock)
options.spinBox_columns.valueChanged.connect(lambda: tabWidget.tabWidget.currentWidget().
setColumnCount(options.spinBox_columns.value()))
class Options(QtWidgets.QWidget):
def __init__(self, parent):
super(Options, self).__init__(parent)
self.__setup__()
def __setup__(self):
self.spinBox_columns = QtWidgets.QSpinBox()
self.spinBox_columns.setValue(1)
self.spinBox_columns.setMinimum(1)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.spinBox_columns)
self.setLayout(layout)
class TabWidget(QtWidgets.QWidget):
def __init__(self, parent):
super(TabWidget, self).__init__(parent)
self.__setup__()
def __setup__(self):
self.tabWidget = QtWidgets.QTabWidget()
for i in range(3):
widget = QtWidgets.QTableWidget()
widget.setColumnCount(1)
widget.setRowCount(3)
self.tabWidget.addTab(widget, "Column " + str(i))
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.tabWidget)
self.setLayout(layout)
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
You have to connect the currentChanged signal provided by the index of the tab, then use the widget() method to obtain the index associated with that index, then access its QTabWidget and obtain the number of columns using it to place the value to the QSpinBox.
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.__setup__()
def __setup__(self):
self.resize(400, 400)
tabWidget = TabWidget(self)
self.setCentralWidget(tabWidget)
options = Options(self)
optionsDock = QtWidgets.QDockWidget()
optionsDock.setWidget(options)
optionsDock.setWindowTitle("Options")
self.addDockWidget(QtCore.Qt.TopDockWidgetArea, optionsDock)
tabWidget.tabWidget.currentChanged.connect(lambda index: options.spinBox_columns.
setValue(tabWidget.tabWidget.widget(index).columnCount()))
options.spinBox_columns.valueChanged.connect(lambda value: tabWidget.tabWidget.currentWidget().
setColumnCount(value))
I have a problem setting the background color of a QWidgetItem based on a value changed.
I have the following set up that is simply generating random numbers into a QTableWidget based on a click of a button.
I would like to make it that the background of a cell changes based if the new value is higher or lower than the old value. For example, if new value is higher, flash blue for a second (or half a second) or if new value is lower to flash/blink for yellow for some time.
I'm quite lost on where to start in this process.
Many thanks
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from random import randint
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 350, 380)
self.createTable()
self.button = QPushButton('Update Values', self)
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.button.clicked.connect(self.on_click)
self.show()
def createTable(self):
self.tableWidget = QTableWidget()
self.nrows=10
self.ncols=3
self.tableWidget.setRowCount(self.nrows)
self.tableWidget.setColumnCount(self.ncols)
for i in range(self.nrows):
for j in range(self.ncols):
self.tableWidget.setItem(i, j, QTableWidgetItem('{}'.format(randint(0,9))))
self.tableWidget.move(0,0)
self.tableWidget.doubleClicked.connect(self.on_click)
#pyqtSlot()
def on_click(self):
for i in range(self.nrows):
for j in range(self.ncols):
self.tableWidget.setItem(i, j, QTableWidgetItem('{}'.format(randint(0,9))))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
There are probably many different ways to implement this. One way is to create a sub-class of QTableWidgetItem and override the setData method. This will allow you to monitor which values are changing for a specific item data role. (If you used a signal like QTableWidgem.itemChanged(), this wouldn't be possible, because it doesn't give you the role).
The only thing to bear in mind about doing things this way, is that it can only monitor changes after the item has been added to the table. So you will need to add blank items first, and then update all the values afterwards.
The mechanism for changing the background color is much simpler, since it only requires a single-shot timer.
Here is a demo of all of the above based on your example:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QColor
from PyQt5.QtCore import pyqtSlot, Qt, QTimer
from random import randint
class TableWidgetItem(QTableWidgetItem):
def setData(self, role, value):
if role == Qt.DisplayRole:
try:
newvalue = int(value)
oldvalue = int(self.data(role))
except (ValueError, TypeError):
pass
else:
if newvalue != oldvalue:
if newvalue > oldvalue:
color = QColor('aliceblue')
elif newvalue < oldvalue:
color = QColor('lightyellow')
def update_background(color=None):
super(TableWidgetItem, self).setData(
Qt.BackgroundRole, color)
update_background(color)
QTimer.singleShot(2000, update_background)
super(TableWidgetItem, self).setData(role, value)
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(700, 100, 350, 380)
self.createTable()
self.button = QPushButton('Update Values', self)
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.button.clicked.connect(self.populateTable)
self.show()
def createTable(self):
self.tableWidget = QTableWidget()
self.nrows=10
self.ncols=3
self.tableWidget.setRowCount(self.nrows)
self.tableWidget.setColumnCount(self.ncols)
for i in range(self.nrows):
for j in range(self.ncols):
self.tableWidget.setItem(i, j, TableWidgetItem())
self.tableWidget.move(0,0)
self.tableWidget.doubleClicked.connect(self.populateTable)
self.populateTable()
#pyqtSlot()
def populateTable(self):
for i in range(self.nrows):
for j in range(self.ncols):
self.tableWidget.item(i, j).setText('{}'.format(randint(0,9)))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
I have QTableView that gets information from a QSqlQueryModel and displays it in real time. The thing is, QTableView allows the user to copy and paste the info from one of the fields.
projectModel = QtSql.QSqlQueryModel()
projectModel.setQuery("select * from queue",self.db)
self.total_rows = projectModel.rowCount()
projectModel.setHeaderData(0, QtCore.Qt.Horizontal, 'ID cola')
projectModel.setHeaderData(1, QtCore.Qt.Horizontal, 'Código')
self.projectView = QtGui.QTableView()
self.projectView.setModel(projectModel)
self.projectView.resizeColumnsToContents()
self.projectView.horizontalHeader().setStretchLastSection(True)
How do I deny copying the content of QTableView and pasting it outside in a text editor, for example?
You can make the whole table read-only like this:
self.projectView.setEditTriggers(QAbstractItemView.NoEditTriggers)
EDIT:
If you also want to prevent copying of cells, you will need to kill the relevant keyboard shortcuts. Below is some example code that does that:
from PySide import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self, rows, columns):
super(Window, self).__init__()
self.table = QtGui.QTableView(self)
model = QtGui.QStandardItemModel(rows, columns, self.table)
for row in range(rows):
for column in range(columns):
item = QtGui.QStandardItem('(%d, %d)' % (row, column))
model.setItem(row, column, item)
self.table.setModel(model)
self.table.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.table)
self.table.installEventFilter(self)
def eventFilter(self, source, event):
if (source is self.table and
event.type() == QtCore.QEvent.KeyPress and
event == QtGui.QKeySequence.Copy):
return True
return super(Window, self).eventFilter(source, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window(5, 5)
window.setGeometry(600, 300, 600, 250)
window.show()
sys.exit(app.exec_())