3

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?

Mika Li
  • 107
  • 1
  • 8
  • I suppose the problem you are referring to is "aaaaaaaaaaaaaaaa ..." in your image, what do you expect as a solution: a horizontal or vertical scrollbar or another solution? you point to a vertical scrollbar but I can not visually imagine what you want, but if I imagine a horizontal scrollbar – eyllanesc May 20 '19 at 20:47
  • Thank you for your comment, I changed the question accordingly. The answer is yes, I am referring to the cell with "aaaaaa..." on my table. I am looking for a way that I could see the entire content of the cell. I though about setting a vertical scroll bar, just for that cell, but I don't know how to do that. – Mika Li May 21 '19 at 05:06
  • Do you want "aaaaaaaaaaa ...." to show up in 2 or more rows? – eyllanesc May 21 '19 at 05:07
  • If you mean there is an option to make the cell larger, horizontally, so all the data could be shown then yes. I tried before to make the cell larger, but I wasn't able to make the content spread on 2 rows. – Mika Li May 21 '19 at 05:10
  • I think you should vertically, in my solution I made it grow horizontally but what you pointed out to me is that you do not want to change the width. – eyllanesc May 21 '19 at 05:11
  • Yes indeed. I do not want to change the width. I have limited space for the table, also the data on it is constantly changing. Space that will do for some data, won't do for other. – Mika Li May 21 '19 at 05:14
  • already undeleted – eyllanesc May 21 '19 at 05:38

1 Answers1

0

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241