Manually set column widths for vision power table in version 8.1

I have a power table on a vision view. I have six columns and I’d like to be able to set the columns widths manually. I can’t find a way to do it.

From the Designer, enable preview mode, use your mouse to resize the column widths, then save the project when you’re happy. Modifications made to components in preview mode are serialized (saved) with the rest of the window, and will be carried over to the client when launched.

Due to how Vision layout works, you might need to go back to the Designer and make some adjustments.

Or if you want to handle it dynamically through scripting here’s a script that I use in a custom method of my power table as a starting point. It can work pretty well depending on how much data your power table has crammed into it. The function gets called from the propertyChange event whenever the data property is updated.

In my case I am setting all boolean columns to a width of 5 so they won’t take up too much space. Also you can put column names in the filter_columns list for any columns you don’t want the script to resize.

	ds = self.viewDataset
	ds_attributes = self.columnAttributesData
	headers = system.dataset.getColumnHeaders(self.viewDataset)
	filter_columns = []  # A list of columns (by name) to remain untouched by this script 
	
	# No point in continuing if the table is empty
	if len(headers) > 0:
	
		# Get list of boolean column indexes. We will make these a fixed smaller size.
		bool_names = [ds_attributes.getValueAt(row, "Name") for row in range(ds_attributes.rowCount) if ds_attributes.getValueAt(row, "treatAsBoolean")]
		bool_col_indexes = [headers.index(name) for name in bool_names if name in headers]
		
		# Get the indexes of the columns that we don't want to resize
		ignore_cols = [headers.index(name) for name in filter_columns if name in headers]
		
		# Hard coded sizing for 2 of the columns
		self.setColumnWidth(0, 30)
		self.setColumnWidth(2, 55)
		ignore_cols.append(0)
		ignore_cols.append(2)
		
		# Get the Graphics object being used by the table. We can use the font render context to measure the size of the text
		g = self.getTable().getGraphics()
		if g is not None:
			lengths = {}
			frc = g.getFontRenderContext()
			for row in range(ds.rowCount):
				for col in range(ds.columnCount):
					if col in ignore_cols:
						continue
					elif col in bool_col_indexes:
						lengths[col] = 5
					else:
						# Check if the text in this cell is wider than the stored column width thus far
						boundPart = self.font.getStringBounds(str(ds.getValueAt(row, col)), frc)
						if int(boundPart.getWidth()) > lengths.get(col, 0):
							lengths[col] = int(boundPart.getWidth())
						
			# Apply the column sizing
			for col in lengths:
				self.setColumnWidth(col, lengths[col])
  • edited to fix a minor issue in the script
1 Like

Nice! I am ok with the simpler solution but good to know for the future if I need it to be more dynamic!