[PyQt] QTableView header connection

Mads Ipsen mpi at comxnet.dk
Wed Mar 18 15:36:56 GMT 2009


>
> Hey all,
>
> Since I still haven't been able to
> http://www.nabble.com/Sorting-column-with-QComboBox-cellwidgets-td22499840.html
> automatically sort a column with combo boxes , and now have another column
> with special sorting conditions, I'd like to manually do the sorting.
>
> The problem is that I can't seem to make a connection from the header.
> This
> is (very simplified) what I'm doing now:
>
> Class GUI:
> 	def __init__(self):
> 		tableWidget = QtGui.QTableWidget()
> 		# Code to insert 5 columns and make a horizontal header)
> 		self.connect(tableWidget.horizontalHeader, QtCore.SIGNAL("clicked()"),
> self.random_function)
>
> 	def random_function(self):
> 		print 'ok'
>
> However, double-clicking the header does not make the console print 'ok'.
>
> Sooo, anyone got an idea why this is not working?
>
> Thanks in advance for any help!
>
> Cheers,
>
> Gert-Jan
> --
> View this message in context:
> http://www.nabble.com/QTableView-header-connection-tp22579905p22579905.html
> Sent from the PyQt mailing list archive at Nabble.com.
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>

You should use the method horizontalHeader() not horizontalHeader when you
connect. The signal you should connect to is 'sectionClicked (int)', I
have included a small example below that hopefully should get you going.

Best, Mads

import sys
from PyQt4 import QtGui, QtCore

class Table(QtGui.QTableWidget):
    def __init__(self, parent=None):
        QtGui.QTableWidget.__init__(self, parent)

        # Add 3 rows
        for i in xrange(3):
            self.insertRow(i)

        # Add 3 cols
        for i in xrange(3):
            self.insertColumn(i)

        # Add some cell data
        for i in xrange(3):
            for j in xrange(3):
                item = QtGui.QTableWidgetItem()
                item.setText(str(i+j))
                self.setItem(i,j,item)

        # Connect
        self.connect(self.horizontalHeader(),
                     QtCore.SIGNAL('sectionClicked (int)'),
                     self.random_function)

    def random_function(self, i):
        print 'Header was clicked on column %d' % (i)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    widget = Table()
    widget.show()

    sys.exit(app.exec_())


More information about the PyQt mailing list