1

Here I have defined a simple abstract list model

from PySide6 import QtCore
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtCore import QAbstractListModel, Qt

loader = QUiLoader()

class TodoModel(QAbstractListModel):
def __init__(self, todos=None):
    super().__init__()
    self.todos = todos or []

def data(self, index, role):
    if role == Qt.DisplayRole:
        status, text = self.todos[index.row()]
        return text

def rowCount(self, index):
    return len(self.todos)

This is where I load the application.

class MainWindow(QtCore.QObject):
    def __init__(self):
        super().__init__()
        self.wid = loader.load("todo.ui", None)
        self.model = TodoModel(todos=[(False,"hello world")])
        self.wid.linedit.setModel()
        self.wid.show()


app = QApplication([])
ui = MainWindow()
app.exec()

I am getting the error the attribute is not present. I understand; I am inheriting a QObject that does not have that attribute; I Tried inheriting the QMainWIndow and the TodoModel.

All of them are giving me some or the other kind of error.

Ideally, I want to display in the todo widget in the lineEdit (QLineEdit) the value "hello world".

What needs to change. Also, I cannot use the pyside6-uic.UI file has to be loaded using the QuiLoader

Adding Traceback

qt.pysideplugin: Environment variable PYSIDE_DESIGNER_PLUGINS is not set, bailing out.
qt.pysideplugin: No instance of QPyDesignerCustomWidgetCollection was found.
Traceback (most recent call last):
  File "/home/danish/playground/QT/Second_TUT/ui_test/main.py", line 35, in <module>
    ui = MainWindow()
  File "/home/danish/playground/QT/Second_TUT/ui_test/main.py", line 30, in __init__
    self.wid.lineEdit.setModel()
AttributeError: 'PySide6.QtWidgets.QLineEdit' object has no attribute 'setModel'

Process finished with exit code 1

Also thelineEdit exists. enter image description here

How to add this list model to the QLineEdit to display inside the QLineEdit "hello world" ?

Danish Xavier
  • 1,225
  • 1
  • 10
  • 21
  • That error has nothing to do with the fact that you're inheriting from QObject, which, by the way, makes no sense, unless you *actually* use QObject features (and, even assuming that, your `MainWindow` doesn't represent a window, but an instance that has such an attribute. That error may be caused by another reason, such as the UI not having a widget named `linedit` or that such widget is a QLineEdit, which has no `setModel()` function. Then, `setModel()` without any argument is completely pointless (and will obviously throw another exception, since the argument is mandatory). – musicamante Feb 15 '23 at 04:04
  • Unfortunately, since you **did not** provide the full error, it's completely impossible to us to tell you where the problem actually is. So: 1. please [edit] your question and provide the full traceback of that error; 2. read the documentation of the classes and functions you're using. Also, don't inherit from QObject for the wrong reasons. Unlike PyQt, PySide is not capable of "setting up" an UI file on an existing widget, so if you want to use proper subclassing, consider following [this answer](https://stackoverflow.com/a/27610822). – musicamante Feb 15 '23 at 04:08
  • I have edited the question. I am new to Pyside. How to add the model and display in the QLineEdit the data of the TodoModel which here Is the "hello world" – Danish Xavier Feb 15 '23 at 04:21
  • As said above, you can't call `setModel()` on a QLineEdit, because it doesn't have such function (as you can see [in the documentation](//doc.qt.io/qt-6/qlineedit.html). You cannot add a model to a QLineEdit because that's not its purpose: it just shows text and allows to edit it. What do you need it for? Are you trying to create a *completion* system? Then use [`QLineEdit.setCompleter()`](//doc.qt.io/qt-6/qlineedit.html#setCompleter) along with a proper QCompleter object. You want to do something else? Then please try to explain it more carefully, as right now your question is very confusing. – musicamante Feb 15 '23 at 05:22

0 Answers0