Retrieve QTableWidget name when clicked/select cells in tablewdiget
-
-
Hi
What do you mean by "tablewidget name" ?
Do you mean the text on the item(s) you click on ?
There is signals for that.If you can have one or more selected, there is also
http://doc.qt.io/qt-5/qtablewidget.html#selectedItems -
Hi
What do you mean by "tablewidget name" ?
Do you mean the text on the item(s) you click on ?
There is signals for that.If you can have one or more selected, there is also
http://doc.qt.io/qt-5/qtablewidget.html#selectedItems@mrjj So I have multiple tablewidgets in my GUI. I actually need to find the selected cell/item falls under which tablewidget? Is there any signal for that?
So whenever i select any item or cell in a tablewidget, it should give me the tablewidget's name.
-
@mrjj So I have multiple tablewidgets in my GUI. I actually need to find the selected cell/item falls under which tablewidget? Is there any signal for that?
So whenever i select any item or cell in a tablewidget, it should give me the tablewidget's name.
@Piyush
Hi
But how come there can be any doubts ??Do you have hook all tablewidgets to same slot or how come its even an issue ?
You have to explain why you need this as normally its non issue :)
u want to hangle item slection from same slot for all tablewidgets or why
do u need name ? -
@Piyush
Hi
But how come there can be any doubts ??Do you have hook all tablewidgets to same slot or how come its even an issue ?
You have to explain why you need this as normally its non issue :)
u want to hangle item slection from same slot for all tablewidgets or why
do u need name ?@mrjj Well, the bigger picture is, I want to copy selected cells irrespective of the tablewidget I choose. Currently, I have a code by which I can copy selected cells for a particular table. But I want to make it dynamic in the sense that I do not have to provide tablewidget name manually. It should automatically understand which tablewidget has the selected cells.
def keyPressEvent(self, e): if (e.modifiers() & QtCore.Qt.ControlModifier): self.table = self.ui.tableWidget selected = self.table.selectedRanges() s = '' if e.key() == QtCore.Qt.Key_C: #copy if self.table.horizontalHeaderItem(0): s = '\t'+"\t".join([str(self.table.horizontalHeaderItem(i).text()) for i in xrange(selected[0].leftColumn(), selected[0].rightColumn()+1)]) s = s + '\n' for r in xrange(selected[0].topRow(), selected[0].bottomRow()+1): if self.table.verticalHeaderItem(0): s += self.table.verticalHeaderItem(r).text() + '\t' for c in xrange(selected[0].leftColumn(), selected[0].rightColumn()+1): try: s += str(self.table.item(r,c).text()) + "\t" except AttributeError: s += "\t" s = s[:-1] + "\n" #eliminate last '\t' self.clip.setText(s)
-
Ok so its for generic handling.
If using signals
there is the sender() to know whom emitted the signal.I assume QTableWidgetItem have a parent or something like that that would also tell you name.
(dont have docs here. if not, you can subclass and add it)
and there is QTableWidgetItem::setData() where you can simply stuff the info and read when needed.
( this should be pretty easy even if a bit wastefull since all items then have it :) -
Ok so its for generic handling.
If using signals
there is the sender() to know whom emitted the signal.I assume QTableWidgetItem have a parent or something like that that would also tell you name.
(dont have docs here. if not, you can subclass and add it)
and there is QTableWidgetItem::setData() where you can simply stuff the info and read when needed.
( this should be pretty easy even if a bit wastefull since all items then have it :) -
@mrjj i tried using self.sender(), but it gives the mainwindow name.
How do i subclass it and get the name? -
@jsulm I added it in the keypressevent function shared above, which gives me mainwindow as the sender. This was my bad.
However, now i created a cellclicked signal for all the tables as below to get the name and it works. Now the issue is keypressevent function is not responding (not copying the selected values). I made self.table as a class global variable, in which i have both the functions
def __init__(self): self.table = '' self.ui.table1.cellClicked.connect(self.get_obj_name) self.ui.table2.cellClicked.connect(self.get_obj_name) self.ui.table3.cellClicked.connect(self.get_obj_name) def get_obj_name(self, row, col): abc= self.sender() table = abc.objectName() self.table = "self.ui." + table
its a type issue i guess. The string i made is <type 'unicode'>, however, I should pass <class 'PyQt5.QtWidgets.QTableWidget'>
How do i correct this issue?