简体   繁体   中英

Horizontal Scrollbar never appears on QTableWidget in PyQt5

I'm trying to display tabular data on a QTableWidget in PyQt5 as below. 默认 QTableWidget 视图

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]. QTableWidget 的前 3 列

However, I am able to click on a cell within the QTableWidget and drag to the right to view the last 2 columns. QTableWidget 的最后 3 列

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM