I have a program with two windows, main and the settings.
When I run setText on a QLineEdit in the settings.py file, the new string is not in the GUI, and I can see the string before the setText code.
When I put the same code in the settingsUI file generated from Qt Designer, It works. But in the settings.py doesn't.
The settings file is the file that contains the SettingsWindow class and I can put the real python code in it.
The settingsUI file is the file that contains the GUI, I generated it with pyuic4 (or pyuic5).
This code works in settingsUI file:self.browse_file.setText("safa")
But dosen't work in settings file.
--UPDATE--
import sys
from PyQt4 import QtCore, QtGui
from settingsui import Ui_Dialog
class SettingsWindow(QtGui.QDialog, Ui_Dialog):
def __init__(self):
QtGui.QDialog.__init__(self)
Ui_Dialog.__init__(self)
self.setupUi(self)
self.lineEdit.setText("safa")
print self.lineEdit.text()
After: self.lineEdit.setText("safa")
, I can't see any text in the QLineEdit.print self.lineEdit.text()
outputs the text "safa"
The problem is in your mainwind.py
file.
You try to use the following method for opening the dialog:
def buttonclicked(self):
Dialog = QtGui.QDialog()
u = settings.SettingsWindow()
u.setupUi(Dialog)
Dialog.exec_()
The reason why the text doesn't show, is because you are creating two dialogs. The second one (named u
) has setText()
called on it, but then gets thrown away without being shown.
Your method should instead look like this:
def buttonclicked(self):
dialog = settings.SettingsWindow()
dialog.exec_()
All the setup code for the SettingsWindow
dialog is already inside its __init__
method, so all you need to do is create an instance of it.
PS:
In MainWindow.__init__
you have Ui_MainWindow.__init__(self)
, and in SettingsWindow.__init__
you have Ui_Dialog.__init__(self)
. These lines don't do anything useful, because the Ui_*
classes are just simple subclasses of object
. So those two lines could be removed.
Shouldn't you initialize your UI along these lines:
class SettingsWindow(QtGui.QDialog):
def __init__(self, parent = None):
QtGui.QDialog.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.lineEdit.setText("safa")
print self.ui.lineEdit.text()
This is how I do it all the time and works like a charm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With