I used the code below to dynamically create a group of radio buttons:
self.wPaymantType.qgbSomeSelectionGroup = QtGui.QGroupBox()
vbox = QtGui.QVBoxLayout()
for row in listOfChoices:
radio = QtGui.QRadioButton(row)
if bIsFirst:
radio.setChecked(True)
bIsFirst = False
if len(row.name) > nMaxLen:
nMaxLen = len(row.name)
vbox.addWidget(radio)
self.wPaymantType.qgbSomeSelectionGroup.setLayout(vbox)
How can I iterate through all radio buttons to find out which one is checked?
I tried something like this, but I didn't get anything good from it:
qvbl = self.qgbSomeSelectionGroup.children()[0]
for i in range(0, qvbl.count()):
child = qvbl.itemAt(i)
radio = QtGui.QRadioButton(child.widget())
if radio != None:
if radio.isChecked():
print "radio button num " + str(i) + " is checked"
Your code is not minimal and self-contained, so it's really hard to help you -- but I've anyway gone through the effort of building a near-minimal self-contained approximation of what you're trying to do and which does seem to work correctly -- here comes...:
from PyQt4 import QtGui
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.dowid()
self.setCentralWidget(self.thewid)
def dowid(self):
self.thewid = QtGui.QGroupBox()
vbox = QtGui.QVBoxLayout()
self.radiobuttons = []
listOfChoices = 'one two three'.split()
for i, row in enumerate(listOfChoices):
radio = QtGui.QRadioButton(row)
if i == 0:
radio.setChecked(True)
self.radiobuttons.append(radio)
vbox.addWidget(radio)
self.thewid.setLayout(vbox)
def examine(self):
for i, radio in enumerate(self.radiobuttons):
if radio.isChecked():
print "radio button num " + str(i) + " is checked"
else:
print "radio button num " + str(i) + " is NOT checked"
if __name__ == '__main__':
app = QtGui.QApplication([])
mainWin = MainWindow()
mainWin.show()
rc = app.exec_()
mainWin.examine()
This seems to do what you want. Key change is to keep the actual Python widget objects around rather than trying to restore them from the layout vbox -- that attempt seems to not be working as intended, at least as regards correct access to the crucial detail about whether a given radio button is checked or not, which is of course the heart of your Q.
I believe the reason why it's not working is your
radio = QtGui.QRadioButton(child.widget())
call at the code where you're checking if your checkbox is checked. I think what you're trying to do is typecast the child object to QtGui.QRadioButton and it doesn't work in this case. Instead you should be creating a new widget. Try changing it to smth. like this:
qvbl = self.qgbSomeSelectionGroup.layout()
for i in range(0, qvbl.count()):
widget = qvbl.itemAt(i).widget()
if (widget!=0) and (type(widget) is QtGui.QRadioButton):
if widget.isChecked():
print "radio button num " + str(i) + " is checked"
the code above should be iterating through child objects of the layout object, check their type and print "radio button..." in case it's radio buttong and it's checked
hope this helps, regards
I guess a better way to identify which button is checked is to use a QButtonGroup, as it provides a container to organize groups of button widgets. It is not a visual object, so it doesn't substitute the layout for visually arranging your radio buttons, but it does allow you to make them mutually exclusive and associate an integer "id" with them, letting you know which one is checked without the need to iterate through all the widgets present in the layout.
If you decide to use it, your code should turn into something like this:
self.wPaymantType.qgbSomeSelectionGroup = QtGui.QGroupBox()
vbox = QtGui.QVBoxLayout()
radioGroup = QtGui.QButtonGroup()
radioGroup.setExclusive(True)
for i,row in enumerate(listOfChoices):
radio = QtGui.QRadioButton(row)
radioGroup.addButton(radio, i)
if bIsFirst:
radio.setChecked(True)
bIsFirst = False
if len(row.name) > nMaxLen:
nMaxLen = len(row.name)
vbox.addWidget(radio)
self.wPaymantType.qgbSomeSelectionGroup.setLayout(vbox)
To identify the checked button, you can use QButtonGroup's checkedId method:
buttonId = radioGroup.checkedId()
or if you want to retrive the button object itself you can use the checkedButton method:
button = radioGroup.checkedButton()
Related
I am trying to have a series of QTableView created at runtime and added to newly created pages of a multipage QTabWidget.
All seems to go fine, but the QTableView don't show up.
The QTabWidget gets zeroed (reset to no pages) and refurbished (...) flawlessly (at least it looks like so) depending on the selection of a combobox (and the dictionaries therein related).
I am also using a delegate callback to include a column of checkboxes to the QTableView (thanks to https://stackoverflow.com/a/50314085/7710452), which works fine stand alone.
Here is the code.
Main Window
EDIT
as recommended by eyllanesc, here is the standalone module (jump to the end of the post for details on the part I think is problematic):
"""
qt5 template
"""
import os
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from PyQt5 import uic
from configparser import ConfigParser, ExtendedInterpolation
from lib.SearchControllers import findGuis, get_controller_dict, show_critical, show_exception
import resources.resources
from lib.CheckBoxesDelegate import CheckBoxDelegate
myForm_2, baseClass = uic.loadUiType('./forms/setup.ui')
class MainWindow(baseClass):
def __init__(self, config_obj: ConfigParser,
config_name: str,
proj_name: str,
*args,
**kwargs):
super().__init__(*args, **kwargs)
self.ui = myForm_2()
self.ui.setupUi(self)
# your code begins here
self.setWindowTitle(proj_name + " Setup")
self.ui.logo_lbl.setPixmap(qtg.QPixmap(':/logo_Small.png'))
self.config_obj = config_obj
self.config_name = config_name
self.proj_filename = proj_name
self.proj_config = ConfigParser(interpolation=ExtendedInterpolation())
self.proj_config.read(proj_name)
self.guis_dict = {}
self.components = {}
self.cdp_signals = {}
self.root_path = self.config_obj['active']['controllers']
self.tableViews = []
self.tabs = []
self.iniControllersBox()
self.setActSignals()
self.load_bulk()
self.set_signals_table()
self.update_CurController_lbl()
self.update_ControllersTab() # here is where the action gets hot
# your code ends here
self.show() # here crashes if I passed the new tab to the instance of
# QTabView. otherwise it shows empty tabs
#########################################################
def load_bulk(self):
# get the list of running components into a dictionary
for i in self.list_controllers:
i_path = os.path.join(self.root_path, i)
print(i)
self.components[i] = get_controller_dict(i_path,
self.config_obj,
'Application.xml',
'Subcomponents/Subcomponent',
'Name',
'src')
for j in self.components[i]:
print(j)
signals_key = (i , j)
tgt = os.path.join(self.root_path, self.components[i][j])
self.cdp_signals[signals_key] = get_controller_dict(i_path,
self.config_obj,
self.components[i][j],
'Signals/Signal',
'Name',
'Type',
'Routing')
def set_signals_table(self):
self.ui.MonitoredDevicesTable.setHorizontalHeaderItem(0, qtw.QTableWidgetItem('GUI caption'))
self.ui.MonitoredDevicesTable.setHorizontalHeaderItem(1, qtw.QTableWidgetItem('Monitored Signal'))
def setActSignals(self):
self.ui.controllersBox.currentIndexChanged.connect(self.update_guis_list)
self.ui.controllersBox.currentIndexChanged.connect(self.update_CurController_lbl)
self.ui.controllersBox.currentIndexChanged.connect(self.update_ControllersTab)
def update_ControllersTab(self):
self.ui.componentsTab.clear() # this is the QTabWidget
self.tabs = []
self.tableViews = []
curr_controller = self.ui.controllersBox.currentText()
for i in self.components[curr_controller]:
if len(self.cdp_signals[curr_controller, i]) == 0:
continue
self.tabs.append(qtw.QWidget())
tabs_index = len(self.tabs) - 1
header_labels = ['', 'Signal', 'Type', 'Routing', 'Input']
model = qtg.QStandardItemModel(len(self.cdp_signals[curr_controller, i]), 5)
model.setHorizontalHeaderLabels(header_labels)
# in the next line I try to create a new QTableView passing
# the last tab as parameter, in the attempt to embed the QTableView
# into the QWidget Tab
self.tableViews.append(qtw.QTableView(self.tabs[tabs_index]))
tbw_Index = len(self.tableViews) - 1
self.tableViews[tbw_Index].setModel(model)
delegate = CheckBoxDelegate(None)
self.tableViews[tbw_Index].setItemDelegateForColumn(0, delegate)
rowCount = 0
for row in self.cdp_signals[curr_controller, i]:
for col in range(len(self.cdp_signals[curr_controller, i][row])):
index = model.index(rowCount, col, qtc.QModelIndex())
model.setData(index, self.cdp_signals[curr_controller, i][row][col])
try:
self.ui.componentsTab.addTab(self.tabs[tabs_index], i) # no problems, some controllers ask up to
except Exception as ex:
print(ex)
def update_CurController_lbl(self):
self.ui.active_controller_lbl.setText(self.ui.controllersBox.currentText())
def iniControllersBox(self):
self.list_controllers = [os.path.basename(f.path) for f in os.scandir(self.root_path) if f.is_dir() and str(
f.path).upper().endswith('NC')]
self.ui.controllersBox.addItems(self.list_controllers)
for i in range(self.ui.controllersBox.count()):
self.ui.controllersBox.setCurrentIndex(i)
newKey = self.ui.controllersBox.currentText()
cur_cntrlr = os.path.join(self.config_obj['active']['controllers'], self.ui.controllersBox.currentText())
self.guis_dict[newKey] = findGuis(cur_cntrlr, self.config_obj)
self.ui.controllersBox.setCurrentIndex(0)
self.update_guis_list()
def update_guis_list(self, index=0):
self.ui.GuisListBox.clear()
self.ui.GuisListBox.addItems(self.guis_dict[self.ui.controllersBox.currentText()])
if __name__ == '__main__':
config = ConfigParser()
config.read('./config.ini')
app = qtw.QApplication([sys.argv])
w = MainWindow(config, './config.ini',
'./test_setup_1.proj')
sys.exit(app.exec_())
and here the external to add the checkboxes column:
class CheckBoxDelegate(QtWidgets.QItemDelegate):
"""
A delegate that places a fully functioning QCheckBox cell of the column to which it's applied.
"""
def __init__(self, parent):
QtWidgets.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
"""
Important, otherwise an editor is created if the user clicks in this cell.
"""
return None
def paint(self, painter, option, index):
"""
Paint a checkbox without the label.
"""
self.drawCheck(painter, option, option.rect, QtCore.Qt.Unchecked if int(index.data()) == 0 else QtCore.Qt.Checked)
def editorEvent(self, event, model, option, index):
'''
Change the data in the model and the state of the checkbox
if the user presses the left mousebutton and this cell is editable. Otherwise do nothing.
'''
if not int(index.flags() & QtCore.Qt.ItemIsEditable) > 0:
return False
if event.type() == QtCore.QEvent.MouseButtonRelease and event.button() == QtCore.Qt.LeftButton:
# Change the checkbox-state
self.setModelData(None, model, index)
return True
if event.type() == QtCore.QEvent.MouseButtonPress or event.type() == QtCore.QEvent.MouseMove:
return False
return False
def setModelData (self, editor, model, index):
'''
The user wanted to change the old state in the opposite.
'''
model.setData(index, 1 if int(index.data()) == 0 else 0, QtCore.Qt.EditRole)
The 1st picture shows the layout in QTDesigner, the 2nd the result (emtpy tabs) when avoiding the crashing.
the QTabWidget has no problems in zeroing, or scale up, back to as many tab as I need, it's just that I have no clue on how to show the QTabview. My approach was to try to embed the QTabView in the tabpage passing it as parameter to the line creating the new QTabView.
Since I am using rather convoluted dictionaries, calling an XML parser to fill them up, not to mention the config files, I know even this version of my script is hardly reproduceable/runnable.
If someone had the patience of focusing on the update_ControllersTab method though, and tell me what I am doing wrong handling the QWidgets, it'd be great.
Again the basic idea is to clear the QTabWidget any time the user selects a different controller (combo box on the left):
self.ui.componentsTab.clear() # this is the QTabWidget
self.tabs = [] # list to hold QTabView QWidgets (pages) throughout the scope
self.tableViews = [] # list to hold QTabView(s) thorughout the scope
count how many tabs (pages) and hence embedded TabViews I need with the new controllers selected.
and then for each tab needed:
create a new tab (page)
self.tabs.append(qtw.QWidget())
tabs_index = len(self.tabs) - 1
create a new QTabView using a model:
header_labels = ['', 'Signal', 'Type', 'Routing', 'Input']
model = qtg.QStandardItemModel(len(self.cdp_signals[curr_controller, i]), 5)
model.setHorizontalHeaderLabels(header_labels)
self.tableViews.append(qtw.QTableView(self.tabs[tabs_index]))
tbw_Index = len(self.tableViews) - 1
self.tableViews[tbw_Index].setModel(model)
populate the TableView with data, and then finally add the tab widget (with the suppposedly embedded QTableView to the QTabWidget (the i argument is a string from my dbases Names:
self.ui.componentsTab.addTab(self.tabs[tabs_index], i)
This method is called also by the __init__ to initialize and apparently all goes error free, until the last 'init' statement:
`self.show()`
at which point the app crashes with:
Process finished with exit code 1073741845
on the other hand, if here instead of trying to embed the QTableView:
self.tableViews.append(qtw.QTableView(self.tabs[tabs_index]))
I omit the parameter, that is:
self.tableViews.append(qtw.QTableView())
the app doesn't crash anymore, but of course no QtableViews are shown, only empty tabpages:
As stupid as this may sound the problem is in... the delegate class that creates the checkboxes in the first column (see https://stackoverflow.com/a/50314085/7710452)
I commented out those two lines:
delegate = CheckBoxDelegate(None)
self.tableViews[tbw_Index].setItemDelegateForColumn(0, delegate)
and... bingo!
the CheckBoxDelegate works fine in the example shown in the post (a single QTableView form). I also tinkered around adding columns and rows, and moving the checkbox column back and forth with no problems. In that standalone. But as soon as I add the class and set the delegate, i am back at square zero, app crashing with:
Process finished with exit code 1073741845
so I am left with this problem now. Thnx to whomever read this.
Problem solved, see comment to post above.
I'm using a QLineEdit widget to enter email addresses and I set up a QRegExpValidor to validate the entry.
The validator is working as far as preventing the input of characters not allowed in the QRegExp, but funny enough, it allows to enter intermediate input, either by pushing the enter key or by firing the "editingfinished" signal.
I verified the return of the validator which is correct (Intermediate or Acceptable).
Checking the PyQt5 documentation it seams that an intermediate state of the validator does not prevent the focus to change to another widget. Furthermore, it stops the firing of the editingFinished and returnedPressed signals so the user may enter wrong address as far as it marches partially the RegExp.
Ref: https://doc.qt.io/qt-5/qlineedit.html#acceptableInput-prop
I was able to solve my needs by removing the validator from the QLineEdit widget, and placing a method "checkValidator" linked to the cursorPositionChanged of the QLineEdit widget discarting the last character entered when nonacceptable, and setting the focus back to the QLineEdit when it validats intermmediate. It works just fine but when the the focus was reset, the others signals on other widgets were fired one at a time. Momentarily, I handle this by checking if the sender has focus at the start of the methods (see lookForFile)
Even though I could handle the issue, I appreciate very much anybody explaining me the proper way to use the RegExpValidator and or why resetting the focus fires the other signals out of the blue.
def setUI(self):
self.setWindowTitle("EMail Settings")
self.setModal(True)
rx = QRegExp(r"[a-z0-9_%]+#[a-z0-9%_]+\.[a-z0-9%_]{3,3}")
lblAddress = QLabel("EMail Address")
self.lineAddress = QLineEdit(self)
self.mailValidator = QRegExpValidator(rx, self.lineAddress)
#self.lineAddress.setValidator(self.mailValidator)
self.lineAddress.cursorPositionChanged.connect(self.checkValidator)
self.lineAddress.returnPressed.connect(self.checkValidator)
lblPassword = QLabel("Password")
self.linePwd = QLineEdit()
self.linePwd.setEchoMode(QLineEdit.PasswordEchoOnEdit)
lblOauth2 = QLabel("Oauth2 Token")
self.lineOauth = QLineEdit()
pushOauth = QPushButton("...")
pushOauth.setObjectName("token")
pushOauth.clicked.connect(self.lookForFile)
pushOauth.setFixedWidth(30)
#pyqtSlot()
def checkValidator(self):
self.lineAddress.blockSignals(True)
v = self.mailValidator.validate(self.lineAddress.text(), len(self.lineAddress.text()))
if v[0] == 0:
self.lineAddress.setText(self.lineAddress.text()[:-1])
elif v[0] == 1:
self.lineAddress.setFocus()
elif v[0] == 2:
pass
print("validates", v)
self.lineAddress.blockSignals(False)
#pyqtSlot()
def lookForFile(self):
try:
if not self.sender().hasFocus():
return
baseDir = "C"
obj = self.sender()
if obj.objectName() == "Draft":
capt = "Email Draft"
baseDir = os.getcwd() + "\\draft"
fileType = "Polo Management Email (*.pad)"
dialog = QFileDialog(self, directory=os.getcwd())
dialog.setFileMode(QFileDialog.Directory)
res = dialog.getExistingDirectory()
elif obj.objectName() == "token":
capt = "Gmail Outh2 token File"
fileType = "Gmail token Files (*.json)"
baseDir = self.lineOauth.text()
res = QFileDialog.getOpenFileName(self, caption=capt, directory=baseDir, filter=fileType)[0]
fileName = res
if obj.objectName() == "Draft":
self.lineDraft.setText(fileName)
elif obj.objectName() == "tokenFile":
self.lineOauth.setText(fileName)
except Exception as err:
print("settings: lookForFile", err.args)
Hope to answer #eyllanesc and Qmusicmante request with this minimal reproducible example. I change the regex for a simple one allowing for a string of lower case a-z follow by a dot and three more lowercase characters.
What I intend is the validator not allowing the user to enter a wrong input. The example allows for "xxxzb.ods" but also allows "xxxzb" or "xxxzb.o" for instance.
In short, not allowing the user to enter a wrong input.
This is my minimal reproducible example:
class CheckValidator(QDialog):
def __init__(self, parent=None):
super().__init__()
self.parent = parent
self.setUI()
def setUI(self):
self.setWindowTitle("EMail Settings")
self.setModal(True)
rx = QRegExp(r"[a-z]+\.[a-z]{3}")
lblAddress = QLabel("Check Line")
self.lineAddress = QLineEdit()
self.mailValidator = QRegExpValidator(rx, self.lineAddress)
self.lineAddress.setValidator(self.mailValidator)
self.lineAddress.cursorPositionChanged[int, int].connect(lambda
oldPos, newPos: self.printValidator(newPos))
lblCheck = QLabel("Check")
lineCheck = QLineEdit()
formLayout = QFormLayout()
formLayout.addRow(lblAddress, self.lineAddress)
formLayout.addRow(lblCheck, lineCheck)
self.setLayout(formLayout)
#pyqtSlot(int)
def printValidator(self, pos):
print(self.mailValidator.validate(self.lineAddress.text(), pos))
if __name__ == '__main__':
app = QApplication(sys.argv)
tst = CheckValidator()
tst.show()
app.exec()
I found a solution and I post it here for such a case it may help somebody else.
First I remove the QRegExpValidator from the QLineEdit widget. The reason being is that QLineEdit only fires editingFinished (and we'll need it ) only when QRegExpValidator returns QValidator.Acceptable while the validator is present.
Then we set a method fired by the 'cursorPositionchanged' signal of QlineEdit widget. On this method, using the QRegExpValidator we determine if the last character entered is a valid one. If not we remove it.
Finally, I set a method fired by the 'editingFinished' signal using the RegEx exactMatch function to determine if the entry is valid. If it is not, we give the user the option to clear the entry or to return to the widget to continue entering data. The regex used is for testing purposes only, for further information about email validation check #Musicamante comments.
This is the code involved:
def setUI(self):
................
................
rx = QRegExp(r"[a-z0-9_%]+#[a-z0-9%_]+\.[a-z0-9%_]{3,3}")
lblAddress = QLabel("EMail Address")
self.lineAddress = QLineEdit(self)
self.mailValidator = QRegExpValidator(rx, self.lineAddress)
self.lineAddress.cursorPositionChanged[int, int].connect(lambda oldPos,
newPos: self.checkValidator(newPos))
self.lineAddress.editingFinished.connect(lambda : self.checkRegExp(rx))
#pyqtSlot(int)
def checkValidator(self, pos):
v = self.mailValidator.validate(self.lineAddress.text(), pos ))
if v[0] == 0:
self.lineAddress.setText(self.lineAddress.text()[:-1])
#pyqtSlot(QRegExp)
def checkRegExp(self, rx):
if not rx.exactMatch(self.lineAddress.text()) and self.lineAddress.text():
if QMessageBox.question(self, "Leave the Address field",
"The string entered is not a valid email address! \n"
"Do you want to clear the field?", QMessageBox.Yes|QMessageBox.No) ==
QMessageBox.Yes:
self.lineAddress.clear()
else:
self.lineAddress.setFocus()
I'm having a problem determining whether or not the checkboxes that are dynamically created have been checked or unchecked by the user in a simple GUI I've created.
I've adapted the relevant code and pasted it below. Although it may be easy to just create and name 4 QStandardItems, I'm dealing with many lists containing many different items that change quite a lot, so it isn't really feasible to create them myself.
Any help finding out how to access these properties would be much appreciated.
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Splash(QWidget):
def __init__(self):
super().__init__()
# imagine this is a very long list...
self.seasons = ['summer','autumn','winter','spring']
self.initUI()
def initUI(self):
layout = QVBoxLayout()
list = QListView()
model = QStandardItemModel()
list.setModel(model)
printbtn = QPushButton('print values')
printbtn.clicked.connect(self.print_action)
for season in self.seasons:
item = QStandardItem(season)
item.setCheckable(True)
model.appendRow(item)
model.dataChanged.connect(lambda: self.print_action(item.text()))
layout.addWidget(printbtn)
layout.addWidget(list)
self.setLayout(layout)
self.show()
def print_action(self, item):
print('changed', item)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = Splash()
sys.exit(app.exec_())
In short - I can detect when an item has been checked using model.dataChanged and connecting that to a function, but it cannot differentiate between the seasons.
If you keep a reference to the list (or the model), you can search for the items by their text, and then get their check-state:
def print_action(self):
model = self.list.model()
for text in 'summer', 'autumn', 'winter', 'spring':
items = model.findItems(text)
if items:
checked = items[0].checkState() == Qt.Checked
print('%s = %s' % (text, checked))
It seems you want to get notified when the checkState of a item has been changed.
In my opinion, there are possible two ways.
First way, QModel will emit "dataChanged" to refresh the view, so you can connect the signal which means the checkState of a item might be changed.
model.dataChanged.connect(self.test)
def test(self):
pass
Second way, use a timer to notify yourself and you check it by yourselves.
timer = QTimer()
timer.timeout.connect(self.test)
timer.start(1000)
sorry if my following question sounds stupid but I am pretty much in a lost, big way!
I have created 2 UIs using the Qt-Designer.
Main UI (anmgToolUI) - http://pastebin.com/raw.php?i=eXVWj99Q
Sub UI (publishInfoUI) - http://pastebin.com/raw.php?i=KsnJC8wR
And the following is the main code:
Main Code - http://pastebin.com/mbg2fuvh
And I am using the following to run it in Maya:
import sys
sys.path.insert(0, '/user_data/test')
import UI_test
reload(UI_test)
win = UI_test.MigrationUi()
win.show()
So basically what I am trying to achieve here is that the values I have typed in the Sub UI is not mapping the values onto the Main UI.
To replicate the case:
Run the execution code
Select any of the 2 items that I have already inserted in the Search Filter QTreeWidget and click on "Add Selected" in which it will transfers it into the Migrate ANMG QTreeWidget
Select an item in the Migrate ANMG QTreeWidget and click onto the Edit Selected button
Try inputting some words etc into either of the 3 fields - Description/ Comment/ Version Comment
I had thought that if I click the OK button, it will mapped the values into columns but it does not seems to be that case, despite me setting a signal/slot in the Qt Designer for accepted() / accept()
Greatly appreciate for any pointers...
I've implemented two solutions, you'll need to comment/uncomment the code. The result is the same in both case.
Solution 1 is based on the reference of the asset you are using in PublishInfoUI class and. Once you press "OK", the modifcations are done inside the PublishInfoUI class on self.assets.
On the other side in solution 2, once you press "OK", it creates a new QTableWidgetItem which is set then returned to your MigrationUI class. Then the modifications are done in this class.
IMO, I prefer the first solution if you rename getValues to updateValues or something like this. But as I said, the result is the same in both case.
class MigrationUi(QtGui.QWidget):
### Some stuff
def editSelected(self):
selected_item = self.ui.treeWidget_migrateAnmg.currentItem()
if selected_item:
inputWin = PublishInfoUI(selected_item)
############################
# Here is the core modifications
if inputWin.exec_(): #Triggered if we press "OK"
#Solution 1:
inputWin.getValues()
#End solution 1
#Solution 2:
returnedAsset = inputWin.getValues()
print "Returned asset: "
print returnedAsset.text(0)
print returnedAsset.text(1)
print returnedAsset.text(2)
print returnedAsset.text(3)
print returnedAsset.text(4)
print returnedAsset.text(5)
print returnedAsset.text(6)
print returnedAsset.text(7)
print returnedAsset.text(8)
selected_item.setText(6, returnedAsset.text(6) )
selected_item.setText(7, returnedAsset.text(7) )
selected_item.setText(8, returnedAsset.text(8) )
#End solution 2
else:
print "Canceled/closed operation"
############################
else:
cmds.warning("Please select an item in ANMG field")
def slotCancel(self):
self.close()
class PublishInfoUI(QtGui.QDialog):
def __init__(self, asset, parent = None, modal = False):
QtGui.QWidget.__init__(self, parent, modal = modal)
self.ui = publishInfoUI_test.Ui_PublishInfo()
self.ui.setupUi(self)
self.assets = asset
self.fill_details()
def fill_details(self):
self.ui.lineEdit_rigUsed.setText(self.assets.text(0))
self.ui.lineEdit_anmLocation.setText(self.assets.text(5))
self.ui.textEdit_comment.setText(self.assets.text(7))
def getValues(self):
#Solution 1:
#Do the modification here
self.assets.setText(6, self.ui.lineEdit_description.text() )
self.assets.setText(7, self.ui.textEdit_comment.toPlainText() )
self.assets.setText(8, self.ui.textEdit_Vcomment.toPlainText() )
#End solution 1
#Solution2:
#Return a new asset and do the modification in MigrationUi class
assetToReturn = QtGui.QTreeWidgetItem()
assetToReturn.setText(6, self.ui.lineEdit_description.text() ) #Feel free to add more infos if necessary
assetToReturn.setText(7, self.ui.textEdit_comment.toPlainText() )
assetToReturn.setText(8, self.ui.textEdit_Vcomment.toPlainText() )
return assetToReturn
#End solution 2
I recently started studying Python and now I'm making a software with a GUI using PyQt Libraries.
Here's my problem:
I create a Scrollarea, I put in this scrollarea a widget which contains a QGridLayout.
sa = QtGui.QScrollArea()
sa_widget = QtGui.QWidget()
self.sa_grid.setSizeConstraint(QtGui.QLayout.SetMinAndMaxSize)
sa_widget.setLayout(self.sa_grid)
sa.setWidgetResizable(True)
sa.setWidget(sa_widget)
Then I add 10 QLabel (this is just an example of course, in this example I'm using a QGridLayout just like a Vertical Layout):
i = 0
while i<100:
i = i +1
add = QtGui.QLabel("Row %i" % i)
self.sa_grid.addWidget(add)
Then I create a button that calls the function "function_name", I want that this function deletes a row, so far this is what I've written:
tmp = QtGui.QWidget()
tmp = self.sa_grid.itemAt(0)
self.sa_grid.removeItem(tmp)
It deletes the first row and every x row of the gridlayout becomes row x-1, however, it doesn't delete the text "Row 1" so I see "Row 0" and "Row 1" on the same row.
Anyone can help me?
Thanks a lot in advance, Davide
Removing an item from a layout does not delete it. The item will just become a free-floating object with no associated layout.
If you want to get rid of the object completely, explicitly delete it:
def deleteGridWidget(self, index):
item = self.sa_grid.itemAt(index)
if item is not None:
widget = item.widget()
if widget is not None:
self.sa_grid.removeWidget(widget)
widget.deleteLater()