How to resize the widget that hold a QTableView? - pyqt4

I have a QTableView inside a layout itself inside a Widget.
How I can set the Widget size after the call of tableView.resizeColumnsToContents() in order to have the same size of the table (and does not need a scrollbar )
Thanks

If I understand properly what you want is to adjust the horizontal size of the container widget to the size of the table contents (which is not necessarily the table size). The following method works for me with a small table of 20 rows and 3 columns:
ask the table model for the total number of columns (it should be returned by your implementation of the columnCount method)
ask the view for the width of every column using the columnWidth method and compute the total width of the columns
finally resize your container widget taking into account the total width of columns, the width of the table vertical header and the autoscroll margin
So the code would look like:
my_table_view.resizeColumnsToContents()
w = 0
for c in range(my_table_model.columnCount()):
w = w + my_table_view.columnWidth(c)
container_widget.resize(w + my_table_view.verticalHeader().width() + my_table_view.autoScrollMargin()*1.5, container_widget.size().height())
As I said, I'm assuming that you want to resize your widget horizontally.

Related

How to toggle QTableView column between stretch and resize

I'm using PyQt5
I have a QTableView, which has a number of columns with varying size, it can change dynamically.
I want the table to take the maximum space it can (I have the horizontal scroll always on).
I know that setting a column setSectionResizeMode to Stretch would force a column to expand to fill the allocated space for the table, the problem is when this space is smaller than what the table needs, it just trucks the content of the column.
I'm looking to a way to do ResizeToContents is the table is larger than the space it has, or Stretch when it doesn't.
Is there such an option?
I needed to have a specific column to always show it's content, so setting the column resize mode to ResizeToContents would give me that. But I also wanted to stretch the same column if the table didn't fill all the allocated area for it in the widget, for that we could use resize mode = Stretch.
The problem that we don't have an OR option between those two modes. Also Stretch can hide the column content if it's too long.
This is what I was able to do to make this possible, hope it would help anyone.
I inherited from QTableView and added an option to select which column to stretch (self._stretch) and capture the column resize events and force this column to the required width.
class myTableView(QTableView):
# ----------------------------------------------------
def __init__(stretch_column, parent=None):
super().__init__(parent=parent)
self._table_width = 0
self._stretch = stretch_column
self._stretch_col_min_width = 0
# Use standard model:
self.model = QStandardItemModel()
self.setModel(self.model)
# Re-calculate the column width when any column width change!
self.horizontalHeader().sectionResized.connect(self._column_resize)
# ----------------------------------------------------
# ----------------------------------------------------
# Re-calculate the column width when the table size change!
def resizeEvent(self, e):
super().resizeEvent(e)
self._table_width = e.size().width()
self._column_resize()
# ----------------------------------------------------
# ----------------------------------------------------
# Calculate what the minimum width for the stretch column:
# This can be called every time we resize, but it's not recommended
# I called it after I finished filling the table data.
def calculate_column_width(self):
self._stretch_col_min_width = 0
font_metrics = self.fontMetrics()
for row in range(0, self.rowCount()):
if self.isRowHidden(row):
continue
cell_data = self.model.data(self.model.index(row, self._stretch))
cell_width = font_metrics.width(cell_data + " ") # Give it some extra chars because of the cell boarder
self._stretch_col_min_width = max(cell_width, self._stretch_col_min_width)
# ----------------------------------------------------
# ---------------------------------------------------
def _column_resize(self):
# Calculate required minimum width of the column:
# FIXME - it can be called after filling the table data, and be removed from here
self.calculate_column_width()
# Calculate all column width - excluding the stretch column:
total_width = 0
for col in range(0, self.columnCount()):
if col != self._stretch:
total_width += self.columnWidth(col)
# Add vertical header width:
total_width += self._v_header.geometry().width()
# Calculate missing area to fill the parent view:
delta = self._table_width - total_width
# Expand the stretched column to fill the table view - or force it to be the minimum required width:
self.setColumnWidth(self._stretch, max(self._stretch_col_min_width, delta))
# ---------------------------------------------------

Min Height for GridLayout Row set to Auto

In a GridLayout, I have set a row to 'auto' because the content size is dynamic. In particular, I have a pulldown menu in the row. When it opens, the row correctly grows to the height of the pulldown list that appears. BUT, when the pulldown is closed, it is merely a button that should have the same height as the other rows. Unfortunately, the row height is a bit too small and causes the overall layout to look bad.
To fix this, I need to set the minimum height of the 'auto' row. Is this possible? Can I set a minimum height for a GridLayout row that is set to 'auto' ???
You can not set minimum height for a particular grid row, instead you can set minHeight (min-height with CSS) on the Button you are talking about / simply wrap the Button with a ContentView / StackLayout if you like to keep the Button smaller while giving it more space.

Spotfire Table Visualization column width

Is there a way to set Spotfire Table visualization column width either through Java or Python script. i can able to change the column width manually but whenever the value changes through property control it reset again. I need to set constant column width. Thanks in advance.
from Spotfire.Dxp.Application.Visuals import TablePlot
# Tab is the (Visualization) parameter passed to the script specify the table to work on
dataTable= Tab.As[TablePlot]().Data.DataTableReference
# Get a handle to the Table plot
table = Tab.As[TablePlot]()
# Get the ColumnCollection for the Table plot
columns = table.TableColumns
# Size all of the columns
for x in columns:
x.Width = 200
good question! you can add an IronPython script (I don't believe it's possible to do this using Javascript unless you are some kind of wizard, or otherwise hate yourself :) to do this pretty simply.
I'll put the examples all in one code snippet, but obviously you would only want to do one of these loops at a time. the snippet expects a parameter called viz whose value is the TablePlot visualization you wish to modify.
from Spotfire.Dxp.Application.Visuals import TablePlot
v = viz.As[TablePlot]()
# increase all column widths by 10 pixels
for col in v.Columns:
col.Width = col.Width + 10
# set all column widths to 100 pixels (the default value)
for col in v.Columns:
col.Width = 100
# set the width of a specific column to 50 pixels
for col in v.Columns:
if col.Name == "My Column":
col.Width = 50

SSRS: Excel-Rendering always created an empty Column

I have a strange problem with the SSRS-Excel Renderer:
As soon as I create a Table, it can be empty, with more than one Column, at the most right column, an empty Excel-Column shows up:
In the example, I just putted a plain Table into the RDLC, added the hardcoded values 1 to 4, and you can see, there is the 'C`Column pretty small, but without any values anywhere in it.
As per my understanding this is possible only if Report width is greater than tablix width,
Just Make sure you have same width for both Report & Tablix, Report width is in pt but tablix Size.width in inch.. so use any conversion and set same width for both.
Roughly 1 inch= 72 pt

Keep Chart Data Label from Wraping

I have a project where I have to put user generated data labels in a chart. These need to be on a series line as such:
![What I want format][1]
However in some instances the text wraps. How do I keep the text from wrapping in excel 10? see below
![enter image description here][2]
'-------- FORMAT DATA LABELS -------------------------------------
ActiveSheet.ChartObjects("OperBal2Takt").Activate
h = 0
For h = 2 To 7
With ActiveChart.SeriesCollection(h).Points(1)
.HasDataLabel = True
.DataLabel.Text = Cells(2018 + h, 8).Value
'.DataLabel.Width = msofit
End With
Next h
In Excel 2013, the size of your data label can be changed. In previous versions, the label cannot be resized.
In earlier Excel versions, sometimes I've stretched the chart to make it wider, but shrunk the plot area of the chart down to the original size, so the white space is wider on both sides of the chart. This works because the maximum size of a label is proportional to the chart width.
Other times I've replaced the data label with a text box, which can be flexibly resized.
use this code:
ChartObj.chart.seriescollection(1).datalabels.Format.TextFrame2.WordWrap = msoFalse
and also, but less usefull:
ChartObj.chart.seriescollection(1).datalabels.Format.TextFrame2.AutoSize = msoAutoSizeNone

Resources