Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:35:28+00:00 2026-06-10T16:35:28+00:00

I have a Python and PySide app that connects to a mysql database and

  • 0

I have a Python and PySide app that connects to a mysql database and displays the results of a query in a QTableView.
I need to print the contents of the table view.
Here’s some code:

    self.db_table = QtGui.QTableView(self)
    self.model =  QtSql.QSqlQueryModel()
    self.model.setQuery("SELECT * FROM simpsons")
    self.model.setHeaderData(1, QtCore.Qt.Horizontal, self.tr("First Name"))
    self.model.setHeaderData(2, QtCore.Qt.Horizontal, self.tr("Last Name"))
    self.db_table.setModel(self.model) 

    self.print_btn = QtGui.QPushButton("Print")
    self.print_btn.clicked.connect(self.print_btn_clicked)

    def print_btn_clicked(self):
        printDialog = QtGui.QPrintDialog(self.printer, self)
        if printDialog.exec_() == QtGui.QDialog.Accepted:
         #printing code

I can’t find an example for this and I don’t understand much from the documentation so I’d appreciate some help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:35:29+00:00Added an answer on June 10, 2026 at 4:35 pm

    One way to do it is to dump the table contents into a QTextDocument, and then print that.

    The following demo uses a simple text-table, but html could be used to get more sophisticated formatting:

    from PyQt4 import QtGui, QtCore
    
    class Window(QtGui.QWidget):
        def __init__(self, rows, columns):
            QtGui.QWidget.__init__(self)
            self.table = QtGui.QTableView(self)
            model =  QtGui.QStandardItemModel(rows, columns, self.table)
            for row in range(rows):
                for column in range(columns):
                    item = QtGui.QStandardItem('(%d, %d)' % (row, column))
                    item.setTextAlignment(QtCore.Qt.AlignCenter)
                    model.setItem(row, column, item)
            self.table.setModel(model)
            self.buttonPrint = QtGui.QPushButton('Print', self)
            self.buttonPrint.clicked.connect(self.handlePrint)
            self.buttonPreview = QtGui.QPushButton('Preview', self)
            self.buttonPreview.clicked.connect(self.handlePreview)
            layout = QtGui.QGridLayout(self)
            layout.addWidget(self.table, 0, 0, 1, 2)
            layout.addWidget(self.buttonPrint, 1, 0)
            layout.addWidget(self.buttonPreview, 1, 1)
    
        def handlePrint(self):
            dialog = QtGui.QPrintDialog()
            if dialog.exec_() == QtGui.QDialog.Accepted:
                self.handlePaintRequest(dialog.printer())
    
        def handlePreview(self):
            dialog = QtGui.QPrintPreviewDialog()
            dialog.paintRequested.connect(self.handlePaintRequest)
            dialog.exec_()
    
        def handlePaintRequest(self, printer):
            document = QtGui.QTextDocument()
            cursor = QtGui.QTextCursor(document)
            model = self.table.model()
            table = cursor.insertTable(
                model.rowCount(), model.columnCount())
            for row in range(table.rows()):
                for column in range(table.columns()):
                    cursor.insertText(model.item(row, column).text())
                    cursor.movePosition(QtGui.QTextCursor.NextCell)
            document.print_(printer)
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication(sys.argv)
        window = Window(25, 2)
        window.resize(300, 400)
        window.show()
        sys.exit(app.exec_())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Python code that pulls info from a sqlite database and then write
We have a PyQt4/PySide Qt4 video player app that has QToolButton object in the
I'm using Python and PySide (the PyQt alternative). I have an application that runs
I have python code below that will loop through a table and print out
I have Python nested list that I'm trying to organize and eventually count number
I have python script that parse xml file and execute commands. I want my
I have python code that needs to do just a couple simple things to
I have python code that contains the following code. d = {} d[(0,0)] =
I have been learning Python on Codecademy.com and transferred a project that worked on
I have installed Qt designer 4.8.2, Pyside, and Python 3.3. When I create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.