Search code examples
pythonc++qtpyqt4qtablewidget

How to make sure all of the data on a Qt table cell is visible?


I am currently working with python 2.7 using PyQt4. However, a solution in C++ might help.

I am using the QTableWidget to create a table. The table has limited space on my app, and it is not editable. Some of the cells in the table contain lots of data. Some of it is not visible to the user. I am looking up for a solution to show all of the cells' data, without making the table larger. I tried to set different flags for the cells, but it got me nowhere.

I thought about setting a scroll bar for the cells. Is it possible?

In order to make sure the table is not editable I used:

table.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)

I tried also to use -

table.item(row_index, col_index).setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)

on every cell in the table, but it got me the same results - no editing + only some data is visible.

IMAGE: I am looking up for an option to make sure all of the data at (2,2), that says "aaaaaa...", will be visible to the user. Maybe setting a vertical scroll bar inside that cell is the solution? If so, how to do it?


Solution

  • A possible solution is to set the resizeMode of the horizontal header in QHeaderView::ResizeToContents

    from PyQt4 import QtCore, QtGui
    
    if __name__ == "__main__":
        import sys
    
        app = QtGui.QApplication(sys.argv)
        w = QtGui.QTableWidget(4, 2)
        w.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        header = w.horizontalHeader()
        header.setResizeMode(QtGui.QHeaderView.ResizeToContents)
    
        for i in range(w.rowCount()):
            for j in range(w.columnCount()):
                it = QtGui.QTableWidgetItem("item ({}-{})".format(i, j))
                w.setItem(i, j, it)
        w.item(2, 1).setText("a" * 20)
        w.show()
        sys.exit(app.exec_())
    

    enter image description here