MouseMove event on cell in Qtablewidget to display or print a message
I have QTablewidget and I want to use a mousemoveevent on a particular cell B
. When mouse moves over this cell B
, a message would appear or be printed. I have created the constructor, but it really does not work. Every thing is allright expect from those lines of code.
def mouseMoveEvent(self, event):
it = self.item(self.rowCount(),1)
it.QToolTip.showText('Insert')
self.onHovered()
Keep in mind that Qtooltip is assigned when the cell is clicked and works. But I want to do this by MouseMoveevent. Maybe my constructor of MouseEvent code is not right.
Expecting to behave.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
def copy_widget(w):
if isinstance(w, QtWidgets.QWidget):
new_w = type(w)()
if isinstance(w, QtWidgets.QComboBox):
vals = [w.itemText(ix) for ix in range(w.count())]
new_w.addItems(vals)
return new_w
class LoadTable(QtWidgets.QTableWidget):
def __init__(self, parent=None):
super(LoadTable, self).__init__(1, 5, parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
headertitle = ("A","B","C","D","E")
self.setHorizontalHeaderLabels(headertitle)
self.verticalHeader().hide()
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setColumnWidth(0, 130)
combox_lay = QtWidgets.QComboBox(self)
combox_lay.addItems(["I","II"])
self.setCellWidget(0, 4, combox_lay)
self.cellChanged.connect(self._cellclicked)
@QtCore.pyqtSlot(int, int)
def _cellclicked(self, r, c):
it = self.item(r, c)
it.setTextAlignment(QtCore.Qt.AlignCenter)
n_it = self.item(r,1)
n_it.setToolTip('Test')
@QtCore.pyqtSlot()
def _addrow(self):
rowcount = self.rowCount()
self.insertRow(rowcount)
combox_add = QtWidgets.QComboBox(self)
combox_add.addItems(["I","II"])
self.setCellWidget(rowcount, 4, combox_add)
@QtCore.pyqtSlot()
def _removerow(self):
if self.rowCount() > 0:
self.removeRow(self.rowCount()-1)
@QtCore.pyqtSlot()
def _copyrow(self):
r = self.currentRow()
if 0 <= r < self.rowCount():
cells = {"items": , "widgets": }
for i in range(self.columnCount()):
it = self.item(r, i)
if it:
cells["items"].append((i, it.clone()))
w = self.cellWidget(r, i)
if w:
cells["widgets"].append((i, copy_widget(w)))
self.copy(cells, r+1)
def copy(self, cells, r):
self.insertRow(r)
for i, it in cells["items"]:
self.setItem(r, i, it)
for i, w in cells["widgets"]:
self.setCellWidget(r, i, w)
def mouseMoveEvent(self, event):
it = self.item(self.rowCount(),1)
it.QToolTip.showText('Insert')
self.onHovered()
def onHovered(self):
print("Works")
class ThirdTabLoads(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ThirdTabLoads, self).__init__(parent)
table = LoadTable()
add_button = QtWidgets.QPushButton("Add")
add_button.clicked.connect(table._addrow)
delete_button = QtWidgets.QPushButton("Delete")
delete_button.clicked.connect(table._removerow)
copy_button = QtWidgets.QPushButton("Copy")
copy_button.clicked.connect(table._copyrow)
button_layout = QtWidgets.QVBoxLayout()
button_layout.addWidget(add_button, alignment=QtCore.Qt.AlignBottom)
button_layout.addWidget(delete_button, alignment=QtCore.Qt.AlignTop)
button_layout.addWidget(copy_button, alignment=QtCore.Qt.AlignTop)
tablehbox = QtWidgets.QHBoxLayout()
tablehbox.setContentsMargins(10, 10, 10, 10)
tablehbox.addWidget(table)
grid = QtWidgets.QGridLayout(self)
grid.addLayout(button_layout, 0, 1)
grid.addLayout(tablehbox, 0, 0)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = ThirdTabLoads()
w.show()
sys.exit(app.exec_())
python pyqt pyqt5 qtablewidget qmouseevent
add a comment |
I have QTablewidget and I want to use a mousemoveevent on a particular cell B
. When mouse moves over this cell B
, a message would appear or be printed. I have created the constructor, but it really does not work. Every thing is allright expect from those lines of code.
def mouseMoveEvent(self, event):
it = self.item(self.rowCount(),1)
it.QToolTip.showText('Insert')
self.onHovered()
Keep in mind that Qtooltip is assigned when the cell is clicked and works. But I want to do this by MouseMoveevent. Maybe my constructor of MouseEvent code is not right.
Expecting to behave.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
def copy_widget(w):
if isinstance(w, QtWidgets.QWidget):
new_w = type(w)()
if isinstance(w, QtWidgets.QComboBox):
vals = [w.itemText(ix) for ix in range(w.count())]
new_w.addItems(vals)
return new_w
class LoadTable(QtWidgets.QTableWidget):
def __init__(self, parent=None):
super(LoadTable, self).__init__(1, 5, parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
headertitle = ("A","B","C","D","E")
self.setHorizontalHeaderLabels(headertitle)
self.verticalHeader().hide()
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setColumnWidth(0, 130)
combox_lay = QtWidgets.QComboBox(self)
combox_lay.addItems(["I","II"])
self.setCellWidget(0, 4, combox_lay)
self.cellChanged.connect(self._cellclicked)
@QtCore.pyqtSlot(int, int)
def _cellclicked(self, r, c):
it = self.item(r, c)
it.setTextAlignment(QtCore.Qt.AlignCenter)
n_it = self.item(r,1)
n_it.setToolTip('Test')
@QtCore.pyqtSlot()
def _addrow(self):
rowcount = self.rowCount()
self.insertRow(rowcount)
combox_add = QtWidgets.QComboBox(self)
combox_add.addItems(["I","II"])
self.setCellWidget(rowcount, 4, combox_add)
@QtCore.pyqtSlot()
def _removerow(self):
if self.rowCount() > 0:
self.removeRow(self.rowCount()-1)
@QtCore.pyqtSlot()
def _copyrow(self):
r = self.currentRow()
if 0 <= r < self.rowCount():
cells = {"items": , "widgets": }
for i in range(self.columnCount()):
it = self.item(r, i)
if it:
cells["items"].append((i, it.clone()))
w = self.cellWidget(r, i)
if w:
cells["widgets"].append((i, copy_widget(w)))
self.copy(cells, r+1)
def copy(self, cells, r):
self.insertRow(r)
for i, it in cells["items"]:
self.setItem(r, i, it)
for i, w in cells["widgets"]:
self.setCellWidget(r, i, w)
def mouseMoveEvent(self, event):
it = self.item(self.rowCount(),1)
it.QToolTip.showText('Insert')
self.onHovered()
def onHovered(self):
print("Works")
class ThirdTabLoads(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ThirdTabLoads, self).__init__(parent)
table = LoadTable()
add_button = QtWidgets.QPushButton("Add")
add_button.clicked.connect(table._addrow)
delete_button = QtWidgets.QPushButton("Delete")
delete_button.clicked.connect(table._removerow)
copy_button = QtWidgets.QPushButton("Copy")
copy_button.clicked.connect(table._copyrow)
button_layout = QtWidgets.QVBoxLayout()
button_layout.addWidget(add_button, alignment=QtCore.Qt.AlignBottom)
button_layout.addWidget(delete_button, alignment=QtCore.Qt.AlignTop)
button_layout.addWidget(copy_button, alignment=QtCore.Qt.AlignTop)
tablehbox = QtWidgets.QHBoxLayout()
tablehbox.setContentsMargins(10, 10, 10, 10)
tablehbox.addWidget(table)
grid = QtWidgets.QGridLayout(self)
grid.addLayout(button_layout, 0, 1)
grid.addLayout(tablehbox, 0, 0)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = ThirdTabLoads()
w.show()
sys.exit(app.exec_())
python pyqt pyqt5 qtablewidget qmouseevent
And is the mouse passed through cell A or cell C that must happen?
– eyllanesc
Jan 1 at 23:32
If mouse passed through cell A and the rest expect from cell B must do nothing. that is why I only markit = self.item(self.rowCount(),1)
.
– Pavel.D
Jan 1 at 23:37
Only in cell B should the tooltip be displayed?
– eyllanesc
Jan 1 at 23:38
Yes exactly, andQtooltips show text
is recommended to be used according pyqt5 documentation, because it is faster to react and display
– Pavel.D
Jan 1 at 23:39
add a comment |
I have QTablewidget and I want to use a mousemoveevent on a particular cell B
. When mouse moves over this cell B
, a message would appear or be printed. I have created the constructor, but it really does not work. Every thing is allright expect from those lines of code.
def mouseMoveEvent(self, event):
it = self.item(self.rowCount(),1)
it.QToolTip.showText('Insert')
self.onHovered()
Keep in mind that Qtooltip is assigned when the cell is clicked and works. But I want to do this by MouseMoveevent. Maybe my constructor of MouseEvent code is not right.
Expecting to behave.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
def copy_widget(w):
if isinstance(w, QtWidgets.QWidget):
new_w = type(w)()
if isinstance(w, QtWidgets.QComboBox):
vals = [w.itemText(ix) for ix in range(w.count())]
new_w.addItems(vals)
return new_w
class LoadTable(QtWidgets.QTableWidget):
def __init__(self, parent=None):
super(LoadTable, self).__init__(1, 5, parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
headertitle = ("A","B","C","D","E")
self.setHorizontalHeaderLabels(headertitle)
self.verticalHeader().hide()
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setColumnWidth(0, 130)
combox_lay = QtWidgets.QComboBox(self)
combox_lay.addItems(["I","II"])
self.setCellWidget(0, 4, combox_lay)
self.cellChanged.connect(self._cellclicked)
@QtCore.pyqtSlot(int, int)
def _cellclicked(self, r, c):
it = self.item(r, c)
it.setTextAlignment(QtCore.Qt.AlignCenter)
n_it = self.item(r,1)
n_it.setToolTip('Test')
@QtCore.pyqtSlot()
def _addrow(self):
rowcount = self.rowCount()
self.insertRow(rowcount)
combox_add = QtWidgets.QComboBox(self)
combox_add.addItems(["I","II"])
self.setCellWidget(rowcount, 4, combox_add)
@QtCore.pyqtSlot()
def _removerow(self):
if self.rowCount() > 0:
self.removeRow(self.rowCount()-1)
@QtCore.pyqtSlot()
def _copyrow(self):
r = self.currentRow()
if 0 <= r < self.rowCount():
cells = {"items": , "widgets": }
for i in range(self.columnCount()):
it = self.item(r, i)
if it:
cells["items"].append((i, it.clone()))
w = self.cellWidget(r, i)
if w:
cells["widgets"].append((i, copy_widget(w)))
self.copy(cells, r+1)
def copy(self, cells, r):
self.insertRow(r)
for i, it in cells["items"]:
self.setItem(r, i, it)
for i, w in cells["widgets"]:
self.setCellWidget(r, i, w)
def mouseMoveEvent(self, event):
it = self.item(self.rowCount(),1)
it.QToolTip.showText('Insert')
self.onHovered()
def onHovered(self):
print("Works")
class ThirdTabLoads(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ThirdTabLoads, self).__init__(parent)
table = LoadTable()
add_button = QtWidgets.QPushButton("Add")
add_button.clicked.connect(table._addrow)
delete_button = QtWidgets.QPushButton("Delete")
delete_button.clicked.connect(table._removerow)
copy_button = QtWidgets.QPushButton("Copy")
copy_button.clicked.connect(table._copyrow)
button_layout = QtWidgets.QVBoxLayout()
button_layout.addWidget(add_button, alignment=QtCore.Qt.AlignBottom)
button_layout.addWidget(delete_button, alignment=QtCore.Qt.AlignTop)
button_layout.addWidget(copy_button, alignment=QtCore.Qt.AlignTop)
tablehbox = QtWidgets.QHBoxLayout()
tablehbox.setContentsMargins(10, 10, 10, 10)
tablehbox.addWidget(table)
grid = QtWidgets.QGridLayout(self)
grid.addLayout(button_layout, 0, 1)
grid.addLayout(tablehbox, 0, 0)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = ThirdTabLoads()
w.show()
sys.exit(app.exec_())
python pyqt pyqt5 qtablewidget qmouseevent
I have QTablewidget and I want to use a mousemoveevent on a particular cell B
. When mouse moves over this cell B
, a message would appear or be printed. I have created the constructor, but it really does not work. Every thing is allright expect from those lines of code.
def mouseMoveEvent(self, event):
it = self.item(self.rowCount(),1)
it.QToolTip.showText('Insert')
self.onHovered()
Keep in mind that Qtooltip is assigned when the cell is clicked and works. But I want to do this by MouseMoveevent. Maybe my constructor of MouseEvent code is not right.
Expecting to behave.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
def copy_widget(w):
if isinstance(w, QtWidgets.QWidget):
new_w = type(w)()
if isinstance(w, QtWidgets.QComboBox):
vals = [w.itemText(ix) for ix in range(w.count())]
new_w.addItems(vals)
return new_w
class LoadTable(QtWidgets.QTableWidget):
def __init__(self, parent=None):
super(LoadTable, self).__init__(1, 5, parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
headertitle = ("A","B","C","D","E")
self.setHorizontalHeaderLabels(headertitle)
self.verticalHeader().hide()
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setColumnWidth(0, 130)
combox_lay = QtWidgets.QComboBox(self)
combox_lay.addItems(["I","II"])
self.setCellWidget(0, 4, combox_lay)
self.cellChanged.connect(self._cellclicked)
@QtCore.pyqtSlot(int, int)
def _cellclicked(self, r, c):
it = self.item(r, c)
it.setTextAlignment(QtCore.Qt.AlignCenter)
n_it = self.item(r,1)
n_it.setToolTip('Test')
@QtCore.pyqtSlot()
def _addrow(self):
rowcount = self.rowCount()
self.insertRow(rowcount)
combox_add = QtWidgets.QComboBox(self)
combox_add.addItems(["I","II"])
self.setCellWidget(rowcount, 4, combox_add)
@QtCore.pyqtSlot()
def _removerow(self):
if self.rowCount() > 0:
self.removeRow(self.rowCount()-1)
@QtCore.pyqtSlot()
def _copyrow(self):
r = self.currentRow()
if 0 <= r < self.rowCount():
cells = {"items": , "widgets": }
for i in range(self.columnCount()):
it = self.item(r, i)
if it:
cells["items"].append((i, it.clone()))
w = self.cellWidget(r, i)
if w:
cells["widgets"].append((i, copy_widget(w)))
self.copy(cells, r+1)
def copy(self, cells, r):
self.insertRow(r)
for i, it in cells["items"]:
self.setItem(r, i, it)
for i, w in cells["widgets"]:
self.setCellWidget(r, i, w)
def mouseMoveEvent(self, event):
it = self.item(self.rowCount(),1)
it.QToolTip.showText('Insert')
self.onHovered()
def onHovered(self):
print("Works")
class ThirdTabLoads(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ThirdTabLoads, self).__init__(parent)
table = LoadTable()
add_button = QtWidgets.QPushButton("Add")
add_button.clicked.connect(table._addrow)
delete_button = QtWidgets.QPushButton("Delete")
delete_button.clicked.connect(table._removerow)
copy_button = QtWidgets.QPushButton("Copy")
copy_button.clicked.connect(table._copyrow)
button_layout = QtWidgets.QVBoxLayout()
button_layout.addWidget(add_button, alignment=QtCore.Qt.AlignBottom)
button_layout.addWidget(delete_button, alignment=QtCore.Qt.AlignTop)
button_layout.addWidget(copy_button, alignment=QtCore.Qt.AlignTop)
tablehbox = QtWidgets.QHBoxLayout()
tablehbox.setContentsMargins(10, 10, 10, 10)
tablehbox.addWidget(table)
grid = QtWidgets.QGridLayout(self)
grid.addLayout(button_layout, 0, 1)
grid.addLayout(tablehbox, 0, 0)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = ThirdTabLoads()
w.show()
sys.exit(app.exec_())
python pyqt pyqt5 qtablewidget qmouseevent
python pyqt pyqt5 qtablewidget qmouseevent
asked Jan 1 at 23:25
Pavel.DPavel.D
871111
871111
And is the mouse passed through cell A or cell C that must happen?
– eyllanesc
Jan 1 at 23:32
If mouse passed through cell A and the rest expect from cell B must do nothing. that is why I only markit = self.item(self.rowCount(),1)
.
– Pavel.D
Jan 1 at 23:37
Only in cell B should the tooltip be displayed?
– eyllanesc
Jan 1 at 23:38
Yes exactly, andQtooltips show text
is recommended to be used according pyqt5 documentation, because it is faster to react and display
– Pavel.D
Jan 1 at 23:39
add a comment |
And is the mouse passed through cell A or cell C that must happen?
– eyllanesc
Jan 1 at 23:32
If mouse passed through cell A and the rest expect from cell B must do nothing. that is why I only markit = self.item(self.rowCount(),1)
.
– Pavel.D
Jan 1 at 23:37
Only in cell B should the tooltip be displayed?
– eyllanesc
Jan 1 at 23:38
Yes exactly, andQtooltips show text
is recommended to be used according pyqt5 documentation, because it is faster to react and display
– Pavel.D
Jan 1 at 23:39
And is the mouse passed through cell A or cell C that must happen?
– eyllanesc
Jan 1 at 23:32
And is the mouse passed through cell A or cell C that must happen?
– eyllanesc
Jan 1 at 23:32
If mouse passed through cell A and the rest expect from cell B must do nothing. that is why I only mark
it = self.item(self.rowCount(),1)
.– Pavel.D
Jan 1 at 23:37
If mouse passed through cell A and the rest expect from cell B must do nothing. that is why I only mark
it = self.item(self.rowCount(),1)
.– Pavel.D
Jan 1 at 23:37
Only in cell B should the tooltip be displayed?
– eyllanesc
Jan 1 at 23:38
Only in cell B should the tooltip be displayed?
– eyllanesc
Jan 1 at 23:38
Yes exactly, and
Qtooltips show text
is recommended to be used according pyqt5 documentation, because it is faster to react and display– Pavel.D
Jan 1 at 23:39
Yes exactly, and
Qtooltips show text
is recommended to be used according pyqt5 documentation, because it is faster to react and display– Pavel.D
Jan 1 at 23:39
add a comment |
1 Answer
1
active
oldest
votes
The itemEntered signal must be used, but to do this, the mouseTracking must be enabled in addition to the item. When a row is added it does not imply that the items for each box exist so I have modified it to create it.
class LoadTable(QtWidgets.QTableWidget):
def __init__(self, parent=None):
super(LoadTable, self).__init__(0, 5, parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
headertitle = ("A","B","C","D","E")
self.setHorizontalHeaderLabels(headertitle)
self.verticalHeader().hide()
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setColumnWidth(0, 130)
self.setMouseTracking(True)
self.itemEntered.connect(self.on_itemEntered)
self._addrow()
def on_itemEntered(self, it):
QtWidgets.QToolTip.hideText()
if it.column() == 1:
r = self.visualItemRect(it)
p = self.viewport().mapToGlobal(QtCore.QPoint(r.center().x(), r.top()))
QtWidgets.QToolTip.showText(p, "Insert")
@QtCore.pyqtSlot()
def _addrow(self):
rowcount = self.rowCount()
self.insertRow(rowcount)
combox_add = QtWidgets.QComboBox(self)
combox_add.addItems(["I","II"])
self.setCellWidget(rowcount, 4, combox_add)
for c in range(self.columnCount()):
self.setItem(rowcount, c, QtWidgets.QTableWidgetItem())
# ...
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999740%2fmousemove-event-on-cell-in-qtablewidget-to-display-or-print-a-message%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The itemEntered signal must be used, but to do this, the mouseTracking must be enabled in addition to the item. When a row is added it does not imply that the items for each box exist so I have modified it to create it.
class LoadTable(QtWidgets.QTableWidget):
def __init__(self, parent=None):
super(LoadTable, self).__init__(0, 5, parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
headertitle = ("A","B","C","D","E")
self.setHorizontalHeaderLabels(headertitle)
self.verticalHeader().hide()
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setColumnWidth(0, 130)
self.setMouseTracking(True)
self.itemEntered.connect(self.on_itemEntered)
self._addrow()
def on_itemEntered(self, it):
QtWidgets.QToolTip.hideText()
if it.column() == 1:
r = self.visualItemRect(it)
p = self.viewport().mapToGlobal(QtCore.QPoint(r.center().x(), r.top()))
QtWidgets.QToolTip.showText(p, "Insert")
@QtCore.pyqtSlot()
def _addrow(self):
rowcount = self.rowCount()
self.insertRow(rowcount)
combox_add = QtWidgets.QComboBox(self)
combox_add.addItems(["I","II"])
self.setCellWidget(rowcount, 4, combox_add)
for c in range(self.columnCount()):
self.setItem(rowcount, c, QtWidgets.QTableWidgetItem())
# ...
add a comment |
The itemEntered signal must be used, but to do this, the mouseTracking must be enabled in addition to the item. When a row is added it does not imply that the items for each box exist so I have modified it to create it.
class LoadTable(QtWidgets.QTableWidget):
def __init__(self, parent=None):
super(LoadTable, self).__init__(0, 5, parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
headertitle = ("A","B","C","D","E")
self.setHorizontalHeaderLabels(headertitle)
self.verticalHeader().hide()
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setColumnWidth(0, 130)
self.setMouseTracking(True)
self.itemEntered.connect(self.on_itemEntered)
self._addrow()
def on_itemEntered(self, it):
QtWidgets.QToolTip.hideText()
if it.column() == 1:
r = self.visualItemRect(it)
p = self.viewport().mapToGlobal(QtCore.QPoint(r.center().x(), r.top()))
QtWidgets.QToolTip.showText(p, "Insert")
@QtCore.pyqtSlot()
def _addrow(self):
rowcount = self.rowCount()
self.insertRow(rowcount)
combox_add = QtWidgets.QComboBox(self)
combox_add.addItems(["I","II"])
self.setCellWidget(rowcount, 4, combox_add)
for c in range(self.columnCount()):
self.setItem(rowcount, c, QtWidgets.QTableWidgetItem())
# ...
add a comment |
The itemEntered signal must be used, but to do this, the mouseTracking must be enabled in addition to the item. When a row is added it does not imply that the items for each box exist so I have modified it to create it.
class LoadTable(QtWidgets.QTableWidget):
def __init__(self, parent=None):
super(LoadTable, self).__init__(0, 5, parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
headertitle = ("A","B","C","D","E")
self.setHorizontalHeaderLabels(headertitle)
self.verticalHeader().hide()
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setColumnWidth(0, 130)
self.setMouseTracking(True)
self.itemEntered.connect(self.on_itemEntered)
self._addrow()
def on_itemEntered(self, it):
QtWidgets.QToolTip.hideText()
if it.column() == 1:
r = self.visualItemRect(it)
p = self.viewport().mapToGlobal(QtCore.QPoint(r.center().x(), r.top()))
QtWidgets.QToolTip.showText(p, "Insert")
@QtCore.pyqtSlot()
def _addrow(self):
rowcount = self.rowCount()
self.insertRow(rowcount)
combox_add = QtWidgets.QComboBox(self)
combox_add.addItems(["I","II"])
self.setCellWidget(rowcount, 4, combox_add)
for c in range(self.columnCount()):
self.setItem(rowcount, c, QtWidgets.QTableWidgetItem())
# ...
The itemEntered signal must be used, but to do this, the mouseTracking must be enabled in addition to the item. When a row is added it does not imply that the items for each box exist so I have modified it to create it.
class LoadTable(QtWidgets.QTableWidget):
def __init__(self, parent=None):
super(LoadTable, self).__init__(0, 5, parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
headertitle = ("A","B","C","D","E")
self.setHorizontalHeaderLabels(headertitle)
self.verticalHeader().hide()
self.horizontalHeader().setHighlightSections(False)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setColumnWidth(0, 130)
self.setMouseTracking(True)
self.itemEntered.connect(self.on_itemEntered)
self._addrow()
def on_itemEntered(self, it):
QtWidgets.QToolTip.hideText()
if it.column() == 1:
r = self.visualItemRect(it)
p = self.viewport().mapToGlobal(QtCore.QPoint(r.center().x(), r.top()))
QtWidgets.QToolTip.showText(p, "Insert")
@QtCore.pyqtSlot()
def _addrow(self):
rowcount = self.rowCount()
self.insertRow(rowcount)
combox_add = QtWidgets.QComboBox(self)
combox_add.addItems(["I","II"])
self.setCellWidget(rowcount, 4, combox_add)
for c in range(self.columnCount()):
self.setItem(rowcount, c, QtWidgets.QTableWidgetItem())
# ...
answered Jan 2 at 0:05
eyllanesceyllanesc
80.5k103258
80.5k103258
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999740%2fmousemove-event-on-cell-in-qtablewidget-to-display-or-print-a-message%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
And is the mouse passed through cell A or cell C that must happen?
– eyllanesc
Jan 1 at 23:32
If mouse passed through cell A and the rest expect from cell B must do nothing. that is why I only mark
it = self.item(self.rowCount(),1)
.– Pavel.D
Jan 1 at 23:37
Only in cell B should the tooltip be displayed?
– eyllanesc
Jan 1 at 23:38
Yes exactly, and
Qtooltips show text
is recommended to be used according pyqt5 documentation, because it is faster to react and display– Pavel.D
Jan 1 at 23:39