Here is a very simple implementation of placing buttons in QLineEdit as custom types written in Python:
from PySide.QtCore import * from PySide.QtGui import * class Entry(QLineEdit): def __init__(self): QLineEdit.__init__(self) self.buttons = [] self.backupText = '' self.textEdited.connect(self.on_change) self.layout = QHBoxLayout() self.setLayout(self.layout) self.layout.addStretch() marginz = QLabel(' ') marginz.show() margin = marginz.width() marginz.hide() self.layout.setContentsMargins(margin, margin, margin, margin) def on_change(self): if self.text()[-1] == ' ' and not self.text().endswith(' '): if len(self.text()) > len(self.backupText): self.setText(self.text() + ' ') self.buttons.append(QPushButton(self.text().split()[-1])) self.layout.insertWidget(self.layout.count()-1, self.buttons[-1]) else: self.setText(self.text()[0:-1]) self.buttons[-1].hide() del self.buttons[-1] self.backupText = self.text() app = QApplication([]) window = QMainWindow() window.setStyleSheet( 'QPushButton {border: 1px solid gray; background: lightgray; color: black;}') entry = Entry() window.setCentralWidget(entry) window.show() app.exec_()
It creates a QHBoxLayout and adds a button to it for each word you enter and discards the button when you get rid of that word.
If you want to place a close button inside each of the sub-widgets, you can also create your own widget.
EDIT
As noted in j_kubik's comment, systems with extended fields can cause button tags to overlap the text that the user is currently typing. I changed the code to force the fields of the inserted buttons (with style sheets), added extra space for each space that the user types, and set the QHBoxLayout contentsMargins to the same width as the space (" "). Now the buttons will not overlap the entered text.
source share