1

I'm creating a simple popup box that contains settings. I want this to be a modal dialog box. I created the window from qt creator and converted the .ui to .py using pyuic. With the main window i'm able to import the .py file into my main project so that I can make changes to the gui and not have them overwritten every time that I update the GUI within QT Creator. When I try the same approach with my dialog box I was able to do so but then had no way of making the window modal. My fix was to bring the code from the file into the main py script. This has the disadvantage of me not being able to update the window from QT Creator now. Is there a better way to incorporate the new popup window that i created from QT Designer into my main GUI? When you click popup that is the window I wish to incorporate.

All the files: main.py

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(637, 559)
        self.centralWidget = QtGui.QWidget(MainWindow)
        self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.centralWidget)
        self.horizontalLayout.setMargin(2)
        self.horizontalLayout.setSpacing(1)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        spacerItem = QtGui.QSpacerItem(13, 485, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.horizontalLayout.addItem(spacerItem)
        self.popup = QtGui.QPushButton(self.centralWidget)
        self.popup.setObjectName(_fromUtf8("popup"))
        self.horizontalLayout.addWidget(self.popup)
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setMargin(2)
        self.verticalLayout.setSpacing(1)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.textBrowser = QtGui.QTextBrowser(self.centralWidget)
        self.textBrowser.setObjectName(_fromUtf8("textBrowser"))
        self.verticalLayout.addWidget(self.textBrowser)
        self.horizontalLayout_7 = QtGui.QHBoxLayout()
        self.horizontalLayout_7.setMargin(2)
        self.horizontalLayout_7.setSpacing(1)
        self.horizontalLayout_7.setObjectName(_fromUtf8("horizontalLayout_7"))
        self.open_port = QtGui.QPushButton(self.centralWidget)
        self.open_port.setObjectName(_fromUtf8("open_port"))
        self.horizontalLayout_7.addWidget(self.open_port)
        self.closeButton = QtGui.QPushButton(self.centralWidget)
        self.closeButton.setObjectName(_fromUtf8("closeButton"))
        self.horizontalLayout_7.addWidget(self.closeButton)
        self.verticalLayout.addLayout(self.horizontalLayout_7)
        self.horizontalLayout.addLayout(self.verticalLayout)
        spacerItem1 = QtGui.QSpacerItem(13, 485, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.horizontalLayout.addItem(spacerItem1)
        MainWindow.setCentralWidget(self.centralWidget)
        self.statusBar = QtGui.QStatusBar(MainWindow)
        self.statusBar.setObjectName(_fromUtf8("statusBar"))
        MainWindow.setStatusBar(self.statusBar)
        self.actionSettings = QtGui.QAction(MainWindow)
        self.actionSettings.setObjectName(_fromUtf8("actionSettings"))

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.closeButton, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.popup.setText(_translate("MainWindow", "popup", None))
        self.open_port.setText(_translate("MainWindow", "Open Port", None))
        self.closeButton.setText(_translate("MainWindow", "Close", None))
        self.actionSettings.setText(_translate("MainWindow", "Settings", None))

comsettings.py what i'm hoping to turn into a dialog box

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_comSettings(object):
    def setupUi(self, comSettings):
        comSettings.setObjectName(_fromUtf8("comSettings"))
        comSettings.resize(252, 209)
        self.verticalLayout = QtGui.QVBoxLayout(comSettings)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.horizontalLayout_13 = QtGui.QHBoxLayout()
        self.horizontalLayout_13.setObjectName(_fromUtf8("horizontalLayout_13"))
        self.label_13 = QtGui.QLabel(comSettings)
        self.label_13.setMinimumSize(QtCore.QSize(70, 0))
        self.label_13.setObjectName(_fromUtf8("label_13"))
        self.horizontalLayout_13.addWidget(self.label_13)
        self.portBox_3 = QtGui.QComboBox(comSettings)
        self.portBox_3.setMinimumSize(QtCore.QSize(150, 0))
        self.portBox_3.setObjectName(_fromUtf8("portBox_3"))
        self.horizontalLayout_13.addWidget(self.portBox_3)
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_13.addItem(spacerItem)
        self.verticalLayout.addLayout(self.horizontalLayout_13)
        self.horizontalLayout_14 = QtGui.QHBoxLayout()
        self.horizontalLayout_14.setObjectName(_fromUtf8("horizontalLayout_14"))
        self.label_14 = QtGui.QLabel(comSettings)
        self.label_14.setMinimumSize(QtCore.QSize(70, 0))
        self.label_14.setObjectName(_fromUtf8("label_14"))
        self.horizontalLayout_14.addWidget(self.label_14)
        self.baudBox_3 = QtGui.QComboBox(comSettings)
        self.baudBox_3.setMinimumSize(QtCore.QSize(150, 0))
        self.baudBox_3.setObjectName(_fromUtf8("baudBox_3"))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.baudBox_3.addItem(_fromUtf8(""))
        self.horizontalLayout_14.addWidget(self.baudBox_3)
        spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_14.addItem(spacerItem1)
        self.verticalLayout.addLayout(self.horizontalLayout_14)
        self.horizontalLayout_15 = QtGui.QHBoxLayout()
        self.horizontalLayout_15.setObjectName(_fromUtf8("horizontalLayout_15"))
        self.label_15 = QtGui.QLabel(comSettings)
        self.label_15.setMinimumSize(QtCore.QSize(70, 0))
        self.label_15.setObjectName(_fromUtf8("label_15"))
        self.horizontalLayout_15.addWidget(self.label_15)
        self.dataBox_3 = QtGui.QComboBox(comSettings)
        self.dataBox_3.setMinimumSize(QtCore.QSize(150, 0))
        self.dataBox_3.setObjectName(_fromUtf8("dataBox_3"))
        self.dataBox_3.addItem(_fromUtf8(""))
        self.dataBox_3.addItem(_fromUtf8(""))
        self.horizontalLayout_15.addWidget(self.dataBox_3)
        spacerItem2 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_15.addItem(spacerItem2)
        self.verticalLayout.addLayout(self.horizontalLayout_15)
        self.horizontalLayout_16 = QtGui.QHBoxLayout()
        self.horizontalLayout_16.setObjectName(_fromUtf8("horizontalLayout_16"))
        self.label_16 = QtGui.QLabel(comSettings)
        self.label_16.setMinimumSize(QtCore.QSize(70, 0))
        self.label_16.setObjectName(_fromUtf8("label_16"))
        self.horizontalLayout_16.addWidget(self.label_16)
        self.parityBox_3 = QtGui.QComboBox(comSettings)
        self.parityBox_3.setMinimumSize(QtCore.QSize(150, 0))
        self.parityBox_3.setObjectName(_fromUtf8("parityBox_3"))
        self.parityBox_3.addItem(_fromUtf8(""))
        self.parityBox_3.addItem(_fromUtf8(""))
        self.parityBox_3.addItem(_fromUtf8(""))
        self.parityBox_3.addItem(_fromUtf8(""))
        self.parityBox_3.addItem(_fromUtf8(""))
        self.horizontalLayout_16.addWidget(self.parityBox_3)
        spacerItem3 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_16.addItem(spacerItem3)
        self.verticalLayout.addLayout(self.horizontalLayout_16)
        self.horizontalLayout_17 = QtGui.QHBoxLayout()
        self.horizontalLayout_17.setObjectName(_fromUtf8("horizontalLayout_17"))
        self.label_17 = QtGui.QLabel(comSettings)
        self.label_17.setMinimumSize(QtCore.QSize(70, 0))
        self.label_17.setObjectName(_fromUtf8("label_17"))
        self.horizontalLayout_17.addWidget(self.label_17)
        self.stopBox_3 = QtGui.QComboBox(comSettings)
        self.stopBox_3.setMinimumSize(QtCore.QSize(150, 0))
        self.stopBox_3.setObjectName(_fromUtf8("stopBox_3"))
        self.stopBox_3.addItem(_fromUtf8(""))
        self.stopBox_3.addItem(_fromUtf8(""))
        self.stopBox_3.addItem(_fromUtf8(""))
        self.horizontalLayout_17.addWidget(self.stopBox_3)
        spacerItem4 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_17.addItem(spacerItem4)
        self.verticalLayout.addLayout(self.horizontalLayout_17)
        self.horizontalLayout_18 = QtGui.QHBoxLayout()
        self.horizontalLayout_18.setObjectName(_fromUtf8("horizontalLayout_18"))
        self.label_18 = QtGui.QLabel(comSettings)
        self.label_18.setMinimumSize(QtCore.QSize(70, 0))
        self.label_18.setObjectName(_fromUtf8("label_18"))
        self.horizontalLayout_18.addWidget(self.label_18)
        self.fcBox_3 = QtGui.QComboBox(comSettings)
        self.fcBox_3.setMinimumSize(QtCore.QSize(150, 0))
        self.fcBox_3.setObjectName(_fromUtf8("fcBox_3"))
        self.fcBox_3.addItem(_fromUtf8(""))
        self.fcBox_3.addItem(_fromUtf8(""))
        self.fcBox_3.addItem(_fromUtf8(""))
        self.horizontalLayout_18.addWidget(self.fcBox_3)
        spacerItem5 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_18.addItem(spacerItem5)
        self.verticalLayout.addLayout(self.horizontalLayout_18)
        self.buttonBox = QtGui.QDialogButtonBox(comSettings)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
        self.verticalLayout.addWidget(self.buttonBox)

        self.retranslateUi(comSettings)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), comSettings.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), comSettings.reject)
        QtCore.QMetaObject.connectSlotsByName(comSettings)

    def retranslateUi(self, comSettings):
        comSettings.setWindowTitle(_translate("comSettings", "Dialog", None))
        self.label_13.setText(_translate("comSettings", "COM Port", None))
        self.label_14.setText(_translate("comSettings", "Baud Rate", None))
        self.baudBox_3.setItemText(0, _translate("comSettings", "110", None))
        self.baudBox_3.setItemText(1, _translate("comSettings", "300", None))
        self.baudBox_3.setItemText(2, _translate("comSettings", "600", None))
        self.baudBox_3.setItemText(3, _translate("comSettings", "1200", None))
        self.baudBox_3.setItemText(4, _translate("comSettings", "2400", None))
        self.baudBox_3.setItemText(5, _translate("comSettings", "4800", None))
        self.baudBox_3.setItemText(6, _translate("comSettings", "9600", None))
        self.baudBox_3.setItemText(7, _translate("comSettings", "14400", None))
        self.baudBox_3.setItemText(8, _translate("comSettings", "19200", None))
        self.baudBox_3.setItemText(9, _translate("comSettings", "38400", None))
        self.baudBox_3.setItemText(10, _translate("comSettings", "57600", None))
        self.baudBox_3.setItemText(11, _translate("comSettings", "115200", None))
        self.baudBox_3.setItemText(12, _translate("comSettings", "230400", None))
        self.baudBox_3.setItemText(13, _translate("comSettings", "460800", None))
        self.baudBox_3.setItemText(14, _translate("comSettings", "921600", None))
        self.label_15.setText(_translate("comSettings", "Data", None))
        self.dataBox_3.setItemText(0, _translate("comSettings", "8 bit", None))
        self.dataBox_3.setItemText(1, _translate("comSettings", "7 bit", None))
        self.label_16.setText(_translate("comSettings", "Parity", None))
        self.parityBox_3.setItemText(0, _translate("comSettings", "None", None))
        self.parityBox_3.setItemText(1, _translate("comSettings", "Odd", None))
        self.parityBox_3.setItemText(2, _translate("comSettings", "Even", None))
        self.parityBox_3.setItemText(3, _translate("comSettings", "Mark", None))
        self.parityBox_3.setItemText(4, _translate("comSettings", "Space", None))
        self.label_17.setText(_translate("comSettings", "Stop", None))
        self.stopBox_3.setItemText(0, _translate("comSettings", "1 bit", None))
        self.stopBox_3.setItemText(1, _translate("comSettings", "1.5 bit", None))
        self.stopBox_3.setItemText(2, _translate("comSettings", "2 bit", None))
        self.label_18.setText(_translate("comSettings", "Flow Control", None))
        self.fcBox_3.setItemText(0, _translate("comSettings", "None", None))
        self.fcBox_3.setItemText(1, _translate("comSettings", "Xon/Xoff", None))
        self.fcBox_3.setItemText(2, _translate("comSettings", "hardware", None))

Main file that incorporates everything together:

from PyQt4 import QtCore, QtGui
from main import Ui_MainWindow
#from comsettings import Ui_comSettings
import serial.tools.list_ports #must explicitly import tools


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class AThread(QtCore.QThread):
    updated = QtCore.pyqtSignal(str)

    def __init__(self):
        QtCore.QThread.__init__(self)

    def run(self):
        while True:
            if ser.in_waiting:
                line=ser.readline()[:-2]#remove end of line \r\n
                self.updated.emit(line.decode('utf-8'))  

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

        self.setObjectName(_fromUtf8("settingsPopUp"))
        self.resize(331, 295)
        self.centralWidget = QtGui.QWidget(self)
        self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.centralWidget)
        self.verticalLayout.setMargin(2)
        self.verticalLayout.setSpacing(1)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setMargin(2)
        self.horizontalLayout.setSpacing(1)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.label = QtGui.QLabel(self.centralWidget)
        self.label.setMinimumSize(QtCore.QSize(70, 0))
        self.label.setObjectName(_fromUtf8("label"))
        self.horizontalLayout.addWidget(self.label)
        self.portBox = QtGui.QComboBox(self.centralWidget)
        self.portBox.setMinimumSize(QtCore.QSize(150, 0))
        self.portBox.setObjectName(_fromUtf8("portBox"))
        self.horizontalLayout.addWidget(self.portBox)
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtGui.QHBoxLayout()
        self.horizontalLayout_2.setMargin(2)
        self.horizontalLayout_2.setSpacing(1)
        self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
        self.label_2 = QtGui.QLabel(self.centralWidget)
        self.label_2.setMinimumSize(QtCore.QSize(70, 0))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.horizontalLayout_2.addWidget(self.label_2)
        self.baudBox = QtGui.QComboBox(self.centralWidget)
        self.baudBox.setMinimumSize(QtCore.QSize(150, 0))
        self.baudBox.setObjectName(_fromUtf8("baudBox"))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.baudBox.addItem(_fromUtf8(""))
        self.horizontalLayout_2.addWidget(self.baudBox)
        spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem1)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtGui.QHBoxLayout()
        self.horizontalLayout_3.setMargin(2)
        self.horizontalLayout_3.setSpacing(1)
        self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
        self.label_3 = QtGui.QLabel(self.centralWidget)
        self.label_3.setMinimumSize(QtCore.QSize(70, 0))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.horizontalLayout_3.addWidget(self.label_3)
        self.dataBox = QtGui.QComboBox(self.centralWidget)
        self.dataBox.setMinimumSize(QtCore.QSize(150, 0))
        self.dataBox.setObjectName(_fromUtf8("dataBox"))
        self.dataBox.addItem(_fromUtf8(""))
        self.dataBox.addItem(_fromUtf8(""))
        self.horizontalLayout_3.addWidget(self.dataBox)
        spacerItem2 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_3.addItem(spacerItem2)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_4 = QtGui.QHBoxLayout()
        self.horizontalLayout_4.setMargin(2)
        self.horizontalLayout_4.setSpacing(1)
        self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4"))
        self.label_4 = QtGui.QLabel(self.centralWidget)
        self.label_4.setMinimumSize(QtCore.QSize(70, 0))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.horizontalLayout_4.addWidget(self.label_4)
        self.parityBox = QtGui.QComboBox(self.centralWidget)
        self.parityBox.setMinimumSize(QtCore.QSize(150, 0))
        self.parityBox.setObjectName(_fromUtf8("parityBox"))
        self.parityBox.addItem(_fromUtf8(""))
        self.parityBox.addItem(_fromUtf8(""))
        self.parityBox.addItem(_fromUtf8(""))
        self.parityBox.addItem(_fromUtf8(""))
        self.parityBox.addItem(_fromUtf8(""))
        self.horizontalLayout_4.addWidget(self.parityBox)
        spacerItem3 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_4.addItem(spacerItem3)
        self.verticalLayout.addLayout(self.horizontalLayout_4)
        self.horizontalLayout_5 = QtGui.QHBoxLayout()
        self.horizontalLayout_5.setMargin(2)
        self.horizontalLayout_5.setSpacing(1)
        self.horizontalLayout_5.setObjectName(_fromUtf8("horizontalLayout_5"))
        self.label_5 = QtGui.QLabel(self.centralWidget)
        self.label_5.setMinimumSize(QtCore.QSize(70, 0))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.horizontalLayout_5.addWidget(self.label_5)
        self.stopBox = QtGui.QComboBox(self.centralWidget)
        self.stopBox.setMinimumSize(QtCore.QSize(150, 0))
        self.stopBox.setObjectName(_fromUtf8("stopBox"))
        self.stopBox.addItem(_fromUtf8(""))
        self.stopBox.addItem(_fromUtf8(""))
        self.stopBox.addItem(_fromUtf8(""))
        self.horizontalLayout_5.addWidget(self.stopBox)
        spacerItem4 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_5.addItem(spacerItem4)
        self.verticalLayout.addLayout(self.horizontalLayout_5)
        self.horizontalLayout_6 = QtGui.QHBoxLayout()
        self.horizontalLayout_6.setMargin(2)
        self.horizontalLayout_6.setSpacing(1)
        self.horizontalLayout_6.setObjectName(_fromUtf8("horizontalLayout_6"))
        self.label_6 = QtGui.QLabel(self.centralWidget)
        self.label_6.setMinimumSize(QtCore.QSize(70, 0))
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.horizontalLayout_6.addWidget(self.label_6)
        self.fcBox = QtGui.QComboBox(self.centralWidget)
        self.fcBox.setMinimumSize(QtCore.QSize(150, 0))
        self.fcBox.setObjectName(_fromUtf8("fcBox"))
        self.fcBox.addItem(_fromUtf8("1"))
        self.fcBox.addItem(_fromUtf8("2"))
        self.fcBox.addItem(_fromUtf8("3"))
        self.horizontalLayout_6.addWidget(self.fcBox)
        spacerItem5 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_6.addItem(spacerItem5)
        self.verticalLayout.addLayout(self.horizontalLayout_6)
        self.buttonBox = QtGui.QDialogButtonBox(self)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
        self.verticalLayout.addWidget(self.buttonBox)
        self.setLayout(self.verticalLayout)
        self.retranslateUi(self)

    def retranslateUi(self, settingsPopUp):
        settingsPopUp.setWindowTitle(_translate("settingsPopUp", "settingsPopUp", None))
        self.label.setText(_translate("settingsPopUp", "COM Port", None))
        self.label_2.setText(_translate("settingsPopUp", "Baud Rate", None))
        self.baudBox.setItemText(0, _translate("settingsPopUp", "110", None))
        self.baudBox.setItemText(1, _translate("settingsPopUp", "300", None))
        self.baudBox.setItemText(2, _translate("settingsPopUp", "600", None))
        self.baudBox.setItemText(3, _translate("settingsPopUp", "1200", None))
        self.baudBox.setItemText(4, _translate("settingsPopUp", "2400", None))
        self.baudBox.setItemText(5, _translate("settingsPopUp", "4800", None))
        self.baudBox.setItemText(6, _translate("settingsPopUp", "9600", None))
        self.baudBox.setItemText(7, _translate("settingsPopUp", "14400", None))
        self.baudBox.setItemText(8, _translate("settingsPopUp", "19200", None))
        self.baudBox.setItemText(9, _translate("settingsPopUp", "38400", None))
        self.baudBox.setItemText(10, _translate("settingsPopUp", "57600", None))
        self.baudBox.setItemText(11, _translate("settingsPopUp", "115200", None))
        self.baudBox.setItemText(12, _translate("settingsPopUp", "230400", None))
        self.baudBox.setItemText(13, _translate("settingsPopUp", "460800", None))
        self.baudBox.setItemText(14, _translate("settingsPopUp", "921600", None))
        self.label_3.setText(_translate("settingsPopUp", "Data", None))
        self.dataBox.setItemText(0, _translate("settingsPopUp", "8 bit", None))
        self.dataBox.setItemText(1, _translate("settingsPopUp", "7 bit", None))
        self.label_4.setText(_translate("settingsPopUp", "Parity", None))
        self.parityBox.setItemText(0, _translate("settingsPopUp", "None", None))
        self.parityBox.setItemText(1, _translate("settingsPopUp", "Odd", None))
        self.parityBox.setItemText(2, _translate("settingsPopUp", "Even", None))
        self.parityBox.setItemText(3, _translate("settingsPopUp", "Mark", None))
        self.parityBox.setItemText(4, _translate("settingsPopUp", "Space", None))
        self.label_5.setText(_translate("settingsPopUp", "Stop", None))
        self.stopBox.setItemText(0, _translate("settingsPopUp", "1 bit", None))
        self.stopBox.setItemText(1, _translate("settingsPopUp", "1.5 bit", None))
        self.stopBox.setItemText(2, _translate("settingsPopUp", "2 bit", None))
        self.label_6.setText(_translate("settingsPopUp", "Flow Control", None))
        self.fcBox.setItemText(0, _translate("settingsPopUp", "None", None))
        self.fcBox.setItemText(1, _translate("settingsPopUp", "Xon/Xoff", None))
        self.fcBox.setItemText(2, _translate("settingsPopUp", "hardware", None))
        #self.closeButton.setText(_translate("settingsPopUp", "Close", None))

class New_Ui_MainWindow(Ui_MainWindow):
    def setupUi(self, MainWindow):
        Ui_MainWindow.setupUi(self,MainWindow)
        self.open_port.clicked.connect(self.start_serial)
        self.popup.clicked.connect(self.popupSettings)
        self.textBrowser.document().setMaximumBlockCount(5000)#number of lines before deleting.
        self.thread= AThread()
        self.thread.updated.connect(self.updateText) 
        #self.comboadd=serial.tools.list_ports.comports()
        #for port in self.comboadd:
        #    self.portBox.addItem(str(port))

    def updateText (self, text ):
        self.textBrowser.append(text)

    def start_serial(self):
        ser.baudrate = 115200
        ser.port = self.portBox.currentText()[:4]
        ser.timeout=.1
        ser.open()
        self.thread.start();

    def popupSettings(self):
        self.settings = SettingsDialog()
        self.settings.exec_()


if __name__ == "__main__":
    import sys
    import serial
    ser = serial.Serial()
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = New_Ui_MainWindow()
    ui.setupUi(MainWindow)

    MainWindow.show()
    sys.exit(app.exec_())
kaminsknator
  • 1,135
  • 3
  • 15
  • 26

1 Answers1

1

Generally, you would override the Qt Widget (i.e. QMainWindow) and not the class that Qt creator generates, but you can still do it the way you're doing if you want. You just need to pass the parent QMainWindow to the Dialog. Currently, you're not storing a reference to it.

class New_Ui_MainWindow(Ui_MainWindow):
    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow
        ...

    def popupSettings(self):
        self.settings = SettingsDialog(self.MainWindow)
        self.settings.exec_()

If you wanted to just use the generated dialog code. You could do this. You

from dialog import Ui_comSettings

class New_Ui_MainWindow(Ui_MainWindow):
    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow
        ...

    def popupSettings(self):
        self.settings = QDialog(self.MainWindow)
        self.settings.ui = Ui_comSettings()
        self.settings.ui.setupUi(self.settings)
        self.settings.exec_()
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • Thank you for your input would you mind going into more detail what you're doing and how it fixes the issue? Using this approach am I able to eliminate the majority of this class: `class SettingsDialog(QtGui.QDialog)` and just reference the .py file as before? – kaminsknator Sep 23 '16 at 14:40
  • Also if i try `self.settings = SettingsDialog(self.MainWindow)` i get the error `TypeError: object() takes no parameters` – kaminsknator Sep 23 '16 at 15:51
  • Your code must not match the example you provided. `SettingsDialog` is a `QDialog` in your example, not an `object` – Brendan Abel Sep 23 '16 at 16:21
  • Yes I was mistaken and was trying it on some other code. That doesn't seem to change any of the operations. How would that help me eliminate the verbose code and instead just use an import? – kaminsknator Sep 23 '16 at 18:38
  • @kaminsknator Updated the answer to show how you could do it with just the generated dialog code from creator. – Brendan Abel Sep 23 '16 at 18:47
  • The code worked great thanks for your insight. I'm still new on a number of aspects. Just to clarify `self.settings = QDialog(self.MainWindow)` creates a new dialog window and passes the parent to the dialog. Then it copies the settings and sets up the window passing the dialog window to the function in the comsettings class. Just trying to understand it so I can troubleshoot this better in the future. – kaminsknator Sep 23 '16 at 20:37
  • Yeah, that's pretty much how it works. Personally, I never liked the *compile-ui-to-python-file* workflow. I much prefer subclassing the Qt classes directly and loading the ui files dynamically. See this [question](http://stackoverflow.com/questions/2398800/linking-a-qtdesigner-ui-file-to-python-pyqt) for an idea of what I mean. That way, you don't have to deal with two different objects that represent your class (the actual QObject, and the generated (and subclassed) UI python object). – Brendan Abel Sep 23 '16 at 20:41
  • That is officially the better best way so far thanks for the insight. I was able to get it working with that method and seem to understand what's going on now. I now no longer have to compile the ui. The only downside to using uic.loadui is that now when trying to access the objects like pushbuttons etc my IDE no longer populates a list of options though the functionality remains. Thanks for your help again very informative. – kaminsknator Sep 23 '16 at 21:12
  • Do I still need to store a reference to the parent as in `self.MainWindow = MainWindow` in this area: `def __init__(self, parent=None): QtGui.QMainWindow.__init__(self, parent) uic.loadUi('mainwindow.ui', self)` doing so results in an error that MainWindow is undefined – kaminsknator Sep 23 '16 at 21:19
  • @kaminsknator No, you don't need to store a reference because the QObject already does. You can just call `self.parent()` to get the reference back. – Brendan Abel Sep 23 '16 at 21:20
  • Ok thanks for clearing up that issue I'm following what you are saying. – kaminsknator Sep 23 '16 at 21:21
  • When creating a dialog the proper call would be `self.settings=QtGui.QDialog(self.parent())` ? – kaminsknator Sep 23 '16 at 21:25
  • You probably want this -- `self.settings = QtGui.QDialog(self)` – Brendan Abel Sep 23 '16 at 21:36