Iterate QPushButton
<p>I am using PyQt4. I have a QPushButton and now I want to Iterate it. On button click, it should load data from csv file into QTableWidget. But I want only one case to display at a time. </p>

<p>For example csv has 1000 rows excluding headers. Now it should assign header to table widget from header. and display only one row below it. So on click, it should display next row information in same row. I am posting code below with little different syntax. I displayed syntax of db for header which i also want to exclude it.</p>

<p>I added .ui file. you can save it directly as .ui:</p>

<pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;ui version="4.0"&gt;
 &lt;class&gt;remarks&lt;/class&gt;
 &lt;widget class="QMainWindow" name="remarks"&gt;
  &lt;property name="geometry"&gt;
   &lt;rect&gt;
    &lt;x&gt;0&lt;/x&gt;
    &lt;y&gt;0&lt;/y&gt;
    &lt;width&gt;1073&lt;/width&gt;
    &lt;height&gt;862&lt;/height&gt;
   &lt;/rect&gt;
  &lt;/property&gt;
  &lt;property name="windowTitle"&gt;
   &lt;string&gt;MainWindow&lt;/string&gt;
  &lt;/property&gt;
  &lt;widget class="QWidget" name="centralwidget"&gt;
   &lt;widget class="QComboBox" name="compcb"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;60&lt;/x&gt;
      &lt;y&gt;360&lt;/y&gt;
      &lt;width&gt;131&lt;/width&gt;
      &lt;height&gt;27&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QComboBox" name="loccb"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;60&lt;/x&gt;
      &lt;y&gt;410&lt;/y&gt;
      &lt;width&gt;131&lt;/width&gt;
      &lt;height&gt;27&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QComboBox" name="rescb"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;60&lt;/x&gt;
      &lt;y&gt;460&lt;/y&gt;
      &lt;width&gt;131&lt;/width&gt;
      &lt;height&gt;27&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QLineEdit" name="lat"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;60&lt;/x&gt;
      &lt;y&gt;540&lt;/y&gt;
      &lt;width&gt;113&lt;/width&gt;
      &lt;height&gt;27&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QLineEdit" name="lon"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;60&lt;/x&gt;
      &lt;y&gt;590&lt;/y&gt;
      &lt;width&gt;113&lt;/width&gt;
      &lt;height&gt;27&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QLineEdit" name="landmark"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;330&lt;/x&gt;
      &lt;y&gt;360&lt;/y&gt;
      &lt;width&gt;113&lt;/width&gt;
      &lt;height&gt;27&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QPlainTextEdit" name="sugges"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;330&lt;/x&gt;
      &lt;y&gt;410&lt;/y&gt;
      &lt;width&gt;121&lt;/width&gt;
      &lt;height&gt;78&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QPlainTextEdit" name="plainTextEdit_2"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;330&lt;/x&gt;
      &lt;y&gt;510&lt;/y&gt;
      &lt;width&gt;121&lt;/width&gt;
      &lt;height&gt;78&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QTableWidget" name="tableWidget"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;20&lt;/x&gt;
      &lt;y&gt;10&lt;/y&gt;
      &lt;width&gt;991&lt;/width&gt;
      &lt;height&gt;311&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QPushButton" name="submit"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;350&lt;/x&gt;
      &lt;y&gt;670&lt;/y&gt;
      &lt;width&gt;99&lt;/width&gt;
      &lt;height&gt;41&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="font"&gt;
     &lt;font&gt;
      &lt;pointsize&gt;12&lt;/pointsize&gt;
      &lt;weight&gt;50&lt;/weight&gt;
      &lt;bold&gt;false&lt;/bold&gt;
     &lt;/font&gt;
    &lt;/property&gt;
    &lt;property name="text"&gt;
     &lt;string&gt;Submit&lt;/string&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
  &lt;/widget&gt;
  &lt;widget class="QMenuBar" name="menubar"&gt;
   &lt;property name="geometry"&gt;
    &lt;rect&gt;
     &lt;x&gt;0&lt;/x&gt;
     &lt;y&gt;0&lt;/y&gt;
     &lt;width&gt;1073&lt;/width&gt;
     &lt;height&gt;25&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
  &lt;/widget&gt;
  &lt;widget class="QStatusBar" name="statusbar"/&gt;
 &lt;/widget&gt;
 &lt;resources/&gt;
 &lt;connections/&gt;
&lt;/ui&gt;
</code></pre>

<pre class="lang-python prettyprint-override"><code>from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import QString

from PyQt4.QtGui import * 
from PyQt4.QtCore import *

import MySQLdb
import os
import time
import sys
import csv 

### Loading .UI file ###
rts_class = uic.loadUiType("main.ui")[0]

class Mainwindow(QtGui.QMainWindow, rts_class):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)

        #self.review.clicked.connect(self, review_application)
        self.submit.clicked.connect(self.submit_application)
        #self.quit.clicked.connect(self, quit_application

        self.load_db()
        self.checkbox()

    def load_db(self):
        self.dadis.setRowCount(1)
        self.dadis.setColumnCount(headlen)
        self.dadis.setHorizontalHeaderLabels(QString('%s' % ', '.join(map(str, mainheader))).split(","))

        if os.path.isfile("RTS.csv") is True:
            with open('RTS.csv', 'r') as fo:
                reader = csv.reader(fo, delimiter = ',')
                ncol = len(next(reader))
                data = list(reader)
                row_count = len(data)

                print row_count
                main =  data[0]

                print main

                for var in range(0, ncol):                                                  
                    self.dadis.setItem(0, var, QTableWidgetItem(main[var]))

                fo.close()

    def checkbox(self):
        self.compcb.addItem(" ")
        self.compcb.addItem("Complete")
        self.compcb.addItem("InComplete")

        self.loccb.addItem(" ")
        self.loccb.addItem("Locatable")
        self.loccb.addItem("UnLocatable")

        self.rescb.addItem(" ")
        self.rescb.addItem("House")
        self.rescb.addItem("Street")
        self.rescb.addItem("Colony")
        self.rescb.addItem("Society")            

    def submit_application(self):
        compout = self.compcb.currentText()
        locout = self.loccb.currentText()
        resout = self.rescb.currentText()
        lattxt = self.lat.text()
        lontxt = self.lon.text()
        landtxt = self.landmark.text()
        suggestxt = self.sugges.toPlainText()
        remarkstxt = self.remarks.toPlainText()

        print compout
        print locout
        print resout
        print lattxt
        print lontxt
        print landtxt
        print suggestxt
        print remarkstxt

        if os.path.isfile("rts_output.csv") is False:

            with open('rts_output.csv', 'a') as fp:
                b = csv.writer(fp, delimiter = ',')
                header = [["COMPLETENESS", "LOCATABLE", "RESOLUTION", "GEO_LAT", "GEO_LON", "LANDMARK", "SUGGESTION", "REMARKS"]]
                b.writerows(header)

        if os.path.isfile("rts_output.csv") is True:

            with open('rts_output.csv', 'a') as fp:
                a = csv.writer(fp, delimiter = ',' )
                data = [[compout, locout, resout, lattxt, lontxt, landtxt, suggestxt, remarkstxt]]
                a.writerows(data)   

        if os.path.isfile("RTS.csv") is True:

            with open('RTS.csv', 'r') as fo:
                reader = csv.reader(fo, delimiter = ',')
                ncol = len(next(reader))
                data = list(reader)
                row_count = len(data)

                x = data[0][0]
                print x

                i = int(x)+1
                print i

                if i &lt;= row_count:

                    main =  data[i-1]

                    print main

                #for var in range(0, ncol):                                                  
                    #self.dadis.setItem(0, var, QTableWidgetItem(main[var]))

            fo.close()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    myMain = Mainwindow()
    myMain.show()
    sys.exit(app.exec_())
</code></pre>

<p>The main problem was, when ever the button was clicked, the iterator is getting set to 0 or 1 what ever it was assigned. So, assign the variable outside of the class and call it into the class to maintain the loop structure.</p>

<pre><code>class staticVariable:
    static_count_clicked = 1

class Mainwindow(QtGui.QMainWindow, rts_class):
    def __init__(self, parent=None, *args, **kwargs):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)

        self.submit.clicked.connect(self.submit_application)

    def submit_application(self, count_clicked):

        staticVariable.static_count_clicked += 1
        print staticVariable.static_count_clicked
</code></pre>

python pyqt pyqt4 qpushbutton import-csv