8

I have a scrollview to which I dynamically add QTableWidgets. However, the QTables themselves also have scrollbars and therefore don't show the full table. Is there a way to disable the scroll bar so that the table always gets shown in full?

enter image description here

EDIT: I added

    self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

As suggested. The scroll bar does disappear, but it still only shows the partial tables (Ican scroll with hovering iverthe table and using the mouse wheel, still). The code for the Widget is below

from PySide.QtGui import *
from PySide.QtCore import *

class MdTable(QTableWidget):
    def __init__(self, data, depth, *args):

        QTableWidget.__init__(self, *args)
        self.hheaders = ["c1", "c2", "c3", "c4"]
        self.depth = depth
        self.bids = data
        self.setData()

    def setData(self):

        self.setRowCount(self.depth)
        self.setColumnCount(5)

        for i in xrange(self.depth):
            if len(self.data) > i:
                d1= QTableWidgetItem(str(self.data[i][0]))
                d2= QTableWidgetItem(str(self.data[i][1]))
                self.setItem(i, 1, d1)
                self.setItem(i, 2, d2)

        self.setHorizontalHeaderLabels(self.hheaders)
        self.verticalHeader().setVisible(False)
        self.resizeRowsToContents()
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
chrise
  • 4,039
  • 3
  • 39
  • 74

1 Answers1

5

If you just want to delete the Scrollbar you must use:

{QtableWidget}.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
{QtableWidget}.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

If you want to show the expanded QTableWidget, add this to the end of the setData() method:

self.setMaximumSize(self.getQTableWidgetSize())
self.setMinimumSize(self.getQTableWidgetSize())

and define getQTableWidgetSize(self) like this:

def getQTableWidgetSize(self):
    w = self.verticalHeader().width() + 4  # +4 seems to be needed
    for i in range(self.columnCount()):
        w += self.columnWidth(i)  # seems to include gridline (on my machine)
    h = self.horizontalHeader().height() + 4
    for i in range(self.rowCount()):
        h += self.rowHeight(i)
    return QtCore.QSize(w, h)

enter image description here

Note: The function getQTableWidgetSize is a conversion of the code in C ++ to python of the following post: How to determine the correct size of a QTableWidget?

Community
  • 1
  • 1
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • from PySide.QtCore import * self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) I added this to the class that inherits QTableWidget. But it does not change anything – chrise Jan 09 '17 at 07:37
  • @chrise self is QTableWidget? – eyllanesc Jan 09 '17 at 07:38
  • yes. It s in this class class DataTable(QTableWidget): – chrise Jan 09 '17 at 07:39
  • actually, I was mistaken. The scrollbar disappeared, but it still shows the reduced data, not the full table – chrise Jan 09 '17 at 07:50
  • @chrise I guess what you want is if there is more information displayed by QTableWidget the font size decreases. – eyllanesc Jan 09 '17 at 08:00
  • Is there another way. That would render text very unreadable as currently it displays two rows and I want 5 to 10, so text would be tiny – chrise Jan 09 '17 at 08:02
  • I tried using resize, both from inside the QtableWidget as self.resize(.,.) as well as from the outside, w.resize(.,.) but this seems to get completely ignored – chrise Jan 09 '17 at 08:03
  • @chrise Can you show me the image that comes with my suggestion? – eyllanesc Jan 09 '17 at 08:16