I am trying to use PySide2 with the dialog buttons.
Each column on the dialogWindow has a chexbox, a button and a label.
Clicking on the button Set Text opens another dialog box to enter a text. I would like to get the input value of this dialog box (just enter this), and update the label next to the clicking button. In this case, changing the first label value Set value to just enter this, as I clicked on the first Set text button.
Finally, by clicking on the "Apply" button, I would like to get the values of the check boxes, and the labels (those at the end of each row with Set value).
Below are the dialog boxes and the code
from PySide2.QtWidgets import (
QApplication, QPushButton,
QWidget, QLabel, QVBoxLayout, QHBoxLayout, QLineEdit, QInputDialog, QCheckBox
)
import sys
from PySide2 import QtWidgets
class AnotherWindow(QWidget):
"""
This "window" is a QWidget. If it has no parent, it
will appear as a free-floating window as we want.
"""
def __init__(self):
super().__init__()
def init_input_dialog(self):
text, ok = QInputDialog.getText(self, 'Text Input Dialog', 'Enter something:')
if ok:
return str(text)
class Window(QWidget):
def __init__(self):
super().__init__()
v = QVBoxLayout()
objets = ['label1', 'label2']
for a in objets:
h = QHBoxLayout()
check_box = QCheckBox()
labels = QLabel( a )
button = QPushButton("Set text")
button.pressed.connect(
lambda val=a: self.show_new_window(val)
)
label_value = QLabel( "Set value", objectName=a )
h.addWidget(check_box)
h.addWidget(labels)
h.addWidget(button)
h.addWidget(label_value)
# Add the current objet entry to the main layout
v.addLayout(h)
# Add buttons: Ok and Cancel
btn_ok = QPushButton("Apply")
btn_ok.clicked.connect(self.get_dialog_box_values)
btn_cancel = QPushButton("Cancel")
btn_cancel.clicked.connect(self.close_dialog_box())
h = QHBoxLayout()
h.addWidget(btn_ok)
h.addWidget(btn_cancel)
v.addLayout(h)
# Set the layout
self.setLayout(v)
def show_new_window(self, n):
win = AnotherWindow()
val = win.init_input_dialog()
print(val)
# Replace the corresponding label with this value
# label_value.setText(str(val))
def get_dialog_box_values(self):
# Get state of checkboxes, and label values
check_box_values = []
label_values = []
print(check_box_values)
print(label_values)
def close_dialog_box(self):
# Close the dialog box
pass
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec_()
I am also open to any suggestions on how to achieve this in a better way.
e.g. if there is a better way to add the Apply and Cancel buttons to the main dialog box.

You need to references to all the widgets that you plan to interact with after they have been created and placed into a layout. In your example and description, at a minimum, you need to be able to interact with the labels at the end of each row, and the checkbox labels, so those are the widgets you should keep a reference to. You can do this by making them attributes of the Window class by giving them the
selfassignment. This is an important and one of the key concepts in the world of OOP.Once they have been assigned as attributes of the window class you can refer to them with the same
selfobject within any of the instance methods for that class.For example:
Used in a for loop