Ido
Ido

Reputation: 55

Pyside PlainText placeholder text not showing

ive created a gui for my project and i need to use a placeholder text for a QPlainText.(im creating the gui using qt designer and generate the code threw pyside) its a bit tedious but thats how i roll right now. i have a problem though with the placeholder - it wont show on the plain text unless im writing and then deleting . and i need it to be shown at all the times unless the placeholder gets clicked or tabbed to. i found some solutions on the net but none seems to work for me.most of the answers i saw use the focus function but i didnt quite get the idea and they didnt work on my whole gui even when i tried setting the focus to clear or other object. moreover i need to solve this issue from the qt designer itself or threw the main class im using - cause im changing the gui and generating a new one every time so changes to the code in the gui wont apply further.

i added the code below as a minimal example for the plaintext im using in my project. and i know that if im generating the code threw pyqt5 after recent changes to the placeholder it will work as i want but unfortunately i need it in pyside which hasn't updated yet.

import sys

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
    MainWindow.resize(480, 640)
    self.centralwidget = QWidget(MainWindow)
    self.centralwidget.setObjectName(u"centralwidget")
    self.shifted_text_encode = QPlainTextEdit(self.centralwidget)
    self.shifted_text_encode.setObjectName(u"shifted_text_encode")
    self.shifted_text_encode.setGeometry(QRect(80, 170, 311, 131))
    self.shifted_text_encode.setFocusPolicy(Qt.StrongFocus)
    self.shifted_text_encode.setTabChangesFocus(True)
    MainWindow.setCentralWidget(self.centralwidget)
    self.statusbar = QStatusBar(MainWindow)
    self.statusbar.setObjectName(u"statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)

    QMetaObject.connectSlotsByName(MainWindow)
# setupUi

def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
    self.shifted_text_encode.setPlainText("")
    self.shifted_text_encode.setPlaceholderText(QCoreApplication.translate("MainWindow", u"Enter text", None))
# retranslateUi

Upvotes: 0

Views: 334

Answers (1)

eyllanesc
eyllanesc

Reputation: 243983

It seems that it is a Qt bug since I also reproduce it in Qt5 and Qt6.

Explanation:

In the private Qt API there is a function that re-evaluates if the placeHolderText is visible or not, and it is invoked when the text changes but it is not re-evaluated when the placeHolder is set causing that unexpected behavior.

Workaround

A possible solution is to remove self.shifted_text_encode.setPlainText("") or use self.shifted_text_encode.clear() after self.shifted_text_encode.setPlaceholderText(...).


I just reported the bug QTBUG-96212

Upvotes: 2

Related Questions