-2

I have created a GUI that ask for files to index, search within it and spit out the results. I am stuck with the last bit that is to show the excerpt of the documents retrieved.

I need to populate the tableWidget with the results of a search engine embedded in the same class. I Thought that the variables were reachable from everywhere within the class is labeled with self, but I was wrong.

This is my latest code I have tried tried so far:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1126, 879)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(40, 30, 100, 30))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(180, 30, 120, 30))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(620, 30, 80, 30))
        self.pushButton_3.setObjectName("pushButton_3")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(380, 60, 191, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(40, 90, 50, 21))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(380, 30, 50, 35))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.label2 = QtWidgets.QLabel(self.centralwidget)
        self.label2.setGeometry(QtCore.QRect(40, 70, 150, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label2.setFont(font)
        self.label2.setObjectName("label")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(0, 120, 1121, 721))
        self.tableWidget.setObjectName("tableWidget")
        data = self.data()
        numrows = len(data)
        numcols = len(data[0])
        self.tableWidget.setColumnCount(numcols)
        self.tableWidget.setRowCount(numrows)
        for row in range(numrows):
            for column in range(numcols):
               self.tableWidget.setItem(row, column, QTableWidgetItem((data[row][column])))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1126, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

The function related to the results is as follow:

def search(self):
        os.chdir(self.folder_path)
        self.ix = open_dir("indexdir")
        MYDIR = ("Results")
        CHECK_FOLDER = os.path.isdir(MYDIR)
        if not CHECK_FOLDER:
            os.makedirs(MYDIR) 
        self.text = self.lineEdit.text()
        self.query_str = self.text
        self.query = qparser.QueryParser("textdata", schema = self.ix.schema)
        self.q = self.query.parse(self.query_str)
        self.topN = self.lineEdit_2.text()
        if self.lineEdit_2.text() == "":
            self.topN = 1000           
        else:
            self.topN = int(self.lineEdit_2.text())
        
        self.data=[]
        with self.ix.searcher() as searcher:
            self.results = searcher.search(self.q, terms=True, limit=self.topN)
            Upper = highlight.UppercaseFormatter()
            self.results.formatter = Upper
            my_cf = highlight.ContextFragmenter(maxchars=500, surround=300)
            self.results.fragmenter = my_cf
            for self.i in self.results:
                   self.data.append({"title": self.i['title'], "text": self.i.highlights('textdata')})
        return self.data

I got this error: AttributeError: 'Ui_MainWindow' object has no attribute 'folder_path'.

How I can access the self.data to populate the table?

  • 1
    Your code is incomplete (for future reference, you should always provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)), but from the look of it I'd stop you right here: you should **NEVER** edit the output of `pyuic` (nor try to mimic the classes it creates). Read more about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html) to understand how to use those files correctly. – musicamante Jun 21 '20 at 23:13
  • I did not posted all the code because it has a lot of code that is not related to this query. I have modified the pyuic content so far and it worked. I need just to know how to access the results search function from the setup ui or if I have to create another function and then access it from there. I have read the documentation and searched for solution before. But I can't see how to do it. – Vito Piepoli Jun 22 '20 at 10:00
  • As said, without a MRE it's hard to find the source of your problem, but it's possible that you're creating a `Ui_MainWindow` instance and using `setupUi` against a QMainWindow instance, which might be the source of the error. I know that editing the pyuic file "seems" to work, but it's *not* the suggested approach; one of the reasons is the fact that it can create confusion between the classes and what they actually do. I strongly suggest you to create a new script that uses the uic class as explained in the link above and implement your code from there. Do NOT edit the pyuic generated files. – musicamante Jun 22 '20 at 12:51

2 Answers2

0

It appears that you're trying to use the "folder_path" attribute without having created it first. Before you use the your search function, add this line to your class:

self.folder_path = add/your/path/

I'll also note that in the code you provided, it appears that the search function is defined outside of the class (this may just be the effect of auto-formatting on stack overflow). In order for to make your self.folder_path variable usable as-named, double check that you've properly indented it as to make it a class method.

cdiddy80
  • 31
  • 5
  • The issue here is that the function Setup UI points to the first row the function Search instead of to the returned value. The search function is within the same class. I think that issue could be also the way I return the value to the function. But I need to append the results to a dictionary before being able to return it as a workable dataset to display. – Vito Piepoli Jun 22 '20 at 10:04
0

I overcame the issue by cleaning up SetupUI and creating a function that runs once I click within the editline.

self.lineEdit.returnPressed.connect(self.datatable)
    
    def datatable(self): 
              
        data = self.data
        numrows = len(self.data)
        numcols = len(self.data[0])
        self.tableWidget.setColumnCount(numcols)
        self.tableWidget.setRowCount(numrows)
        self.tableWidget.setHorizontalHeaderLabels((list(self.data[0].keys())))
        self.tableWidget.resizeColumnsToContents()
        self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
        self.tableWidget.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
        for row in range(numrows):
            for column in range(numcols):
                item = (list(self.data[row].values())[column])
                self.tableWidget.setItem(row, column, QTableWidgetItem(item))