Problem with selecting next/previowus record
-
Hi everyone,
I have a problem with selecting next or previous selected record (all rows) in TableView.
It works only one - only choose the next/previous record, but If I press pushbutton to start this fuction again nothing change - doesn't choose next record.mayby I forgot sth to add to change current Index to, but I don't know what?
def NextOne(self): ind= self.dlg.tableView_3.selectionModel() ind2= ind.currentIndex() if ind2: next1 = ind2.siblingAtRow(ind2.row() + 1)#prev has: ind2.row() - 1 self.dlg.tableView_3.selectionModel().select(next1,ind.ClearAndSelect | ind.Rows) self.selectRow() else: next1 = ind2.model().index() self.dlg.tableView_3.selectRow(0)
-
Hi,
Do you mean the else clause where you would use what is an invalid index ?
-
@SGaist else for me is for situation when none of records in Table View is choosen ex. a little after pressing the button which shows the result of sql hidden in button
-
Can you provide a minimal script that shows the behavior ?
It's not exactly clear from your description. -
I attached the whole of the code of this funtion above, I don't know what more can I add here
-
Can you provide a minimal script that shows the behavior ?
It's not exactly clear from your description.@SGaist ! I put a photo with a situation was that funtion does make:
()
-
Please don't publish it in any media!
-
That is why I was requesting a minimal runnable script to that would show your issue.
From the looks of it, you can use QTableView's currentIndex to grab the currently selected index.
From a code point of view, you should refactor your NextOne function. It's hard to read and reason about because you are not using meaningful variable names. Also, NextOne does not follow python best practices for function naming.
Here a simplified and clearer version:
def select_next_row(self): selection_model = self.dlg.tableView_3.selectionModel() current_index = selection_model.currentIndex() if current_index.isValid(): next_index = current_index.siblingAtRow(current_index.row() + 1) if next_index.isValid(): selection_model.select(next_index, QItemSelectionModel.ClearAndSelect | QItemSelectionModel.Rows)
Unless you want to implement wrapping from the bottom to the top of your table view, there's nothing more that method should do.
-
Thnaks, but still change only to first next.
What kind of code (for which funtion) you want to see to find better solution for my problem? -
Thnaks, but still change only to first next.
What kind of code (for which funtion) you want to see to find better solution for my problem?@Karoluss96 said in Problem with selecting next/previowus record:
What kind of code
A runnable script as small as possible which shows this behaviour.
-
Function for showing records in tableView looks that:
def present_data(self): query = QSqlQuery(db) query.exec_(Zap) global ier ier= ['ID','TERYT'] #and more atributtes self.model4.clear() self.model4.setHorizontalHeaderLabels(ier) x = 0 while query.next(): x += 1 self.model4.setItem(x, 0, QStandardItem(str(int(query.record().value(ier[0])))))#ID self.model4.setItem(x, 1, QStandardItem(str(query.record().value(ier[1]))))#code of ad level 1 #next atributtes in table 'ier' self.dlg.tableView_3.clicked.connect(self.SelekcjaRzedu)
and next:
def selectRow(self): dane=self.dlg.tableView_3.selectionModel().currentIndex() rzad=dane.row() self.dlg.label_53.setText(str(self.dlg.comboBox_9.currentText()))#administrative level 2 woj= ((self.dlg.comboBox_3.currentText())[0:-5]).lower()#administrative level 1 a1=dane.siblingAtRow(rzad).siblingAtColumn(0).data()#ID b2=dane.siblingAtRow(rzad).siblingAtColumn(1).data()#code of ad level 1 #next attributes... if woj !='do':#not put NULL in labels self.dlg.label_82.setText(woj) else: self.dlg.label_82.setText('') if a1 != 'NULL': self.dlg.label_32.setText(str(a1)) else: self.dlg.label_32.setText('')
-
Function for showing records in tableView looks that:
def present_data(self): query = QSqlQuery(db) query.exec_(Zap) global ier ier= ['ID','TERYT'] #and more atributtes self.model4.clear() self.model4.setHorizontalHeaderLabels(ier) x = 0 while query.next(): x += 1 self.model4.setItem(x, 0, QStandardItem(str(int(query.record().value(ier[0])))))#ID self.model4.setItem(x, 1, QStandardItem(str(query.record().value(ier[1]))))#code of ad level 1 #next atributtes in table 'ier' self.dlg.tableView_3.clicked.connect(self.SelekcjaRzedu)
and next:
def selectRow(self): dane=self.dlg.tableView_3.selectionModel().currentIndex() rzad=dane.row() self.dlg.label_53.setText(str(self.dlg.comboBox_9.currentText()))#administrative level 2 woj= ((self.dlg.comboBox_3.currentText())[0:-5]).lower()#administrative level 1 a1=dane.siblingAtRow(rzad).siblingAtColumn(0).data()#ID b2=dane.siblingAtRow(rzad).siblingAtColumn(1).data()#code of ad level 1 #next attributes... if woj !='do':#not put NULL in labels self.dlg.label_82.setText(woj) else: self.dlg.label_82.setText('') if a1 != 'NULL': self.dlg.label_32.setText(str(a1)) else: self.dlg.label_32.setText('')
@Karoluss96 This is not a RUNNABLE script...
-
you want the run function?
-
the run function to open this gui looks that:
def run(self):
global TERYT, qmlPath, mainPath, controlPath, db, connectionParam, schema, password, database, port, host, oznaczenieZmiany# paths to files qmlPath = Path(QgsApplication.qgisSettingsDirPath())/Path("python/plugins/SystemBDOT10k/BDOT10k_QML/") mainPath = Path(QgsApplication.qgisSettingsDirPath())/Path("python/plugins/SystemBDOT10k/") controlPath = Path(QgsApplication.qgisSettingsDirPath())/Path("python/plugins/SystemBDOT10k/Kontrole/") self.dlg = SystemBDOT10k_dialog() self.dlg.setWindowFlag(Qt.WindowMinimizeButtonHint, True) self.dlg.rejected.connect(self.close) #get parameters from file SystemBDOT10k.ini config = configparser.ConfigParser() config.read(str(mainPath)+'/SystemBDOT10k.ini') connectionParam = config['oracle'] schema = (base64.b64decode((connectionParam['Schema']).encode('utf-8'))).decode('utf-8') password = (base64.b64decode((connectionParam['Password']).encode('utf-8'))).decode('utf-8') port = connectionParam['Port'] host = connectionParam['Host'] database = connectionParam['Database'] markChange = config['MainSettings']['markChange'] # connect to data base db = QSqlDatabase.addDatabase("QOCISPATIAL") db.setDatabaseName(host + ':' + port + '/' + database) db.setUserName(schema) db.setPassword(password) ok = db.open() self.operetionOnData() self.dlg.comboBox.currentTextChanged.connect(self.chooseStatus) self.statusPowiatu() self.chooseStatus() # selecting QML folders to comboBox path = os.listdir(str(qmlPath).replace("\\","/")) length = len(path) for i in range(length): self.dlg.comboBox_1.addItem(path[i]) self.dlg.show() result = self.dlg.exec_() # Run the dialog event loop
-
the run function to open this gui looks that:
def run(self):
global TERYT, qmlPath, mainPath, controlPath, db, connectionParam, schema, password, database, port, host, oznaczenieZmiany# paths to files qmlPath = Path(QgsApplication.qgisSettingsDirPath())/Path("python/plugins/SystemBDOT10k/BDOT10k_QML/") mainPath = Path(QgsApplication.qgisSettingsDirPath())/Path("python/plugins/SystemBDOT10k/") controlPath = Path(QgsApplication.qgisSettingsDirPath())/Path("python/plugins/SystemBDOT10k/Kontrole/") self.dlg = SystemBDOT10k_dialog() self.dlg.setWindowFlag(Qt.WindowMinimizeButtonHint, True) self.dlg.rejected.connect(self.close) #get parameters from file SystemBDOT10k.ini config = configparser.ConfigParser() config.read(str(mainPath)+'/SystemBDOT10k.ini') connectionParam = config['oracle'] schema = (base64.b64decode((connectionParam['Schema']).encode('utf-8'))).decode('utf-8') password = (base64.b64decode((connectionParam['Password']).encode('utf-8'))).decode('utf-8') port = connectionParam['Port'] host = connectionParam['Host'] database = connectionParam['Database'] markChange = config['MainSettings']['markChange'] # connect to data base db = QSqlDatabase.addDatabase("QOCISPATIAL") db.setDatabaseName(host + ':' + port + '/' + database) db.setUserName(schema) db.setPassword(password) ok = db.open() self.operetionOnData() self.dlg.comboBox.currentTextChanged.connect(self.chooseStatus) self.statusPowiatu() self.chooseStatus() # selecting QML folders to comboBox path = os.listdir(str(qmlPath).replace("\\","/")) length = len(path) for i in range(length): self.dlg.comboBox_1.addItem(path[i]) self.dlg.show() result = self.dlg.exec_() # Run the dialog event loop
@Karoluss96 Runnable means in this case: something somebody can download/copy from here and drectly execute to reproduce your issue.
-
This is a QGIS plugin with GUI, so it isn't possible to put it outside of QGIS program (which you can download for free here: https://qgis.org/pl/site/forusers/download.html)