简体   繁体   中英

PyQt4 Custom dialog not showing

i'm working with PyQt4 to make a simple Python GUI app.

The situation is the following:

i have an QMainWindow displaying a central widget and a QDockWidget containing this custom Widget:

class ListTagWidget(QWidget):
        def __init__(self, parent = None):
            super(ListTagWidget, self).__init__()
            addButton = QPushButton("&Add Tag...")
            editButton = QPushButton("&Edit Tag...")
            removeButton = QPushButton("&Delete Tag")
            self.taglist = QListWidget()
            layout = QGridLayout(self)
            layout.addWidget(self.taglist, 1, 1, 1, 1)
            layout.addWidget(addButton, 2, 1)
            layout.addWidget(editButton, 3, 1)
            layout.addWidget(removeButton, 4, 1)
            self.setLayout(layout)
            self.adjustSize()
            #Connections
            self.connect(addButton, SIGNAL("clicked()"), self.addTag)

        def addTag(self):
            dialog = AddTagDlg(self)
            dialog.show()

I basicly want to display this custom dialog class when addButton is clicked:

class AddTagDlg(QDialog):
    def __init__(self, Parent=None):
        super(AddTagDlg, self).__init__()
        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel)
        label = QLabel("Tag Name:")
        lineEdit = QLineEdit()
        layout = QGridLayout()
        layout.addWidget(label, 1, 1)
        layout.addWidget(lineEdit, 1, 2)
        layout.addWidget(buttonBox, 2, 1)
        self.setLayout(layout)
        self.setWindowTitle("Add Tag...")

But this don't work. I've managed to create a dialog inline by changing the addTag method to:

def addTag(self):
    dialog = QDialog()
    dialog.show()

But i'm not satisfied with inline dialog creation. What's my error? Thank you.

EDIT

The problem was with the custom dialog class constructor:

class AddTagDlg(QDialog):
    def __init__(self, Parent=None):
        super(AddTagDlg, self).__init__(parent) #<--WAS MISSING
        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel)
        ...

尝试在对话框上调用exec_() ,这exec_()您显示对话框。

If the name of the code is flask , it will search inside it. So change the name of the code from flask and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM