Solved Generate tablewidget name on selecting range
-
Hi,
I need to generate tablewidget name on selecting range inside the tablewidget. Currently it works while i select a single cell, but when I select a range of cells, then it does not provide the table name. Code snippet shared:
self.ui.table1.cellClicked.connect(self.get_obj_name) def get_obj_name(self, row, col): abc= self.sender() table = abc.objectName() self.table123 = "self.ui." + table self.table123 = QTableWidget()
-
@Piyush What is the use case? Why do you need to generate object name like this? Sounds quite unusual...
And also: you're actually not generating object name but variable name (at least you try)!
This, somehow, does not make sense:self.table123 = "self.ui." + table self.table123 = QTableWidget()
First you assign a string and then a QTableWidget instance. So, the result will be that self.table123 will contain a QTableWidget() instance, doesn't matter what you selected.
Is it possible that you actually want to use http://doc.qt.io/qt-5/qobject.html#objectName-prop? -
@jsulm I am trying to generate signal for retrieving object name for a generic copy function, which would work on any tablewidget, whenever we select a range. So, when we select multiples cells/range in a tablewidget it will copy the selected area.
Please do not focus on the string part. The real question is how we can get the object name when we select a range within tablewidget.
-
@Piyush Isn't sender() what you need? It returns you the object which emitted the signal (selection in this case).
-
@jsulm sender() provides the object name when we select a single cell withing the object. However, in my case I need to select a range of cells. So, sender() is not providing any signal if I select a range.
I guess i need to change the cellClicked.connect to something else, which corresponds to selecting range?
-
@Piyush As far as I know you get http://doc.qt.io/qt-5/qtablewidget.html#itemSelectionChanged even if more than one cell is selected and you can get selected cells using http://doc.qt.io/qt-5/qtablewidget.html#selectedItems.
Calling sender() in slot connected to itemSelectionChanged signal should give you the table widget where the selection was done. -
@jsulm I am quite new to Qt. Can you please share an example in Python?
-
@jsulm You are correct on this thing. Implementing it in this way is wrong as it will return a QTableWidget instance and not the name.
self.table123 = "self.ui." + table
This provides a string instead of <class 'PyQt5.QtWidgets.QTableWidget'>. Can you please advise how I can get it as <class 'PyQt5.QtWidgets.QTableWidget'>??
-
@Piyush Did you try to use sender()? As I said it should work even if you select more than one cell. Simply connect as slot to itemSelectionChanged signal and call sender() there.
-
@jsulm I am not sure how to use the itemSelectionChanged signal. Is it like below:
self.ui.xray_tw1.itemSelectionChanged.connect(self.get_obj_name) def get_obj_name(self): abc= self.sender() table = abc.objectName()
-
@Piyush What is the problem now? I don't know what you want to achieve and what does not work now.
-
This post is deleted! -
This post is deleted! -
@jsulm Sorry for the confusion. I got it now. Thanks for being patient with me.