0

I´m looking for a smart way to select a specific Row without using the curser or klick inside my QSqlTableModel. Instead I would like to use a QLineEdit.

So here is the function I´m using for my QLineEdit, which works well.

def update_filter_ID(self, s):
        s = re.sub(r'[\W_]+', "", s)
        filter_str = f"[Article Number] LIKE '%{s}%'".format(s)
        
        self.ui.model.setFilter(filter_str)
        self.ui.DB_View.selectRow(0)

self.ui.search_ID.textChanged.connect(self.update_filter_ID)

I also got a function to get information which ID the User has clicked:

def right_click_action(self):
        
        global current_row
        
        rows = self.ui.DB_View.selectionModel().selectedIndexes()
        current_row= self.ui.model.record(rows[0].row()).value(0)
        print(current_row)
        return current_row

Now I would like to combine the functions to let the model select the row, whenever the filter string is matching the ID.

I thought about making a List of ID´s and then compare the filter_str with a loop. But I would like to know, if there is a more elegant way to do so.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Nullschall
  • 11
  • 6
  • [QTableWidget.findItems()](https://doc.qt.io/qt-5/qtablewidget.html#findItems). – ekhumoro Jul 12 '22 at 17:17
  • @ekhumoro but the OP is using a QTableView... – musicamante Jul 12 '22 at 17:22
  • Sorry, maybe I was a bit unclear. I´m using QSqlTableModel and not a TableWidget. Still appreciate your answer. It would help if I would use the TableWidget. – Nullschall Jul 12 '22 at 17:24
  • Then use [QAbstractItemModel.match()](https://doc.qt.io/qt-5/qabstractitemmodel.html#match) (which is what `findItems` uses internally). – ekhumoro Jul 12 '22 at 18:18
  • @ekhumoro AFAIU the OP wants to match the table unique ID's, which are normally mapped to the vertical header, and are not usually considered for `match()`. – musicamante Jul 13 '22 at 03:15
  • @musicamante I don't know where you're gettinhg that from. The OP's code takes the ID from `model.record(row).value(0)`, which is the first column. The table can be filtered by the "Article Number" column, but they also want to select any rows where the "ID" column matches the input value (i.e. *without* any extra filtering). The `match()` method will work just fine for that. I don't see the relevance of the vertical header. I'm assuming the OP is using a sane table schema with a primary key (which corresponds to the "ID" column). – ekhumoro Jul 13 '22 at 10:42
  • @Nullschall If your requirements somehow aren't as discussed above, please edit your question and clarify accordingly (perhaps also including a [mre]). Otherwise, I can post an answer with a simple demo that uses `match()` if you'd find that helpful. – ekhumoro Jul 13 '22 at 10:58
  • @ekhumoro first thank you for your answer. I really appreciate that. I will build a "mre" so everyone it is more clear. this will take me a while so please be patient. – Nullschall Jul 14 '22 at 05:42

0 Answers0