PySide6 Layout add space between items - python

I am using the QVBoxLayout from PySide6 to organize some items.
I now want to add some more space between the "Search" button and the "Modbus Data" group box.
They are all located inside the same QVBoxLaxout.
Here is the code which creates the displayed UI.
def setup_ui(self):
v_layout_base = QVBoxLayout(self)
h_layout_com_list = QHBoxLayout(self)
h_layout_com_list.addWidget(QLabel("Modbus COM"))
self.lw_com = QListWidget(self)
h_layout_com_list.addWidget(self.lw_com)
h_layout_search = QHBoxLayout(self)
self.button_search_com = QPushButton("Search")
h_layout_search.addWidget(self.button_search_com)
group_box = QGroupBox(QTranslator.tr("Modbus data"))
v_layout_modbus_data = QVBoxLayout(self)
h_layout_baudrate = QHBoxLayout(self)
h_layout_baudrate.addWidget(QLabel("Baudrate"))
self.cB_baudrate = QComboBox()
h_layout_baudrate.addWidget(self.cB_baudrate)
h_layout_bytesize = QHBoxLayout(self)
h_layout_bytesize.addWidget(QLabel("Bytesize"))
self.cB_bytesize = QComboBox()
h_layout_bytesize.addWidget(self.cB_bytesize)
h_layout_parity = QHBoxLayout(self)
h_layout_parity.addWidget(QLabel("Parity"))
self.cB_parity = QComboBox()
h_layout_parity.addWidget(self.cB_parity)
h_layout_stop_bit = QHBoxLayout(self)
h_layout_stop_bit.addWidget(QLabel("Stop bit"))
self.cB_stop_bit = QComboBox()
h_layout_stop_bit.addWidget(self.cB_stop_bit)
h_layout_connect = QHBoxLayout(self)
self.pb_connect = QPushButton("Connect")
self.pb_com_search = QPushButton("Disconnect")
h_layout_connect.addWidget(self.pb_connect)
h_layout_connect.addWidget(self.pb_com_search)
h_layout_com_label = QHBoxLayout(self)
h_layout_com_label.addWidget(QLabel("COM:"))
self.button_com_connect_label = QLabel("No COM selected")
h_layout_com_label.addWidget(self.button_com_connect_label)
h_layout_modbus_label = QHBoxLayout(self)
h_layout_modbus_label.addWidget(QLabel("Modbus:"))
self.button_modbus_connect_label = QLabel("Not connected to Modbus")
h_layout_modbus_label.addWidget(self.button_modbus_connect_label)
v_layout_base.addLayout(h_layout_com_list)
v_layout_base.addLayout(h_layout_search, 100)
v_layout_modbus_data.addLayout(h_layout_baudrate)
v_layout_modbus_data.addLayout(h_layout_bytesize)
v_layout_modbus_data.addLayout(h_layout_parity)
v_layout_modbus_data.addLayout(h_layout_stop_bit)
group_box.setLayout(v_layout_modbus_data)
v_layout_base.addWidget(group_box)
v_layout_base.addLayout(h_layout_connect)
v_layout_base.addLayout(h_layout_com_label)
v_layout_base.addLayout(h_layout_modbus_label)

Related

PyQt5 Different Grids on Different Tabs

I am trying to design a GUI interface that will have two Tabs both Tabs have there own Grid. However, I am getting this GUI Look;
Here is my code:
class Ui_MainWindow(QMainWindow):
def __init__(self):
super(Ui_MainWindow, self).__init__()
self.setupUi()
def generate_pstest_tab_elements(self,str1):
groupBox = QGroupBox(str1)
label1 = QLabel(self)
label1.setText('Serial No:')
line1 = QLineEdit(self)
line1.setText("XXXX")
label2 = QLabel(self)
label2.setText('Chip ID:')
line2 = QLineEdit(self)
line2.setText("XXXX")
rd_group = QButtonGroup()
rdbtn_FCB15 = QRadioButton("FCB-15")
rdbtn_DCB15 = QRadioButton("DCB-15")
rd_group.addButton(rdbtn_FCB15)
rd_group.addButton(rdbtn_FCB15)
layout1 = QFormLayout()
layout1.addRow(label1,line1)
layout1.addRow(label2,line2)
layout1.addRow(rdbtn_FCB15,rdbtn_DCB15)
grid1 = QGridLayout()
grid1.addWidget(label1,0,0)
grid1.addWidget(line1,0,1)
grid1.addWidget(rdbtn_FCB15,0,2)
grid1.addWidget(rdbtn_DCB15,0,3)
grid1.addWidget(label2,1,0)
grid1.addWidget(line2,1,1)
groupBox.setLayout(grid1)
return groupBox
def generate_pscalib_tab_elements(self,str1):
groupBox = QGroupBox(str1)
label1 = QLabel(self)
label1.setText('Serial No:')
line1 = QLineEdit(self)
line1.setText("XXXX")
label2 = QLabel(self)
label2.setText('Chip ID:')
line2 = QLineEdit(self)
line2.setText("XXXX")
label3 = QLabel(self)
label3.setText('QR Code:')
line3 = QLineEdit(self)
line3.setText("XXXX")
label4 = QLabel(self)
label4.setText('Current Range:')
line4 = QLineEdit(self)
line4.setText("1.0")
label5 = QLabel(self)
label5.setText("PS Offset")
line5 = QLineEdit(self)
line5.setText("0.0")
line5.setReadOnly(True)
label6 = QLabel()
label6.setText("PSGain")
line6 = QLineEdit(self)
line6.setText("1.0")
line6.setReadOnly(True)
grid2 = QGridLayout()
grid2.addWidget(label1,0,0)
grid2.addWidget(line1,0,1)
grid2.addWidget(label2,1,0)
grid2.addWidget(line2,1,1)
grid2.addWidget(label3,2,0)
grid2.addWidget(line3,2,1)
grid2.addWidget(label4,3,0)
grid2.addWidget(line4,3,1)
grid2.addWidget(label5,4,0)
grid2.addWidget(line5,4,1)
grid2.addWidget(label6,5,0)
grid2.addWidget(line6,5,1)
groupBox.setLayout(grid2)
return groupBox
def setupUi(self):
self.title = 'BiPolar Power Supply Testing'
self.setWindowTitle(self.title)
print (self.title)
self.left = 50
self.top = 50
self.width = 1000
self.height = 800
self.PSTestTabGrid = QGridLayout()
self.PSCalibTabGrid = QGridLayout()
#self.setGeometry(self.left, self.top, self.width, self.height)
#Create one main box that will hold all the tabs
self.mainbox = QFormLayout()
##################### Items for PS Functional Test Tab #################
self.PSTestTabGrid.addWidget(self.generate_pstest_tab_elements("PS1"), 0,0)
self.PSTestTabGrid.addWidget(self.generate_pstest_tab_elements("PS2"), 1,0)
self.PSTestTabGrid.addWidget(self.generate_pstest_tab_elements("PS3"), 2,0)
self.PSTestTabGrid.addWidget(self.generate_pstest_tab_elements("PS4"), 3,0)
self.PSTestTabGrid.addWidget(self.generate_pstest_tab_elements("PS5"), 4,0)
self.PSTestTabGrid.addWidget(self.generate_pstest_tab_elements("PS6"), 5,0)
self.PSTestTabGrid.addWidget(self.generate_pstest_tab_elements("PS7"), 6,0)
self.PSTestTabGrid.addWidget(self.generate_pstest_tab_elements("PS8"), 7,0)
self.PSFStart_btn = QPushButton("Start",self)
self.PSFStop_btn = QPushButton("Stop",self)
self.PSTestTabGrid.addWidget(self.PSFStart_btn,8,0)
self.PSTestTabGrid.addWidget(self.PSFStop_btn,9,0)
##################### Items for PS Calib Tab #################
self.PSCalibTabGrid.addWidget(self.generate_pscalib_tab_elements("PS"), 0,0)
self.PSCalibStart_btn = QPushButton("Calib Start",self)
self.PSCalibStop_btn = QPushButton("Calib Stop",self)
self.PSCalibTabGrid.addWidget(self.PSCalibStart_btn,1,0)
self.PSCalibTabGrid.addWidget(self.PSCalibStop_btn,2,0)
######################################################################
#Init Tab Screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
#Add Tab
self.tabs.addTab(self.tab1, "PS Tests")
self.tabs.addTab(self.tab2, "PS Calib")
#Set Layout for tab1, tab2
self.tab1.setLayout(self.PSTestTabGrid)
self.tab2.setLayout(self.PSCalibTabGrid)
#Add the Tabs widget to MainBox
self.mainbox.addWidget(self.tabs)
#self.mainbox.addRow(self.tab2HBox)
self.wid = QWidget(self)
self.setCentralWidget(self.wid)
#layout = QtGui.QVBoxLayout()
self.wid.setLayout(self.mainbox)
There are two functions that are being called from SetupUi().
First function is to generate layout for Tab1 by calling generate_pstest_tab_elements()
Second function is to generater layout for Tab2 by calling generate_pscalib_tab_elements()
The spacing between different widgets on Tab2 is very large. I tried calling setVerticalSpacing() to shrink the vertical spacing but it doesnt shrink either.
However If I commment out Tab1 items such that Tab1 doesnt contain any widget then Tab2 items look fine.
It seems like Qt is some how confused between the two independent Grids ?
Attached are the snapshots of the two tabs.
Tab1 image
Tab2 image
Each tab takes the size of the previous tab shown, and in the case of the first tab shown the available size will be used, if you want the smallest size to be taken then you must establish a stretch at the end using a QVBoxLayout.
Considering the above, the solution is:
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("BiPolar Power Supply Testing")
tab_widget = QtWidgets.QTabWidget()
self.setCentralWidget(tab_widget)
pstest_widget = QtWidgets.QWidget()
tab_widget.addTab(pstest_widget, "PS Tests")
pstest_vlay = QtWidgets.QVBoxLayout(pstest_widget)
for i in range(1, 9):
title = "PS{}".format(i)
group_box = MainWindow.create_pstest_element(title)
pstest_vlay.addWidget(group_box)
self.PSFStart_btn = QtWidgets.QPushButton("Start")
self.PSFStop_btn = QtWidgets.QPushButton("Stop")
pstest_vlay.addWidget(self.PSFStart_btn)
pstest_vlay.addWidget(self.PSFStop_btn)
pstest_vlay.addStretch()
pscalib_widget = QtWidgets.QWidget()
tab_widget.addTab(pscalib_widget, "PS Calib")
pscalib_vlay = QtWidgets.QVBoxLayout(pscalib_widget)
group_box = MainWindow.create_pscalib_element("PS")
self.PSCalibStart_btn = QtWidgets.QPushButton("Calib Start")
self.PSCalibStop_btn = QtWidgets.QPushButton("Calib Stop")
pscalib_vlay.addWidget(group_box)
pscalib_vlay.addWidget(self.PSCalibStart_btn)
pscalib_vlay.addWidget(self.PSCalibStop_btn)
pscalib_vlay.addStretch()
#staticmethod
def create_pstest_element(title):
group_box = QtWidgets.QGroupBox(title)
grid = QtWidgets.QGridLayout()
group_box.setLayout(grid)
serial_label = QtWidgets.QLabel("Serial No:")
serial_lineedit = QtWidgets.QLineEdit("XXXX")
chipid_label = QtWidgets.QLabel("Chip ID:")
chipid_lineedit = QtWidgets.QLineEdit("XXXX")
rd_group = QtWidgets.QButtonGroup()
rdbtn_FCB15 = QtWidgets.QRadioButton("FCB-15")
rdbtn_DCB15 = QtWidgets.QRadioButton("DCB-15")
grid.addWidget(serial_label, 0, 0)
grid.addWidget(serial_lineedit, 0, 1)
grid.addWidget(rdbtn_FCB15, 0, 2)
grid.addWidget(rdbtn_DCB15, 0, 3)
grid.addWidget(chipid_label, 1, 0)
grid.addWidget(chipid_lineedit, 1, 1)
return group_box
#staticmethod
def create_pscalib_element(title):
group_box = QtWidgets.QGroupBox(title)
flay = QtWidgets.QFormLayout()
group_box.setLayout(flay)
serial_lineedit = QtWidgets.QLineEdit("XXXX")
chipid_lineedit = QtWidgets.QLineEdit("XXXX")
qrcode_lineedit = QtWidgets.QLineEdit("XXXX")
range_lineedit = QtWidgets.QLineEdit("1.0")
offset_lineedit = QtWidgets.QLineEdit("0.0", readOnly=True)
gain_lineedit = QtWidgets.QLineEdit("1.0", readOnly=True)
flay.addRow("Serial No:", serial_lineedit)
flay.addRow("Chip ID:", chipid_lineedit)
flay.addRow("QR Code:", qrcode_lineedit)
flay.addRow("Current Range:", range_lineedit)
flay.addRow("PS Offset:", offset_lineedit)
flay.addRow("PS Gain:", gain_lineedit)
return group_box
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
If you want to continue using QGridLayout then the solution is to place it inside a QVBoxLayout:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("BiPolar Power Supply Testing")
tab_widget = QtWidgets.QTabWidget()
self.setCentralWidget(tab_widget)
pstest_widget = QtWidgets.QWidget()
tab_widget.addTab(pstest_widget, "PS Tests")
grid_lay_1 = QtWidgets.QGridLayout(pstest_widget)
pstest_vlay = QtWidgets.QVBoxLayout()
grid_lay_1.addLayout(pstest_vlay, 0, 0)
for i in range(1, 9):
title = "PS{}".format(i)
group_box = MainWindow.create_pstest_element(title)
pstest_vlay.addWidget(group_box)
self.PSFStart_btn = QtWidgets.QPushButton("Start")
self.PSFStop_btn = QtWidgets.QPushButton("Stop")
pstest_vlay.addWidget(self.PSFStart_btn)
pstest_vlay.addWidget(self.PSFStop_btn)
pstest_vlay.addStretch()
pscalib_widget = QtWidgets.QWidget()
tab_widget.addTab(pscalib_widget, "PS Calib")
grid_lay_2 = QtWidgets.QGridLayout(pscalib_widget)
pscalib_vlay = QtWidgets.QVBoxLayout()
grid_lay_2.addLayout(pscalib_vlay, 0, 0)
group_box = MainWindow.create_pscalib_element("PS")
self.PSCalibStart_btn = QtWidgets.QPushButton("Calib Start")
self.PSCalibStop_btn = QtWidgets.QPushButton("Calib Stop")
pscalib_vlay.addWidget(group_box)
pscalib_vlay.addWidget(self.PSCalibStart_btn)
pscalib_vlay.addWidget(self.PSCalibStop_btn)
pscalib_vlay.addStretch()
# ...

Unable to resize QTableWidget

I m trying to resize a table widget inside a QVBoxlayout which I further add as a row to a QFormlayout in pyqt
I m currently adding a QVboxlayout which contains a table widget inside it as a row in a Qformlayout.
And the main aim is to strecth the Table widget till the end of the application window that it acquires the left over space on the window
Using the below code -
class PGSearchDetails():
def __init__(self,parent=None):
self.widget_pgsd = QWidget()
self.layout_pgsd = QFormLayout()
self.layout_pgsd.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy(2|1))
self.PG_Id = QLineEdit()
#rx = QRegExp("^\\D+[!,#,#,$,%,\^,&,*,(,),:,\",{,},?,<,>,|,+,-,~,]")
#rx = QRegExp(" (?!(#,#,$|%|\^|&|*|(|)|:|\"|{|}|?|<|>|\||+|-|~|!))[\\w]+")
str_rx = QRegExp("^[A-Za-z*]{20}(?!##$%^&*():\"\{\}?<>\|+-~!_-)")
adhr_rx = QRegExp("[A-Z0-9]{12}(?!##$%^&*():\"\{\}?<>\|+-~!_)")
val = QRegExpValidator(str_rx)
val3 = QRegExpValidator(adhr_rx)
self.PG_Id.setValidator(val3)
self.LastName = QLineEdit()
self.LastName.setValidator(val)
self.MobNum = QLineEdit()
qint_val = QIntValidator()
qint_val.setTop(10)
self.MobNum.setValidator(qint_val)
self.layout_pgsd.addRow("First Name",self.PG_Id)
self.layout_pgsd.addRow("Last Name",self.LastName)
self.layout_pgsd.addRow("Mobile Number",self.MobNum)
update_layout_pgsd = QHBoxLayout()
layout_test,table = dbv.Search_View(self.getT)
#layout_test.setGeometry(QRect(200,200,50,50))
#table.setMaximumHeight(800)
#table.setGeometry(200,200,200,200)
#table.setGeometry(1,1,1000,600)
table.resize(1000,600)
update_btn_pgsd = QPushButton('Update')
reset_btn_pgsd = QPushButton('Reset')
update_layout_pgsd.addWidget(update_btn_pgsd)
update_layout_pgsd.addWidget(reset_btn_pgsd)
self.layout_pgsd.addRow(update_layout_pgsd)
##Adding the Table Widget to FormLayot
self.layout_pgsd.addRow(layout_test)
update_btn_pgsd.clicked.connect(partial(self.database,table,self.MobNum,
self.LastName))
#self.widget.setLayout(self.layout_pgsd)
def returnLayout(self):
return self.layout_pgsd
def returnWidget(self):
return self.widget_pgsd
def getT(self,linedit):
print("LE--->",linedit.text())
QtableWidget setup ---
def Search_View(self):
print("Inside Search_view")
central_widget = QWidget() # Create a central widget
db_layout = QVBoxLayout()
#col_count = len(self.pg_details.__dict__.keys())
col_count = 3
table = QTableWidget() # Create a table
#central_widget.setGeometry(200,200,150,150)
#table.maximumSize()
#db_layout.setMaximumSize(200)
db_layout.setGeometry(QRect(0,0,100,30))
db_layout.addWidget(table)
##Tried resizing the Table widget but nothing seems to works
table.resize(1000,600)
table.setGeometry(0,2,1000,600)
#central_widget.resize(central_widget.sizeHint())
#central_widget.setColumnWidth(1000,600)
#db_layout.addItem(update_layout)
central_widget.setLayout(db_layout)
print("Geometry H--->",table.geometry().height())
print("Geometry W--->",table.geometry().width())
print("Geometry X--->",table.geometry().x())
print("Geometry Y--->",table.geometry().y())
return central_widget,table
After the resize function, The table geometry changes to 1000,600 but on the screen it is not reflected. On the app screen it remains the same size everytime
Also find the entire code which contains the Tablayouts as well and Stacked widget for individual radio buttons
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.tab_widget = MainTabWindow()
self.setCentralWidget(self.tab_widget)
self.show()
class MainTabWindow(QTabWidget):
def __init__(self,parent=None):
super(MainTabWindow, self).__init__(parent)
self.init_ui()
def init_ui(self):
self.setWindowTitle('PyQt5 Tab Example')
self.tab1 = QWidget()
self.addTab(self.tab1,"Search")
self.PGSearchTab()
def PGSearchTab(self):
print("Search Tab First Tab")
self.central_layout = QVBoxLayout()
self.stack1 = QWidget()
self.stack2 = QWidget()
self.stack3 = QWidget()
self.stack_widget = QStackedWidget()
button_layout = QHBoxLayout()
radio_button_1 = QRadioButton("Search")
radio_button_2 = QRadioButton("Add")
radio_button_3 = QRadioButton("Update")
button_layout.addWidget(radio_button_1)
button_layout.addWidget(radio_button_2)
button_layout.addWidget(radio_button_3)
self.central_layout.addItem(button_layout)
self.stack_widget.addWidget(self.stack1)
self.stack_widget.addWidget(self.stack2)
self.stack_widget.addWidget(self.stack3)
self.central_layout.addWidget(self.stack_widget)
radio_button_1.toggled.connect(lambda :self.SelectButtonCheck(radio_button_1))
self.setTabText(0,"Search")
update_layout = QHBoxLayout()
update_layout.setAlignment(QtCore.Qt.AlignBottom)
update_btn = QPushButton('Update')
reset_btn = QPushButton('Reset')
update_layout.addWidget(update_btn)
update_layout.addWidget(reset_btn)
self.tab1.setLayout(self.central_layout)
def SelectButtonCheck(self,b):
if b.text() == "Search":
if b.isChecked():
print(b.text()+ "is selected")
self.obj_pgsd = pgsd.PGSearchDetails()
layout = self.obj_pgsd.returnLayout()
if self.stack1.layout() is None:
self.stack1.setLayout(layout)
self.stack_widget.setCurrentIndex(0)
def main():
application = QApplication(sys.argv)
main_window = MainTabWindow()
main_window.show()
sys.exit(application.exec_())
if __name__ == '__main__':
main()
I do not seem to understand what is I m missing here
Any pointers would be appreciated.
Also find the working code to execute the above layout setup.
#!/usr/local/bin/python3
import sys
from PyQt5.QtWidgets import (QApplication, QWidget,QMainWindow,QLineEdit,QAction,
QLabel,QPushButton,QVBoxLayout,
QTabWidget,QFormLayout,QHBoxLayout,
QRadioButton,QCheckBox,QTextEdit,
QListView,QDialogButtonBox,QSizePolicy,QCalendarWidget)
from PyQt5 import QtCore
import PyQt5.Qt
from PyQt5.Qt import *
from PyQt5.QtCore import pyqtSlot
from PyQt5 import QtSql
from functools import partial
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.tab_widget = MainTabWindow()
self.setCentralWidget(self.tab_widget)
self.show()
class MainTabWindow(QTabWidget):
def __init__(self,parent=None):
super(MainTabWindow, self).__init__(parent)
self.init_ui()
def init_ui(self):
self.setWindowTitle('PyQt5 Tab Example')
self.tab1 = QWidget()
self.addTab(self.tab1,"Search")
self.PGSearchTab()
def PGSearchTab(self):
print("Search Tab First Tab")
self.central_layout = QVBoxLayout()
self.stack1 = QWidget()
self.stack2 = QWidget()
self.stack3 = QWidget()
self.stack_widget = QStackedWidget()
button_layout = QHBoxLayout()
radio_button_1 = QRadioButton("Search")
radio_button_2 = QRadioButton("Add")
radio_button_3 = QRadioButton("Update")
button_layout.addWidget(radio_button_1)
button_layout.addWidget(radio_button_2)
button_layout.addWidget(radio_button_3)
self.central_layout.addItem(button_layout)
self.stack_widget.addWidget(self.stack1)
self.stack_widget.addWidget(self.stack2)
self.stack_widget.addWidget(self.stack3)
self.central_layout.addWidget(self.stack_widget)
radio_button_1.toggled.connect(lambda :self.SelectButtonCheck(radio_button_1))
self.setTabText(0,"Search")
update_layout = QHBoxLayout()
update_layout.setAlignment(QtCore.Qt.AlignBottom)
update_btn = QPushButton('Update')
reset_btn = QPushButton('Reset')
update_layout.addWidget(update_btn)
update_layout.addWidget(reset_btn)
self.tab1.setLayout(self.central_layout)
def SelectButtonCheck(self,b):
if b.text() == "Search":
if b.isChecked():
print(b.text()+ "is selected")
self.obj_pgsd = PGSearchDetails()
layout = self.obj_pgsd.returnLayout()
if self.stack1.layout() is None:
self.stack1.setLayout(layout)
self.stack_widget.setCurrentIndex(0)
class PGSearchDetails():
def __init__(self,parent=None):
self.widget_pgsd = QWidget()
self.layout_pgsd = QFormLayout()
self.layout_pgsd.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy(2|1))
self.PG_Id = QLineEdit()
#rx = QRegExp("^\\D+[!,#,#,$,%,\^,&,*,(,),:,\",{,},?,<,>,|,+,-,~,]")
#rx = QRegExp(" (?!(#,#,$|%|\^|&|*|(|)|:|\"|{|}|?|<|>|\||+|-|~|!))[\\w]+")
str_rx = QRegExp("^[A-Za-z*]{20}(?!##$%^&*():\"\{\}?<>\|+-~!_-)")
adhr_rx = QRegExp("[A-Z0-9]{12}(?!##$%^&*():\"\{\}?<>\|+-~!_)")
val = QRegExpValidator(str_rx)
val3 = QRegExpValidator(adhr_rx)
self.PG_Id.setValidator(val3)
self.LastName = QLineEdit()
self.LastName.setValidator(val)
self.MobNum = QLineEdit()
qint_val = QIntValidator()
qint_val.setTop(10)
self.MobNum.setValidator(qint_val)
self.layout_pgsd.addRow("First Name",self.PG_Id)
self.layout_pgsd.addRow("Last Name",self.LastName)
self.layout_pgsd.addRow("Mobile Number",self.MobNum)
update_layout_pgsd = QHBoxLayout()
layout_test,table = self.Search_View(self.getT)
#layout_test.setGeometry(QRect(200,200,50,50))
#table.setMaximumHeight(800)
#table.setGeometry(200,200,200,200)
#table.setGeometry(1,1,1000,600)
table.resize(1000,600)
update_btn_pgsd = QPushButton('Update')
reset_btn_pgsd = QPushButton('Reset')
update_layout_pgsd.addWidget(update_btn_pgsd)
update_layout_pgsd.addWidget(reset_btn_pgsd)
self.layout_pgsd.addRow(update_layout_pgsd)
##Adding the Table Widget to FormLayot
self.layout_pgsd.addRow(layout_test)
update_btn_pgsd.clicked.connect(partial(self.database,table,self.MobNum,
self.LastName))
#self.widget.setLayout(self.layout_pgsd)
def returnLayout(self):
return self.layout_pgsd
def returnWidget(self):
return self.widget_pgsd
def getT(self,linedit):
print("LE--->",linedit.text())
def Search_View(self,text):
print("Inside Search_view")
central_widget = QWidget() # Create a central widget
db_layout = QVBoxLayout()
#col_count = len(self.pg_details.__dict__.keys())
col_count = 3
table = QTableWidget() # Create a table
#central_widget.setGeometry(200,200,150,150)
#table.maximumSize()
#db_layout.setMaximumSize(200)
db_layout.setGeometry(QRect(0,0,100,30))
db_layout.addWidget(table)
##Tried resizing the Table widget but nothing seems to works
table.resize(1000,600)
table.setGeometry(0,2,1000,600)
#central_widget.resize(central_widget.sizeHint())
#central_widget.setColumnWidth(1000,600)
#db_layout.addItem(update_layout)
central_widget.setLayout(db_layout)
print("Geometry H--->",table.geometry().height())
print("Geometry W--->",table.geometry().width())
print("Geometry X--->",table.geometry().x())
print("Geometry Y--->",table.geometry().y())
return central_widget,table
def SqlExec(self,text):
db = QtSql.QSqlDatabase.addDatabase('QMYSQL')
db.setHostName('localhost')
db.setDatabaseName('Test')
db.setUserName('root')
db.open()
query = QtSql.QSqlQuery()
select = "select * from Test.PG_Details where PG_Id=?"# where PG_Id = 1"
query.prepare(select)
indexes = range(3)
print("TEXT----->",text)
query.addBindValue(text)
#query.exec_(select)
query.exec_()
print("Sizze----",query.size())
row_count = query.size()
db.record('PG_Details')
col_list = []
for i in range(db.record('PG_Details').count()):
print("FIELD----->",db.record('PG_Details').field(i).name())
col_list.append(db.record('PG_Details').field(i).name())
db.close()
return query,row_count,col_list
def database(self,table,text,text2):
text = text.text()
query_result,row_count,col_list = self.SqlExec(text)
i = 0
table.setColumnCount(3) #Set three columns
table.setRowCount(row_count)
table.setHorizontalHeaderLabels(col_list)
while query_result.next():
#print(query_result.value(i))
result_row = [query_result.value(index) for index in range(query_result.record().count())]
#print("RR--->",result_row)
for idx,val in enumerate(result_row):
#print("IDX----->",idx)
table.setItem(i, idx, QTableWidgetItem(val))
i = i + 1
def main():
application = QApplication(sys.argv)
main_window = MainTabWindow()
main_window.show()
sys.exit(application.exec_())
if __name__ == '__main__':
main()
The problem is that QFormLayout makes every time you add a widget using addRow () uses strech, the solution is to use a QVBoxLayout, and in that QVBoxLayout is to pass the QFormLayout and the layout_test.
class PGSearchDetails():
def __init__(self,parent=None):
self.widget_pgsd = QWidget()
self.layout = QVBoxLayout() # <---
self.layout_pgsd = QFormLayout()
self.layout.addLayout(self.layout_pgsd) # <---
self.layout_pgsd.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy(2|1))
...
self.layout_pgsd.addRow(update_layout_pgsd)
##Adding the Table Widget to FormLayot
# self.layout_pgsd.addRow(layout_test)
self.layout.addWidget(layout_test) # <---
update_btn_pgsd.clicked.connect(partial(self.database,table,self.MobNum,
self.LastName))
def returnLayout(self):
return self.layout # <---

Adding QScrollArea to QTabWidget

I'm trying to add a scroll area to a QTabWideget.
At the moment I've set it up with two different tabs and the scrollArea is added to the second tab.
When I run my program, items are added to the scrollArea and the scroll bar is visible(policy set to always show), but it's greyed out.
Code:
class MyTableWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QScrollArea()
self.tabs.setMaximumWidth(300)
self.tabs.setMaximumHeight(100)
# Add tabs
self.tabs.addTab(self.tab1,"Tab 1")
self.tabs.addTab(self.tab2,"Tab 2")
# Create first tab
# ...
# Create second tab
self.tab2.layout = QFormLayout(self)
self.tab2.setWidgetResizable(True)
self.tab2.setVerticalScrollBar(QScrollBar())
self.tab2.setVerticalScrollBarPolicy(2)
self.tab2.setFixedSize(100, 70)
self.t1 = QLabel('Test1', self)
self.t2 = QLabel('Test2', self)
self.t3 = QLabel('Test3', self)
self.t4 = QLabel('Test4', self)
self.t5 = QLabel('Test5', self)
self.t6 = QLabel('Test6', self)
self.tab2.layout.addRow(self.t1)
self.tab2.layout.addRow(self.t2)
self.tab2.layout.addRow(self.t3)
self.tab2.layout.addRow(self.t4)
self.tab2.layout.addRow(self.t5)
self.tab2.layout.addRow(self.t6)
self.tab2.setLayout(self.tab2.layout)
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
Code above turns out like this:
All squished together. I would like to be able to scroll and add more data without squishing whats there already.
Also, can I make the scroll area have the same background as seen in the picture below?
You do not have to replace the QScrollArea layout but add a new widget that has the QFormLayout as shown below.
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, \
QTabWidget, QScrollArea, QFormLayout, QLabel
class MyTableWidget(QWidget):
def __init__(self, parent=None):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QScrollArea()
self.tabs.addTab(self.tab1, 'Tab 1')
self.tabs.addTab(self.tab2, 'Tab 2')
content_widget = QWidget()
self.tab2.setWidget(content_widget)
flay = QFormLayout(content_widget)
self.tab2.setWidgetResizable(True)
self.t1 = QLabel('Test1')
self.t2 = QLabel('Test2')
self.t3 = QLabel('Test3')
self.t4 = QLabel('Test4')
self.t5 = QLabel('Test5')
self.t6 = QLabel('Test6')
flay.addRow(self.t1)
flay.addRow(self.t2)
flay.addRow(self.t3)
flay.addRow(self.t4)
flay.addRow(self.t5)
flay.addRow(self.t6)
self.layout.addWidget(self.tabs)
self.resize(300, 100)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = MyTableWidget()
w.show()
sys.exit(app.exec_())

PySide QlistWidget selected Item directory(path)

I have a problem; How can i get the path (directory) from "QListWidget" selected item.
When I 'am using os.path, this gives me Autodesk Maya Directory. But i need directory of my selected item from QListWidget item.
from PySide.QtGui import *
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
import os
import maya.cmds as cm
Import maya commands from maya as cm. This not include python source files.
class projectManagerMainWindow(MayaQWidgetDockableMixin, QWidget):
def __init__(self):
super(projectManagerMainWindow, self).__init__()
self.selectProject = QComboBox()
self.selectProject.setFixedHeight(30)
items = ["Main Projects","Assets"]
self.selectProject.addItems(items)
self.createNewProject = QPushButton()
self.createNewProject.setFixedSize(100,30)
icon = QPixmap('C:/Users/Faithcure/Documents/maya/2016/scripts/projectManager/new.png')
self.createNewProject.setText("New Project")
self.createNewProject.setIcon(icon)
self.dir_label = QLabel()
def select_driectory():
select_path = cm.fileDialog2(fileMode=3, okc = "Select")
self.sceneList.clear()
for path in select_path:
folders = os.listdir(path)
self.sceneList.addItems(folders)
self.dir_label.setText(path)
self.choose_directory = QPushButton()
self.choose_directory.setText("Directory")
self.choose_directory.setIcon(icon)
self.choose_directory.setFixedSize(100,30)
self.choose_directory.clicked.connect(select_driectory)
def new_project_window():
import newProject
reload(newProject)
ui = newProject.start()
ui.exec_()
def selected_item_path():
pass
self.createNewProject.clicked.connect(new_project_window)
This is Qlistwidget from selected directory items.
self.sceneLabel = QLabel()
self.sceneLabel.setText("Episodes: ")
self.sceneList = QListWidget()
self.sceneList.itemClicked.connect(selected_item_path)
And this is my second list widget (i want to list under the selected directories folder from scneneList Widget...
self.shotLabel = QLabel("Shots: ")
self.shotList = QListWidget()
self.topLayout = QHBoxLayout()
self.topLayout.addWidget(self.selectProject)
self.topLayout.addWidget(self.createNewProject)
self.topLayout.addWidget(self.choose_directory)
self.secondLayout = QVBoxLayout()
self.thirdLayout = QHBoxLayout()
self.thirdLayout.addWidget(self.sceneList)
self.thirdLayout.addWidget(self.shotList)
self.labels = QHBoxLayout()
self.labels.addWidget(self.sceneLabel)
self.labels.addWidget(self.shotLabel)
self.saveSceneVer = QPushButton()
self.saveSceneVer.setText("Save New Version")
self.saveSceneVer.setFixedHeight(30)
self.saveSceneVer.setStyleSheet("background-color:#00aff0")
self.openTrack = QPushButton()
self.openTrack.setText("Open Track")
self.openTrack.setFixedHeight(30)
self.openTrack.setStyleSheet("background-color:#ff5a80")
self.opButtons = QHBoxLayout()
self.opButtons.addWidget(self.saveSceneVer)
self.opButtons.addWidget(self.openTrack)
#Tab section for a lat of operations!
self.operation_tabs = QTabWidget()
tabWidget_Animation = QWidget()
optimize_for_animator_scene = QPushButton()
optimize_for_animator_scene.setText("I am an Animator")
create_render_camera = QPushButton()
create_render_camera.setText("Create Camera")
make_playblast = QPushButton()
make_playblast.setText("Playblast")
tabWidget_Animation_Layout = QGridLayout()
tabWidget_Animation.setLayout(tabWidget_Animation_Layout)
tabWidget_Animation_Layout.addWidget(optimize_for_animator_scene,0,0,0)
tabWidget_Animation_Layout.addWidget(create_render_camera, 0,1,0)
tabWidget_Animation_Layout.addWidget(make_playblast, 0,2,0)
tabWidget_render = QWidget()
tabWidget_td = QWidget()
self.operation_tabs.addTab(tabWidget_Animation,"Animation")
self.operation_tabs.addTab(tabWidget_render,"Render")
self.operation_tabs.addTab(tabWidget_td,"Compositing")
self.masterLayout = QVBoxLayout()
self.masterLayout.addLayout(self.topLayout)
self.masterLayout.addWidget(self.dir_label)
self.masterLayout.addLayout(self.secondLayout)
self.masterLayout.addLayout(self.labels)
self.masterLayout.addLayout(self.thirdLayout)
self.masterLayout.addLayout(self.opButtons)
self.masterLayout.addWidget(self.operation_tabs)
self.setLayout(self.masterLayout)
def start():
ui = projectManagerMainWindow()
ui.setWindowTitle("Project Manager")
ui.setFixedWidth(400)
ui.show(dockable=True, floating=False, area='right')
return ui
Here is script for Autodesk Maya.

PyQt - Tabbed dialog not displaying embedded widgets

I am trying to create a dialog with three tabs using PyQt. However, I am annoyed because although the dialog is displayed, the embedded widgets are not displayed!. I suppose this is a very simple problem with a correspondingly very simple solution, but I am struck! Can anyone give a hint? Thanks in advance!
Here is my code so far:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class TabbedDialog(QDialog):
def __init__(self, parent = None):
super(TabbedDialog, self).__init__(parent)
self.tabWidget = QTabWidget()
self.tabWidget.tab1 = QWidget()
self.tabWidget.tab2 = QWidget()
self.tabWidget.tab3 = QWidget()
self.tabWidget.addTab(self.tabWidget.tab1,"Tab 1")
self.tabWidget.addTab(self.tabWidget.tab2,"Tab 2")
self.tabWidget.addTab(self.tabWidget.tab3,"Tab 3")
self.tab1UI()
self.tab2UI()
self.tab3UI()
self.setWindowTitle("tab demo")
def tab1UI(self):
layout = QFormLayout()
layout.addRow("Name",QLineEdit())
layout.addRow("Address",QLineEdit())
self.tabWidget.setTabText(0,"Contact Details")
self.tabWidget.tab1.setLayout(layout)
def tab2UI(self):
layout = QFormLayout()
sex = QHBoxLayout()
sex.addWidget(QRadioButton("Male"))
sex.addWidget(QRadioButton("Female"))
layout.addRow(QLabel("Sex"),sex)
layout.addRow("Date of Birth",QLineEdit())
self.tabWidget.setTabText(1,"Personal Details")
self.tabWidget.tab2.setLayout(layout)
def tab3UI(self):
layout = QHBoxLayout()
layout.addWidget(QLabel("subjects"))
layout.addWidget(QCheckBox("Physics"))
layout.addWidget(QCheckBox("Maths"))
self.tabWidget.setTabText(2,"Education Details")
self.tabWidget.tab3.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
form = TabbedDialog()
retval = form.exec_()
here is my solution to the problem
On the init method, I declared a layout, then added the 'tabWidget' widget to that layout and set that layout as the layout of your QDialog.
def __init__(self, parent = None):
super(TabbedDialog, self).__init__(parent)
self.tabWidget = QTabWidget()
self.tabWidget.tab1 = QWidget()
self.tabWidget.tab2 = QWidget()
self.tabWidget.tab3 = QWidget()
self.tabWidget.addTab(self.tabWidget.tab1,"Tab 1")
self.tabWidget.addTab(self.tabWidget.tab2,"Tab 2")
self.tabWidget.addTab(self.tabWidget.tab3,"Tab 3")
self.tab1UI()
self.tab2UI()
self.tab3UI()
self.setWindowTitle("tab demo")
# Here is the addition to the code.
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.tabWidget)
self.setLayout(mainLayout)

Categories

Resources