# QCombobox Case selection - Pyside2 Index via Sqlite

I have a very simple file where I have a Qcombobox Filter. When you select the filter, I run through a case on the index and output results.

So, on selection I trigger:

self.comboBox.activated.connect(self.handleActivated)


Then,

my handled function is

    def handleActivated(self, index):
db.setDatabaseName("data.db")
db.open()
# tableView = QtWidgets.QTableView()

if index == 0:
sourceModel = QSqlQueryModel()
sourceModel.setQuery(
db)
proxyModel = QSortFilterProxyModel(self)
proxyModel.setSourceModel(sourceModel)
self.tableView.setModel(proxyModel)
self.tableView.setSortingEnabled(True)
elif index == 1:
sourceModel = QSqlQueryModel()
sourceModel.setQuery(
"SELECT url,size,content_type FROM 'google.com.au' WHERE content_type LIKE '%html%'",
db)
proxyModel = QSortFilterProxyModel(self)
proxyModel.setSourceModel(sourceModel)
self.tableView.setModel(proxyModel)
self.tableView.setSortingEnabled(True)
elif index == 2:
sourceModel = QSqlQueryModel()
sourceModel.setQuery(
"SELECT url,size,content_type FROM 'google.com.au' WHERE content_type LIKE '%javascript%'",
db)
proxyModel = QSortFilterProxyModel(self)
proxyModel.setSourceModel(sourceModel)
self.tableView.setModel(proxyModel)
self.tableView.setSortingEnabled(True)


My question is, is this the most efficient way of writing this code ? I have upto 15 filters and this will become really cumbersome in my main file.

• Welcome to CodeReview@SE. Well done on acting on your doubts about the advisability of the approach presented. – greybeard May 18 at 9:20

Well you have already identified the problem: repetition. It can be tackled fairly easily. Instead of that if/elif block you can use a (zero-indexed) list that contains the WHERE clause for the query:

    options = [ None, '%html%', '%javascript%']
query = "SELECT url,size,content_type FROM 'google.com.au'"

if options[index] is not None:
query += f" WHERE content_type LIKE '{options[index]}'"

sourceModel = QSqlQueryModel()
sourceModel.setQuery(query, db)
proxyModel = QSortFilterProxyModel(self)
proxyModel.setSourceModel(sourceModel)
self.tableView.setModel(proxyModel)
self.tableView.setSortingEnabled(True)


Only the first option is different: there is no WHERE clause. In the other places the request is always the same, it's only the search pattern that is different.

What I am doing here is simply selective string concatenation. Note: using an F-string for the WHERE clause, requires Python >= 3.6. Usage is not mandatory but if you have a recent version of Python embrace the new features.

Instead of None you could say '' (an empty string). I chose None to signify option #0 is a special case. I am not checking that index is actually in the range 0->2. If you think this could happen, add a if to check.

• Wow ! Thank you, that makes much more sense. On running your code I got a small error TypeError: can only concatenate tuple (not “str”) to tuple Error' which I fixed by adding a ()` on the query and it works as expected. – Sam May 19 at 0:23