0

I'm trying to display tabular data on a QTableWidget in PyQt5 as below. Default QTableWidget View

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]. First 3 columns of the QTableWidget

However, I am able to click on a cell within the QTableWidget and drag to the right to view the last 2 columns. Last 3 columns of the QTableWidget

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!

  • 1
    We cannot help you if you don't provide some code to start with, and from the look of your screenshots you're clearly using stylesheets. Please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with both the ui and python script (please, read the link: it *has* to be *both* minimal *and* reproducible). – musicamante Nov 24 '20 at 10:05
  • I've added a link to a GitHub repo containing the sample python code and UI file to reproduce the issue. – Pradeep Kumar N Nov 24 '20 at 12:28
  • Please avoid using external websites to share your code and embed it in the question instead. – musicamante Nov 24 '20 at 12:38

1 Answers1

1

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?.

musicamante
  • 41,230
  • 6
  • 33
  • 58