我正在尝试制作一个按钮(或任何其他 Qwidget),这会在悬停时更改用户光标。
因此,例如,当我悬停 QPushButton 时,它会将光标从箭头更改为指向手。
我正在使用 Qt 样式表,所以我不完全确定,但是有没有办法在那里做类似的事情?,应该是这样的:
btn.setStyleSheet("#btn {background-image: url(':/images/Button1.png'); border: none; }"
"#btn:hover { change-cursor: cursor('PointingHand'); }
Run Code Online (Sandbox Code Playgroud)
注意:例如上面的代码,第二行根本没有任何功能。
但是,如果没有,是否还有其他方法可以实现这一目标?
小智 7
对于任何想在 PyQt5 中实现这一目标的人来说,这就是我设法做到的。假设您有一个按钮,并且希望将鼠标悬停在按钮上时将光标更改为“PointingHandCursor”。你可以用your_button.setCursor(QCursor(QtCore.Qt.PointingHandCursor)). 例如:
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QLabel, QProgressBar,
QLineEdit, QFileDialog
from PyQt5 import QtGui, QtCore
from PyQt5.QtGui import QCursor
class Window(QWidget):
def __init__(self):
super().__init__()
self.title = "your_title"
self.screen_dim = (1600, 900)
self.width = 650
self.height = 400
self.left = int(self.screen_dim[0]/2 - self.width/2)
self.top = int(self.screen_dim[1]/2 - self.height/2)
self.init_window()
def init_window(self):
self.setWindowIcon(QtGui.QIcon('path_to_icon.png'))
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.setStyleSheet('background-color: rgb(52, 50, 51);')
self.create_layout()
self.show()
def create_layout(self):
self.button = QPushButton('Click Me', self)
self.button.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
if __name__ == '__main__':
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
Run Code Online (Sandbox Code Playgroud)
使用QWidget.setCursor (self, QCursor)
http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#setCursor
http://doc.qt.io/qt-4.8/qwidget.html#cursor-prop
| 归档时间: |
|
| 查看次数: |
10797 次 |
| 最近记录: |