PDA

View Full Version : Qtableview margin/padding



Zoner
24th February 2021, 06:38
Hi Team

I have a small problem with tableview.
The problem I have is that the QT designer view and the actual application view differ (as shown in the in the pic below).
The table is somehow split in two columns, but when populating all data appears on the left side, including scroll bars.
After import of my data, these views stay the same. My data appears in the left field (with scroll adjuster) whilst the right field stays blank.

13598
Does anybody know what setting may need to change in QTdesigner (or in my code) to resolve this?


from PyQt5 import QtCore, QtWidgets, uic
from PyQt5.QtCore import Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import (qApp, QComboBox, QMainWindow, QApplication, QMessageBox, QFileDialog)

#define class
class Bathyui(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Bathyui, self).__init__(parent)
uic.loadUi('Open_File_UWN_Model.ui', self)

#connect to widget
self.table = QtWidgets.QTableView(self.tableView)

#populate with dataframe
self.table.setModel(file_reader)



Below the .ui code
13599

Im a littlebit lost how to solve this. I pretty much hit every setting in the QT Designer formatting menu.
Im using anaconda python with qt designer 5.11.1 included

d_stranz
24th February 2021, 17:32
For one thing, why are you not using layouts for such a complex form? It looks like you have put every widget in place with absolute size and positioning. This will almost certainly result in headaches if you resize the window or deploy it on a PC with different default magnification. Other than that bad problem, I don't see anything wrong with your UI file.

I am not a Python expert, but isn't line 13 creating a second QTableView using the current self.tableView as its parent? This is probably why you are seeing two tables. The left-hand table is this new one, which is getting filled from your model, while the original table created from the UI file is underneath it and in the size you specified in the UI file.

Get rid of line 13 and in line 16 use "self.tableView" to set the model.

Zoner
25th February 2021, 10:34
That was indeed the solution, and it looks like I need to go down the same road for some pyvistaqt graphs.
I reckon Im still struggling with the widgets.

Thanks for the tip with the layout form, I wondered why I couldnt read it right on my laptop, another one to add to my reading list.

thanks d_stranz
really appreciated!

d_stranz
25th February 2021, 18:27
another one to add to my reading list

Look at the many examples in the Qt distribution. Almost all of the desktop-oriented GUI apps with anything more than a simple UI use them.

My go-to layout for most dialogs and other form-like widget is QGridLayout. You can make particular use of the ability for a widget to span more than one grid cell and to use horizontal and vertical spacers to squish things down to preferred sizes. Be sure to set a layout for the entire widget so that when the widget is resized, everything in it gets laid out appropriately. Try to avoid setting absolute size limits for widgets unless you can't get them to behave by any other way.


another one to add to my reading list

Look at the many examples in the Qt distribution. Almost all of the desktop-oriented GUI apps with anything more than a simple UI use them.

My go-to layout for most dialogs and other form-like widget is QGridLayout. You can make particular use of the ability for a widget to span more than one grid cell and to use horizontal and vertical spacers to squish things down to preferred sizes. Be sure to set a layout for the entire widget so that when the widget is resized, everything in it gets laid out appropriately. Try to avoid setting absolute size limits for widgets unless you can't get them to behave by any other way.

Added after 1 36 minutes:

For fun I decided to edit your UI form to add some layouts, but for some reason the forum links to attach files aren't working for me. So, here's the text of the UI file itself. You can copy and paste it into an empty document and save it with a .ui extension.



<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>584</width>
<height>585</height>
</rect>
</property>
<property name="windowTitle">
<string>Select Bathy File</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="0">
<widget class="QLabel" name="Lat_label">
<property name="text">
<string>Latitude</string>
</property>
</widget>
</item>
<item row="3" column="7">
<widget class="QSpinBox" name="SkipRows"/>
</item>
<item row="6" column="0" colspan="8">
<widget class="QGraphicsView" name="graphicsView"/>
</item>
<item row="3" column="3">
<widget class="QComboBox" name="Combo_Y"/>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="Combo_X"/>
</item>
<item row="3" column="4">
<widget class="QLabel" name="Depth_Label">
<property name="text">
<string>Depth</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="8">
<widget class="QTableView" name="tableView">
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<property name="autoScrollMargin">
<number>10</number>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="showDropIndicator" stdset="0">
<bool>true</bool>
</property>
<property name="textElideMode">
<enum>Qt::ElideRight</enum>
</property>
<property name="showGrid">
<bool>true</bool>
</property>
<property name="cornerButtonEnabled">
<bool>true</bool>
</property>
<attribute name="horizontalHeaderDefaultSectionSize">
<number>70</number>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
</widget>
</item>
<item row="2" column="4">
<widget class="QRadioButton" name="Other_radioButton_4">
<property name="text">
<string>Other</string>
</property>
</widget>
</item>
<item row="3" column="6">
<widget class="QLabel" name="Skip_row_label">
<property name="text">
<string>Skip Rows</string>
</property>
<property name="buddy">
<cstring>SkipRows</cstring>
</property>
</widget>
</item>
<item row="7" column="0" colspan="8">
<widget class="QDialogButtonBox" name="OK_buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="3" column="5">
<widget class="QComboBox" name="Combo_Z"/>
</item>
<item row="2" column="1">
<widget class="QRadioButton" name="Comma_radioButton_2">
<property name="text">
<string>Comma</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QLabel" name="Long_Label">
<property name="text">
<string>Longitude</string>
</property>
</widget>
</item>
<item row="2" column="3">
<widget class="QRadioButton" name="SPACE_radioButton_3">
<property name="text">
<string>SPACE</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Delimiter</string>
</property>
</widget>
</item>
<item row="2" column="5">
<widget class="QLineEdit" name="Delimeter_text">
<property name="enabled">
<bool>false</bool>
</property>
<property name="maxLength">
<number>2</number>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QRadioButton" name="TAB_radioButton">
<property name="text">
<string>TAB</string>
</property>
</widget>
</item>
<item row="3" column="8">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="7">
<widget class="QPushButton" name="File_Name_Browse">
<property name="text">
<string>Browse</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="7">
<widget class="QLineEdit" name="File_Name_Box"/>
</item>
<item row="5" column="0" colspan="8">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="Dummy_button">
<property name="text">
<string>Dummy</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="Bathy_Import_Button">
<property name="text">
<string>Import</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="0" column="0" colspan="3">
<widget class="QLabel" name="label">
<property name="text">
<string>Select File</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<tabstops>
<tabstop>File_Name_Box</tabstop>
<tabstop>File_Name_Browse</tabstop>
<tabstop>Comma_radioButton_2</tabstop>
<tabstop>TAB_radioButton</tabstop>
<tabstop>SPACE_radioButton_3</tabstop>
<tabstop>Other_radioButton_4</tabstop>
<tabstop>Delimeter_text</tabstop>
<tabstop>Combo_X</tabstop>
<tabstop>Combo_Y</tabstop>
<tabstop>Combo_Z</tabstop>
<tabstop>SkipRows</tabstop>
<tabstop>tableView</tabstop>
<tabstop>Dummy_button</tabstop>
<tabstop>Bathy_Import_Button</tabstop>
<tabstop>graphicsView</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>

Zoner
26th February 2021, 04:42
I watched some vids yesterday, not sure why I didn't do it before. I guess I was code obsessed.
Thank you very much for the code example, I will have a good look over the weekend.
Again, really appreciate it. I chose quite a complicated python project to emerge myself, so need all the help (and time) I can get.

Zoner
1st March 2021, 02:02
This was very helpful. I was able to change my previous design, since I like doing things the hard way.
I do need to research your example a bit more, since I think I might have missed some expert features.
May I bother you for one more question.
Its similar to the tableview problem.
I want to connect my pyvistacode to graphicsView (or the likes) based on the .ui you altered
I can make it work, but I seem to overwrite the graphicsView with a new window.




#required imports

import pyvista as pv
import pyvistaqt
from pyvistaqt import *

#setting printer (creating double widget!)
self.plt = pyvistaqt.QtInteractor(self.graphicsView)

self.add_box()
def add_box(self):
box = pv.Box()
self.plt.add_mesh(box)
self.plt.reset_camera()
self.plt.fly_to_mouse_position()
self.plt.show()


I've read somewhere that the object name can already be considered as the plotter.
what I dont seem to grasp is:
1. am I using the right Widget
2. If so, how do I connect it

d_stranz
1st March 2021, 04:11
I'm afraid I don't know anything about pyvistaqt. But line 8 -is- creating a new pyvistaqt QtInteractor object with your graphicsView object as its parent and assigning the reference to self.plt. If you have already created a QtInteractor and assigned it to self.plt, then this line of code is wrong.

You seem to be a bit confused about how to retrieve an object reference from somewhere and creating a new one. Because your code is creating new instances, you are going to end up with overlapping widgets (and orphans, because if self.plt was already assigned elsewhere, when you reassign it you have no way to retrieve the previous reference). But because you also create them with parents, the widgets don't go away until the parents themselves are deleted. So you end up with overlapped widgets.


how do I connect it

I am not really sure what you mean by "connect". I think you mean to say, "retrieve the reference to it" because in Qt, connect() has a very specific meaning in terms of signal-slot "connections".

If you haven't seen it already, there is an example of using a QtInteractor here. (https://qtdocs.pyvista.org/usage.html)

Zoner
2nd March 2021, 00:43
Yeah I do struggle a bit. I was integrating with qinteractor, but I think it also has to do with widget compatibility. I can get the whole thing going in a frame widget, but not in a graphic widget. I'm rejigging all the code now to simplify it, then I can probably oversee everything a bit better.

Thanks for your reply

d_stranz
2nd March 2021, 15:54
Thanks for turning me on to pyvista and pyvistaqt. I use a little bit of VTK in my C++ applications and it is nice to know there is a way to use it from Python as well.

Zoner
3rd March 2021, 08:34
ha

Glad I could help for once! Their example projects are a big motivator, absolutely stunning.