Skip to content

Instantly share code, notes, and snippets.

@gitex
Last active May 2, 2016 09:31
Show Gist options
  • Save gitex/83b06e8ceaac18ad5dec2d332ed6cbaa to your computer and use it in GitHub Desktop.
Save gitex/83b06e8ceaac18ad5dec2d332ed6cbaa to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
class Widget(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(parent)
grid = QtGui.QGridLayout()
grid.setSpacing(3)
self.edit_first = QtGui.QLineEdit()
grid.addWidget(QtGui.QLabel('Question 1'), 1, 0)
grid.addWidget(self.edit_first, 1, 1)
# add layout for second widget
self.edit_second = QtGui.QLineEdit()
grid.addWidget(QtGui.QLabel('Question 2'), 2, 0)
grid.addWidget(self.edit_second, 2, 1)
apply_button = QtGui.QPushButton('Apply', self)
apply_button.clicked.connect(self.close)
grid.addWidget(apply_button, 4, 3)
self.setLayout(grid)
self.setGeometry(300, 300, 350, 300)
def return_strings(self):
# Return list of values. It need map with str (self.lineedit.text() will return QString)
return map(str, [self.q1Edit.text(), self.q2Edit.text()])
@staticmethod
def get_data(parent=None):
dialog = Widget(parent)
dialog.exec_()
return dialog.return_strings()
def main():
app = QtGui.QApplication([])
window = Widget()
print window.get_data() # window is value from edit field
if __name__ == '__main__':
main()
@chriselswede
Copy link

Great it works! But when I tried to have more than one question in the same dialog box, the dialog opens twice:

import sys
from PyQt4 import QtGui

class Widget(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        self.answer1 = QtGui.QLabel()
        q1Edit = QtGui.QLineEdit()
        q1Edit.textChanged.connect(self.q1Changed)

        self.answer2 = QtGui.QLabel()
        q2Edit = QtGui.QLineEdit()
        q2Edit.textChanged.connect(self.q2Changed)

        grid = QtGui.QGridLayout()
        grid.setSpacing(20)

        grid.addWidget(QtGui.QLabel('Question 1'), 1, 0)
        grid.addWidget(q1Edit, 1, 1)
        grid.addWidget(self.answer1, 1, 2)

        grid.addWidget(QtGui.QLabel('Question 2'), 2, 0)
        grid.addWidget(q2Edit, 2, 1)
        grid.addWidget(self.answer2, 2, 2)

        applyBtn = QtGui.QPushButton('Apply', self)
        applyBtn.clicked.connect(self.close)

        grid.addWidget(applyBtn,4,3)
        self.setLayout(grid)
        self.setGeometry(300, 300, 350, 300)

    def q1Changed(self, text):
        self.answer1.setText(text)

    def q2Changed(self, text):
        self.answer2.setText(text)

    def returnAnswer1(self):
        return self.answer1.text()

    def returnAnswer2(self):
        return self.answer2.text()

    @staticmethod
    def getData1(parent=None):
        dialog = Widget(parent)
        dialog.exec_()
        return dialog.returnAnswer1()

    @staticmethod
    def getData2(parent=None):
        dialog = Widget(parent)
        dialog.exec_()
        return dialog.returnAnswer2()

def main():
    app = QtGui.QApplication([])
    window = Widget()
    print window.getData1()  # window is value from edit field
    print window.getData2()  # window is value from edit field


if __name__ == '__main__':
    main()

Any idea how to solve that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment