Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QLineEdit is not updating with setText

Tags:

python

qt

pyqt

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"

like image 637
Safa Alfulaij Avatar asked Jan 25 '14 06:01

Safa Alfulaij


2 Answers

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.

like image 113
ekhumoro Avatar answered Sep 24 '22 02:09

ekhumoro


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.

like image 44
Hyperboreus Avatar answered Sep 26 '22 02:09

Hyperboreus