How to simulate a left click in a (row,column) of a QTableWidget instance?

218 views Asked by At

To do unit tests I need to simulate a left click in a table made by QTableWidget.

I can't do it with QTest.mouseClick(Something, Qt.LeftButton).

How can I do it?

By taking a very simple example:

>>> import sys 
>>> from PyQt5.QtWidgets import * 
>>>                     
>>>    
>>> class App(QWidget): 
...     def __init__(self): 
...         super().__init__() 
...         self.title = 'PyQt5 - QTableWidget'
...         self.left = 0
...         self.top = 0
...         self.width = 300
...         self.height = 200
...    
...         self.setWindowTitle(self.title) 
...         self.setGeometry(self.left, self.top, self.width, self.height) 
...    
...         self.createTable() 
...    
...         self.layout = QVBoxLayout() 
...         self.layout.addWidget(self.tableWidget) 
...         self.setLayout(self.layout) 
...    
...         
...         self.show() 
...    
...     
...     def createTable(self): 
...         self.tableWidget = QTableWidget() 
...   
...         
...         self.tableWidget.setRowCount(4)  
...   
...         
...         self.tableWidget.setColumnCount(2)   
...   
...         self.tableWidget.setItem(0,0, QTableWidgetItem("Name")) 
...         self.tableWidget.setItem(0,1, QTableWidgetItem("City")) 
...         self.tableWidget.setItem(1,0, QTableWidgetItem("Aloysius")) 
...         self.tableWidget.setItem(1,1, QTableWidgetItem("Indore")) 
...         self.tableWidget.setItem(2,0, QTableWidgetItem("Alan")) 
...         self.tableWidget.setItem(2,1, QTableWidgetItem("Bhopal")) 
...         self.tableWidget.setItem(3,0, QTableWidgetItem("Arnavi")) 
...         self.tableWidget.setItem(3,1, QTableWidgetItem("Mandsaur")) 
...    
...         
...         self.tableWidget.horizontalHeader().setStretchLastSection(True) 
...         self.tableWidget.horizontalHeader().setSectionResizeMode( 
...             QHeaderView.Stretch)
...
>>> app = QApplication(sys.argv)
>>> ex = App()

Let's say I want to simulate a click on "City". I look for something like:

QTest.mouseClick(self.tableWidget.cellWidget(0, 1), Qt.LeftButton) #does not work!
0

There are 0 answers