QLineEdit is not updating with setText
Asked Answered
P

2

10

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"

Portray answered 25/1, 2014 at 6:39 Comment(9)
What does "doesn't work" mean? Could you show us some code?Bari
I mean that I can't see the new string "safa" in the QLineEdit in the GUI, but if I print self.browse_file.text() I can see the "safa" in the terminal.Portray
Some more code might be helpful hereNakashima
A full example: mediafire.com/download/uf8q89p6w43egwn/example.tar.xzPortray
Most of peopple (including me) will not download an archive to track down your problem in it. You may consider to extract a complete minimal example of your problem (from your question I would say circa 20lines...)Bedclothes
Are you sure that self.lineEdit is the same widget that you're looking at? Maybe the right widget has different name.Cony
It is the same widget, It will not cause a problem here (in the code after update), you can see the problem in the example file... there are about 5 files, run program.py and you will not see that "safa" in the QLineEdit when you press the button. I pasted the files again in paste bin so you can check them. pastebin.com/es2f5EyTPortray
Create a two file example: one file generated by pyuic which you do not need to post, just post the minimal file that reproduces the problem.Adda
I don't see any problem with two files, the problem occurs with something bigger, like 5 files. There is no problem with two files, but of course my program will not contain two files only...Portray
S
7

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.

Sotos answered 25/1, 2014 at 19:1 Comment(1)
Thanks! That solve my one-month problem! And sorry for my misunderstanding :\Portray
B
4

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.

Bari answered 25/1, 2014 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.