Results 1 to 5 of 5

Thread: How to add LineEdit dynamically

  1. #1
    Join Date
    Feb 2018
    Location
    Denmark
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default How to add LineEdit dynamically

    Hej.

    I am trying to add LineEdit dynamically and it works partly.
    Fields are added and looks okay but when I close the program then I get the message "Python.exe" has stopped working.

    Bellow I have added the two codes which I am trying to get working.

    It generates a 5 x 5 matrix of LineEdit's and this fails on close.
    If I only add 1x1 then I don't get any error on closing.

    I have tried various approaches most try and error but I think that here is something fundamentally I don't understand.


    The UI
    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. # Form implementation generated from reading ui file 'User_terminal.ui'
    4. #
    5. # Created by: PyQt4 UI code generator 4.11.4
    6. #
    7. # WARNING! All changes made in this file will be lost!
    8.  
    9. from PyQt4 import QtCore, QtGui
    10.  
    11. try:
    12. _fromUtf8 = QtCore.QString.fromUtf8
    13. except AttributeError:
    14. def _fromUtf8(s):
    15. return s
    16.  
    17. try:
    18. _encoding = QtGui.QApplication.UnicodeUTF8
    19. def _translate(context, text, disambig):
    20. return QtGui.QApplication.translate(context, text, disambig, _encoding)
    21. except AttributeError:
    22. def _translate(context, text, disambig):
    23. return QtGui.QApplication.translate(context, text, disambig)
    24.  
    25. class Ui_MainWindow(object):
    26. def setupUi(self, MainWindow):
    27. MainWindow.setObjectName(_fromUtf8("MainWindow"))
    28. MainWindow.resize(800, 600)
    29. self.centralwidget = QtGui.QWidget(MainWindow)
    30. self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    31. MainWindow.setCentralWidget(self.centralwidget)
    32. self.menubar = QtGui.QMenuBar(MainWindow)
    33. self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
    34. self.menubar.setObjectName(_fromUtf8("menubar"))
    35. MainWindow.setMenuBar(self.menubar)
    36. self.statusbar = QtGui.QStatusBar(MainWindow)
    37. self.statusbar.setObjectName(_fromUtf8("statusbar"))
    38. MainWindow.setStatusBar(self.statusbar)
    39.  
    40. self.retranslateUi(MainWindow)
    41. QtCore.QMetaObject.connectSlotsByName(MainWindow)
    42.  
    43. def retranslateUi(self, MainWindow):
    44. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
    45.  
    46.  
    47. if __name__ == "__main__":
    48. import sys
    49. app = QtGui.QApplication(sys.argv)
    50. MainWindow = QtGui.QMainWindow()
    51. ui = Ui_MainWindow()
    52. ui.setupUi(MainWindow)
    53. MainWindow.show()
    54. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    The main code.
    Qt Code:
    1. import sys
    2. from User_terminal import Ui_MainWindow
    3. from PyQt4 import QtCore, QtGui
    4.  
    5.  
    6. try:
    7. _fromUtf8 = QtCore.QString.fromUtf8
    8. except AttributeError:
    9. def _fromUtf8(s):
    10. return s
    11.  
    12.  
    13. class Class_user_terminal(QtGui.QMainWindow, Ui_MainWindow):
    14.  
    15. def __init__(self, parent=None):
    16. super(Class_user_terminal, self).__init__(parent)
    17. self.setupUi(self)
    18. self.My_list = []
    19.  
    20. X_rast = 60
    21. Y_rast = 30
    22. Y_n = 0
    23. i = 0
    24. while Y_n < 5:
    25. X_n = 0
    26. while X_n < 5:
    27. self.My_list.append(QtGui.QLineEdit(self.centralwidget))
    28. self.My_list[-1].setGeometry(QtCore.QRect((60 + X_n*X_rast), (60 + Y_n*Y_rast), 40, 20))
    29. self.My_list[-1].setText("But {}".format(i))
    30. self.My_list[-1].setObjectName(_fromUtf8("lineEdit_myline{}".format(i)))
    31. X_n = X_n + 1
    32. i = i + 1
    33. Y_n = Y_n + 1
    34.  
    35.  
    36.  
    37. if __name__ == '__main__':
    38. app = QtGui.QApplication(sys.argv)
    39. frame = Class_user_terminal()
    40. frame.show()
    41. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 


    /Jan

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to add LineEdit dynamically

    Why are you laying out the geometry of your central widget manually, using setGeometry calls? Insert a QGridLayout as the layout for the central widget, then insert the line edits into the grid. The grid layout will take care of everything required to arrange them properly and to move them when the main window is resized. You can insert horizontal and vertical spacers into the grid to limit the "spread" of the widgets as the window is resized.

    My guess as to the reason for the crash is that you are creating your line edits as children of the central widget (which makes the central widget their owner and in control of their lifetimes) and also adding them to My_list. When the central widget goes out of scope, it will automatically delete these child widgets. It could also be that My_list is trying to delete them as well. Whichever one gets there second is attempting to delete an object that has already been deleted, and that will cause a crash.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Feb 2018
    Location
    Denmark
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to add LineEdit dynamically

    Hej d_stranz

    Thanks I makes sense that both central widget and My_list are trying to delete them and this gives the exit error.

    Regarding the layout then I normally have a fixed layout created in Qt designer and then I just need to connect functions.

    In this project I have a variable number of items and I searched the net for suggestions of how to do this.
    Ended up with this suggestion which seem to work at the first view.
    However when I detected that the code failed on close event I realized that the example I used may not be correct.

    I have no clue at the moment of how to use a QGridLayout but will try to search for it.

    The reason for the My_list is that I want to update LineEdit one by one (make color shift) when they are updated in a database.

    If you have any ideas, suggestions or example code then it will be appreciated as I normally don't modify what comes out from QT designer in the UI file, meaning I don't have a clue of what I am doing at the moment and that it is more a try and error.

    /Jan

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to add LineEdit dynamically

    You can start learning about QGridLayout here. I don't think there are any differences of importance between Qt4 and Qt5 for the grid layout. The translation from C++ to Python is straightforward.

    The reason for the My_list is that I want to update LineEdit one by one (make color shift) when they are updated in a database.
    The grid layout indexes its items by [row, column]. So instead of storing the line edit itself in your list (which causes the double-delete problem), store the [row, column] pair. You can retrieve the line edit using the QGridLayout::itemAtPosition() method.

    I normally don't modify what comes out from QT designer in the UI file
    It is possible to make changes to the UI files from a normal text editor, but it is very, very easy to make a mistake that causes the file to break. In a case like yours, where the widget is very simple (a QWidget with a single layout containing child widgets) it is usually easiest to skip Qt Designer and just build the UI in code. If the UI is complex (a dialog with multiple levels of layouts and child widgets, for example) it is better to use the Designer. For things in between, you can choose to create the basic widget using Designer, then make modifications to the content or appearance in code. Qt is very flexible in that regard.

    I tend to use Qt Designer mostly for widgets that will be used in dialogs; for main window and other high-level widgets, I tend to create them in code.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Feb 2018
    Location
    Denmark
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to add LineEdit dynamically

    Hej again d_stranz.

    And again Thanks!.

    I have had a quick look at the grid layout and it looks to be the way.


    And then a small comment.
    The code I added here is just a small part cut down to the part which is troubling me.
    The UI's is much more complex.

    "I normally don't modify what comes out from QT designer in the UI file "
    I always uses the Qt designer as it takes a few seconds when I want to make changes.
    But what I meant was that I normally don't add new widgets and so on in main code. I just import the UI and leave it. Eventually adding new items to a combobox but that is all.

    So what is complete new to me is that I create an UI uses it in my main code as usual but now adding input dialogs in the main code.

    /Jan

Similar Threads

  1. Replies: 5
    Last Post: 22nd December 2016, 22:04
  2. lineedit
    By madhukumar in forum Qt Programming
    Replies: 1
    Last Post: 17th February 2014, 19:17
  3. LineEdit's
    By Johnnyj2j in forum Newbie
    Replies: 6
    Last Post: 9th July 2012, 12:34
  4. Replies: 12
    Last Post: 24th October 2011, 07:56
  5. LineEdit.
    By Rewo in forum Newbie
    Replies: 17
    Last Post: 2nd July 2010, 09:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.