Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7411231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:21:21+00:00 2026-05-29T06:21:21+00:00

First I’ll show the code. class XLineEdit(QtGui.QLineEdit): ”’QLineEdit with clear button, which appears when

  • 0

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. 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. It seems that vertical size changes on changing the size of QLineEdit’s font, but horizontal size is not. How can I fix this? Is there any other way to create QLineEdit with clear button? I’ve tried to create QPushButton with custom QIcon, but icon doesn’t change it’s size at all (neither vertical, nor horizontal).

  2. How can I create a new signal when user clicks on QLabel? It seems that there is no analog for QPushButton’s ‘clicked’.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:21:21+00:00Added an answer on May 29, 2026 at 6:21 am

    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…

    You can create a clickable QLabel by overloading the MousePressEvent and emitting your own custom signal.

    from PyQt4.QtCore import pyqtSignal
    from PyQt4.QtGui import 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. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, I have a dbcontext factory which is defined public class DatabaseFactory : Disposable,
First is the code #include <stdio.h> typedef wchar_t* BSTR; wchar_t hello[] = LHello; class
First off, I understand the reasons why an interface or abstract class (in the
First I will show you example tables that my issue pertains to, then I
First time poster, be gentle ;-) I'm writing an audio app (in C++) which
First the code (sorry if its not 100%) I am no expert and then
First time user of stackoverflow, but I have followed its development over on Coding
first question asked, so I'll get straight to it. I've got some C code
First, let's get the security considerations out of the way. I'm using simple authentication
First off, I am using Windows XP. I have multiple hard drives and it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.