0

I am trying to put a QtableWidget inside a QScrollArea (only one widget) to be able to scroll it vertically and horizontaly (I have reasons not to use scrollbars in Qtablewidget ). However, no scrollbar appears even though the tableWidget can’t fit inside the window so I set QtCore.Qt.ScrollBarAlwaysOn, and now they are there but they are gray and still I can't scroll.

Here is my code:

class Table(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Table, self).__init__(parent)
        layout = QtGui.QGridLayout() 
        tableWidget = QtGui.QTableWidget()
        #.... set up and populate tableWidget here 1000rows-10col ....
        myScrollArea = QtGui.QScrollArea()
        myScrollArea.setWidgetResizable(True)
        myScrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        myScrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        myScrollArea.setWidget(tableWidget)
        layout.addWidget(myScrollArea)
        self.setLayout(layout)
        self.setMinimumSize(1000, 700)

I am begginer with PyQt and I don't really understand layouts and containers, so I can't figure out what I'm doing wrong. Please point me in right direction, help would be appreciated.

Aleksandar
  • 3,541
  • 4
  • 34
  • 57

2 Answers2

1

QtScrollBar by default has horizontal and vertical scrollBar. tablewidget by default has horizontal and vertical scrollBar. so i have made it off. just using the resize event i have resized width and height of tablewidget.

class MainWin(QtGui.QDialog):
def __init__(self,parent=None):
    QtGui.QDialog.__init__(self,parent)

    self.table =QtGui.QTableWidget(100,4)
    self.table.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    self.table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

    lay = QtGui.QGridLayout()
    self.sc = QtGui.QScrollArea()
    self.sc.setWidget(self.table)
    lay.addWidget(self.sc,0,0)
    self.setLayout(lay)


def resizeEvent(self,event):
    self.table.resize(self.sc.width(),self.sc.height())




def main():
    app=QtGui.QApplication(sys.argv)
    win=MainWin()
    win.show()
    sys.exit(app.exec_())

main()
raton
  • 418
  • 5
  • 14
  • thank you for your answer. I'have also did `ScrollBarAlwaysOff` for table like you. When I run your code I can see QScrollArea's scrollbars and 5 rows, but I can scroll down only to see one more row. I'am using PyQt4 and Python 2.6. Can you scroll down to all 100 rows with your code? please help me this brings me headache for a while now – Aleksandar Jul 01 '13 at 07:51
  • Oh, I forgot to mention that I need to use `self.table.resizeRowsToContents()` and `self.table.resizeColumnsToContents()` after the table is populated – Aleksandar Jul 01 '13 at 07:54
0

I finally get it: I've used resizeColumnsToContents() and resizeRowsToContents() to make the columns/rows of the table adjust to the data - text, but that doesn't do the same thing with the Table itself - table height and width stays the same. So in order to make table to be sized around the rows and columns I've used this:

self.table.resizeRowsToContents()
self.table.resizeColumnsToContents()   
self.table.setFixedSize(self.table.horizontalHeader().length(), self.table.verticalHeader().length())

and now I can scroll with QScrollArea's scrollbars through entire table.

Aleksandar
  • 3,541
  • 4
  • 34
  • 57