I'm trying to display tabular data on a QTableWidget in PyQt5 as below.
I've set the columns to QtWidgets.QHeaderView.ResizeToContents
.
But after the data is populated into the QTableWidget, the Horizontal Scrollbar does not appear although the 5 columns are wider than the viewport [Refer below image].
However, I am able to click on a cell within the QTableWidget and drag to the right to view the last 2 columns.
I've also disabled the 'stretchLastHeaderSection' property as mentioned in a similar post regarding QTreeView, but the Horizontal Scrollbar still does not appear.
A sample python code and UI file to reproduce the issue can be found in the link below. https://github.com/PradeepKumarN93/StackOverflowQuestions
Could someone please tell me how I can get the Horizontal Scrollbar when the contents are larger than the viewport?
Note: I'm not generating code from the ui file, but loading it directly using the following command.
uic.loadUi(join(dirname(realpath(__file__)), "UI", "DMTInputDownloader.ui"), self)
Thanks!
When styling complex widgets like QScrollBar you must be very careful about their properties, as in these cases changing stylesheets are responsible of drawing the whole widget, not just changing some parameters.
When setting all properties with pseudo selectors for sub-controls, you should consider the sizes correctly; in your case, you have set the width for the horizontal scrollbar, but you need to set its height instead:
QScrollBar:horizontal {
border: none;
background: #F0F0F0;
height: 10px;
margin: 0px 0px 0px 0px;
}
Also note that you should set a layout for the central widget too, otherwise the contents won't be properly resized: if the window is too big, the widgets will still be on the top left of the window leaving a lot of unused space, and if the window is too small some controls would be partially (if not completely) hidden. See How to make a Qt Widget grow with the window size?.