简体   繁体   English

PyQt4:QLabel与清除按钮

[英]PyQt4: QLabel with clear button

First I'll show the code. 首先,我将显示代码。

class XLineEdit(QtGui.QLineEdit):
  '''QLineEdit with clear button, which appears when user enters text.'''
  def __init__(self, pixmap, parent=None):
    QtGui.QLineEdit.__init__(self, parent)
    self.layout = QtGui.QHBoxLayout(self)
    self.image = QtGui.QLabel(self)
    self.image.setCursor(QtCore.Qt.ArrowCursor)
    self.image.setFocusPolicy(QtCore.Qt.NoFocus)
    self.image.setStyleSheet("border: none;")
    self.image.setPixmap(pixmap)
    self.image.setSizePolicy(
      QtGui.QSizePolicy.Expanding,
      QtGui.QSizePolicy.Expanding)
    self.image.adjustSize()
    self.image.setScaledContents(True)
    self.layout.addWidget(
      self.image, alignment=QtCore.Qt.AlignRight)
    self.textChanged.connect(self.changed)
    self.image.hide()

  def changed(self, text):
    if len(text) > 0:
      self.image.show()
    else: # if entry is empty
      self.image.hide()

That creates QLineEdit object with custom button from QLabel at the right side of QLineEdit. 这将使用QLineEdit右侧的QLabel中的自定义按钮创建QLineEdit对象。 I have only two problems: 我只有两个问题:

  1. If I change the font of XLineEdit ("XLineEdit object".setFont(QFont)), image button will look good by vertical, but will look ugly by horizontal. 如果更改XLineEdit的字体(“ XLineEdit对象” .setFont(QFont)),则图像按钮在垂直方向上看起来不错,但在水平方向上则很难看。 It seems that vertical size changes on changing the size of QLineEdit's font, but horizontal size is not. 似乎在改变QLineEdit字体大小时,垂直大小会改变,但水平大小则不会。 How can I fix this? 我怎样才能解决这个问题? Is there any other way to create QLineEdit with clear button? 还有其他方法可以使用清除按钮创建QLineEdit吗? I've tried to create QPushButton with custom QIcon, but icon doesn't change it's size at all (neither vertical, nor horizontal). 我试图用自定义QIcon创建QPushButton,但是图标根本不改变其大小(垂直或水平都没有)。

  2. How can I create a new signal when user clicks on QLabel? 用户单击QLabel时如何创建新信号? It seems that there is no analog for QPushButton's 'clicked'. 似乎没有模拟的QPushButton的“被点击”。

Thanks! 谢谢!

While @reclosedev already commented on your question with a link to a C++ example for the clear button aspect, I wanted to add information about your second question... 虽然@reclosedev已经通过清除按钮方面的C ++示例链接评论了您的问题,但我想添加有关第二个问题的信息...

You can create a clickable QLabel by overloading the MousePressEvent and emitting your own custom signal. 您可以通过重载MousePressEvent并发出自己的自定义信号来创建可点击的QLabel。

from PyQt4.QtCore import pyqtSignal from PyQt4.QtGui import QLabel, QStyle 从PyQt4.QtCore导入pyqtSignal从PyQt4.QtGui导入QLabel,QStyle

class ClickLabel(QLabel):

    clicked = pyqtSignal()

    def __init__(self, *args, **kwargs)
        super(ClickLabel, self).__init__(*args, **kwargs)

    def mousePressEvent(self, event):
        event.accept()
        self.clicked.emit()

A comment about the C++ link that was provided in the other comment. 其他注释中提供的有关C ++链接的注释。 Instead of using an HBoxLayout, they are just directly parenting the button to the QLabel widget, and using the resizeEvent to always move it to the right side of the QLabel. 他们不使用HBoxLayout,而是直接将按钮作为QLabel小部件的父项,并使用resizeEvent始终将其移至QLabel的右侧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM