title: PyQt: viewing query results in a document or .txt file
body: <p>Is it possble to view results of this query (and queries alike) in a .txt or .doc or any printable file, which user can then save wherever he likes and prints if he wants to? </p>
  
  <pre><code>query.setQuery(""" SELECT * FROM "%s" WHERE METAPHONE("%s", 3) = METAPHONE('%s', 3) OR LEVENSHTEIN("%s", '%s') &lt; 4 """ % (str(self.search_from_table_lineEdit.text()), str(self.search_where_lineEdit.text()), str(self.search_has_value_lineEdit.text()), str(self.search_where_lineEdit.text()), str(self.search_has_value_lineEdit.text())))
                       global var
                       var = QtGui.QTableView()
                       var.setModel(query)
                       var.show()
  </code></pre>
  
  <p><strong>EDIT:</strong>
  I tried this:</p>
  
  <pre><code>class report_GUI(QtGui.QWidget, Ui_report_Widget):
         def __init__(self):
                QtGui.QWidget.__init__(self)
                self.setupUi(self)
                self.report_Create_Report_button.clicked.connect(self.report_data)
  
         def report_data(self):
                FILE_NAME = 'export.csv'
                exportQSqlQueryModel = QSqlQueryModel()
                exportQSqlQueryModel.setQuery(""" SELECT * FROM "%s" WHERE "%s" = '%s' """ % (str(self.report_from_table_lineEdit.text()), str(self.report_where_lineEdit.text()), str(self.report_has_value_lineEdit.text())))
                exportFile = open(FILE_NAME, 'wt')
                writer = csv.writer(exportFile)
  
                for row in exportQSqlQueryModel.rowCount():
                       listsTmpData = []
                       for column in exportQSqlQueryModel.columnCount():
                              listsTmpData.append(str(exportQSqlQueryModel.record(row).value(column).toString()))
                       writer.writerow(listsTmpData)
                exportFile.close()
  </code></pre>
  
  <p>And got this error: </p>
  
  <pre><code>line 563, in report_data
      for row in exportQSqlQueryModel.rowCount():
  TypeError: 'int' object is not iterable
  </code></pre>
  
  <p><strong>EDIT2</strong>:</p>
  
  <pre><code>def report_data(self):
                FILE_NAME = 'export.csv'
                exportQSqlQueryModel = QSqlQueryModel()
                exportQSqlQueryModel.setQuery(""" SELECT * FROM "%s" WHERE "%s" = '%s' """ % (str(self.report_from_table_lineEdit.text()), str(self.report_where_lineEdit.text()), str(self.report_has_value_lineEdit.text())))
                exportFile = open(FILE_NAME, 'wt')
                writer = csv.writer(exportFile)
  
                if (self.report_from_table_lineEdit.text() == "pacijent"):
                    exportQSqlQueryModel.setHeaderData(0, QtCore.Qt.Horizontal, 'OIB pacijenta')
                    exportQSqlQueryModel.setHeaderData(1, QtCore.Qt.Horizontal, 'Prezime')
                    exportQSqlQueryModel.setHeaderData(2, QtCore.Qt.Horizontal, 'Ime')
                    exportQSqlQueryModel.setHeaderData(3, QtCore.Qt.Horizontal, 'ID sobe')
                    exportQSqlQueryModel.setHeaderData(4, QtCore.Qt.Horizontal, 'Adresa')
                    exportQSqlQueryModel.setHeaderData(5, QtCore.Qt.Horizontal, 'Datum rodjenja')
                    exportQSqlQueryModel.setHeaderData(6, QtCore.Qt.Horizontal, 'Spol')
  
                listsTmpData = []
                for row in range(exportQSqlQueryModel.rowCount()):
                       listsTmpData.append(str(exportQSqlQueryModel.headerData(row, QtCore.Qt.Horizontal)))
                writer.writerow(listsTmpData)
  
                for row in range(exportQSqlQueryModel.rowCount()):
                       listsTmpData = []
                       for column in range(exportQSqlQueryModel.columnCount()):
                              listsTmpData.append(str(exportQSqlQueryModel.record(row).value(column)))
                       writer.writerow(listsTmpData)
                exportFile.close()
  </code></pre>
  
  <p>It shows only the first 4 column titles and it looks like this: <a href="http://oi62.tinypic.com/2guxf5t.jpg" rel="nofollow">http://oi62.tinypic.com/2guxf5t.jpg</a></p>