Reputation: 78
I am using QtDesigner (4.8.7) to build a QDialog which will serve as the main interaction point for a QGIS plugin I am creating. Most of the user input is entered through various QLineEdit fields, some of which have a placeholderText
set.
Unfortunately, every time the QDialog is opened, a QLineEdit field is immediately selected (i.e. it receives focus), which causes the placeholderText to disappear in order to allow the user to enter text. Therefore, I'm wondering if it's possible to create a QDialog which does not automatically focus on any field. This would allow the end user of my plugin to inspect the place holder texts before entering any value themselves.
I'm currently initializing the QDialog as follows:
import PyQt4.QtGui as QTG
import PyQt4.QtCore as QTC
from dialog_ui import Ui_dialog
class UI (object):
def __init__(self, iface):
# iface is just a way to interact with QGIS
self.iface = iface
self.container = QTG.QDialog()
self.ui = Ui_dialog()
self.setup()
def setup(self):
self.ui.setupUi(self.container)
# Custom ui setup code follows after this...
# Called by an external function
def show(self):
self.container.exec_()
Upvotes: 1
Views: 1934
Reputation: 78
I eventually went for a combination of ekhumoro's answer and the solution that I found here. The gist of the solution is to use clearFocus()
on the QLineEdit to force it to lose focus.
I decided to make the QLineEdit lose focus when the user clicks on another location of the overarching QDialog. The code ends up looking like this:
import PyQt4.QtGui as QTG
import PyQt4.QtCore as QTC
from dialog_ui import Ui_dialog
class CustomDialog (QTG.QDialog):
def mousePressEvent(self, event):
focusWidget = QTG.QApplication.focusWidget()
if focusWidget:
focusWidget.clearFocus()
class UI (object):
def __init__(self, iface):
# iface is just a way to interact with QGIS
self.iface = iface
self.container = CustomDialog()
self.ui = Ui_dialog()
# Rest of the code remains unchanged
Upvotes: 1
Reputation: 120568
Use the Tab Order Editing Mode in Qt Designer.
Start the tab-ordering on some widget that doesn't have placeholder text. That widget will get the initial focus.
Alternatively, just call setFocus()
on an appropriate widget before showing the dialog.
Upvotes: 4