调整QDialog中项目的位置

时间:2013-12-19 11:19:52

标签: python qt pyqt qdialog

我必须显示一个将在QTableView中搜索的查找对话框。我有以下代码:

   def handleFind(self):
    findDialog = QDialog()
    findLabel = QLabel("Find what", findDialog)
    findField = QLineEdit(findDialog)
    findButton = QPushButton("Find", findDialog)
    #closeButton = QPushButton("Close", findDialog)
    findDialog.show() 
    findDialog.exec_()

我的问题:如何调整QDialog中项目的位置,因为现在closeButton会覆盖findButton和findLabel,而且我还想显示findLable和findField下面的按钮。如果你指导我,我将不胜感激。

--->已解决:使用QGridLayout:

        def handleFind(self):
    findDialog = QDialog()
    grid = QGridLayout()
    findDialog.setLayout(grid)

    findLabel = QLabel("Find what", findDialog)
    grid.addWidget(findLabel,1,0)
    findField = QLineEdit(findDialog)
    grid.addWidget(findField,1,1)
    findButton = QPushButton("Find", findDialog)
    grid.addWidget(findButton,2,0)
    closeButton = QPushButton("Close", findDialog)
    grid.addWidget(closeButton,2,1)

    findDialog.show() 
    findDialog.exec_()        

2 个答案:

答案 0 :(得分:1)

不要使用对话框,而是创建自己的“对话框”。 我尝试了类似你正在尝试的东西,我发现这是最简单的。

class CConfigWindow(QtGui.QMainWindow):    
    def __init__(self):
        super(CConfigWindow, self).__init__()
        self.setGeometry(200, 200, 800, 600)
        self.setWindowTitle("config")
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        paneW = QtGui.QWidget()
        grid = QtGui.QGridLayout()
        paneW.setLayout(grid)
        tab = QtGui.QTabWidget(self)
        grid.addWidget(tab,0,0,1,5)

        hest2 = QtGui.QPushButton("Ok")
        hest2.clicked.connect(self.ok)
        grid.addWidget(hest2,1,0)

        hest2 = QtGui.QPushButton("Cancel")
        hest2.clicked.connect(self.cancel)
        grid.addWidget(hest2,1,1)

        hest2 = QtGui.QPushButton("Factory Defaults")
        hest2.clicked.connect(self.Reset)
        grid.addWidget(hest2,1,2)

        self.setCentralWidget(paneW)
        self.show()

        self.activateWindow()
        self.raise_()

    def Reset(self):
        pass            
    def ok(self):
        self.close()

    def cancel(self):
        self.close()

    def closeEvent(self, event):
        event.accept()

self.crap = CConfigWindow()

答案 1 :(得分:1)

我更新了您的代码,以垂直布局排列项目:

def handleFind(self):
findDialog = QDialog()

# add a layout to dialog
layout = QVBoxLayout()
findDialog.setLayout()

# add the items to layout instead of dialog
findLabel = QLabel("Find what", findDialog)
layout.addWidget(findLabel)
findField = QLineEdit(findDialog)
layout.addWidget(findField)
findButton = QPushButton("Find", findDialog)
layout.addWidget(findButton)

#closeButton = QPushButton("Close", findDialog)

# no need to call show() when calling exec_()
#findDialog.show()
findDialog.exec_()

有关Qt布局的更多信息,请转到here