[PySide] Qinputdialog
Erik Johansson
erik.johansson at fido.se
Wed Jul 3 09:47:12 CEST 2013
You should take a look at the setData function. That should enable you to
add int's and automatically get correct number sorting.
Cheers,
Erik
On Wed, Jul 3, 2013 at 8:43 AM, Sean Fisk <sean at seanfisk.com> wrote:
> Hi David,
>
> I‘ve wanted to do this before. I don’t think it's possible to use
> QInputDialog.getInt() without a spin box, so constructing a custom QDialogseems like the proper solution. I've also include a solution with
> QInputDialog, but that doesn't restrict the user to positive integers
> only. Just give it a try and you'll see what I mean. I personally like the
> QDialog way.
>
> I wasn't exactly sure what you meant by the sorting, so I just threw the
> integers in a QListWidget. Unfortunately, the QListWidget sorting
> algorithm<http://qt-project.org/doc/qt-4.8/qlistwidget.html#sortingEnabled-prop>sorts lexicographically, not numerically. So I just used Python‘s list sort
> and re-added all the numbers to the list. Not the nicest solution, but it
> works. That shouldn’t be too hard to adapt to QTreeWidget.
>
> Let me know if I misunderstood any part of your question.
>
> ~ Sean
>
> Here is the code:
>
> #!/usr/bin/env python
> # Prompt the user for an integer and display it in a sorted list.# Python 2.7 code
> from PySide import QtGui, QtCore
> class MainWindow(QtGui.QMainWindow):
> def __init__(self, parent=None):
> super(MainWindow, self).__init__(parent)
>
> self._int_list = []
>
> self.setCentralWidget(QtGui.QWidget())
>
> self._layout = QtGui.QVBoxLayout(self.centralWidget())
> self._int_list_widget = QtGui.QListWidget()
> self._layout.addWidget(self._int_list_widget)
>
> self._add_int_qinputdialog_button = QtGui.QPushButton(
> 'Add integer (QInputDialog)')
> self._add_int_qinputdialog_button.clicked.connect(
> self._add_int_qinputdialog)
> self._layout.addWidget(self._add_int_qinputdialog_button)
>
> self._add_int_qdialog_button = QtGui.QPushButton(
> 'Add integer (Custom QDialog)')
> self._add_int_qdialog_button.clicked.connect(
> self._add_int_qdialog)
> self._layout.addWidget(self._add_int_qdialog_button)
>
> def _complain_about_integer(self):
> QtGui.QMessageBox.critical(
> self, 'Invalid Input', 'Please enter a positive integer.')
>
> def _add_int_to_list(self, text):
> try:
> integer = int(text)
> except ValueError:
> self._complain_about_integer()
> return
> if integer < 0:
> # For the QInputDialog solution, since it doesn't prevent
> # negatives.
> self._complain_about_integer()
> return
> self._int_list.append(integer)
> self._int_list.sort()
> self._int_list_widget.clear()
> self._int_list_widget.addItems(
> [str(integer) for integer in self._int_list])
>
> def _add_int_qinputdialog(self):
> text, ok = QtGui.QInputDialog.getText(
> self, 'Values id Dialog', 'Enter Id of the Value',
> inputMethodHints=QtCore.Qt.ImhDigitsOnly)
> if not ok:
> # User closed the dialog.
> return
> self._add_int_to_list(text)
>
> def _add_int_qdialog(self):
> dialog = IntDialog()
> accepted = dialog.exec_()
> if accepted == QtGui.QDialog.Rejected:
> # User closed the dialog.
> return
> self._add_int_to_list(dialog.int_text)
> class IntDialog(QtGui.QDialog):
> def __init__(self, parent=None):
> super(IntDialog, self).__init__(parent)
>
> self.setWindowTitle('Values id Dialog')
>
> self._layout = QtGui.QFormLayout(self)
>
> self._input_field = QtGui.QLineEdit()
> self._int_validator = QtGui.QIntValidator()
> self._int_validator.setBottom(0)
> self._input_field.setValidator(self._int_validator)
>
> self._layout.addRow('Enter Id of the Value', self._input_field)
>
> self._button_box = QtGui.QDialogButtonBox(
> QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
> self._button_box.accepted.connect(self.accept)
> self._button_box.rejected.connect(self.reject)
> self._layout.addWidget(self._button_box)
>
> @property
> def int_text(self):
> return self._input_field.text()
> def main(argv):
> app = QtGui.QApplication(argv)
>
> w = MainWindow()
> w.show()
> w.raise_()
> return app.exec_()
> if __name__ == '__main__':
> import sys
> raise SystemExit(main(sys.argv))
>
> On Tue, Jul 2, 2013 at 2:05 PM, <litltbear at gmail.com> wrote:
>
> 1. I want to get input from a user it will be an integer. I'm trying
>> valueId, ok = QtGui.QInputDialog.getInt(self, 'Values id Dialog',
>> 'Enter Id of the Value', minValue=0)
>>
>> and it always show a spin box. How do i remove the spin box.
>>
>> 2. When I display the int I use:
>> item_0 = QtGui.QTreeWidgetItem(self.ValuesTree)
>> item_0.setText(0, str(value.valueId))
>>
>> Is there a way to display int's to the screen as nit's so they
>> can be sorted/displayed in numerical order?
>>
>> ie 11, 12, 104
>> not 104, 11, 12.
>>
>> David
>> _______________________________________________
>> PySide mailing list
>> PySide at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/pyside
>>
> --
> Sean Fisk
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130703/737b184c/attachment.html>
More information about the PySide
mailing list