Adjust position of items in QDialog

1.5k views Asked by At

I have to show a find dialog which will search in a QTableView. I have the following code:

   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_()

My problem: How to adjust the position of items in QDialog, as now closeButton overwrites findButton and findLabel and also i would like to show the buttons bellow findLable and findField. I would appreciate if you guide me in this..

---> SOLVED: By using 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

There are 2 answers

0
Svend Feldt On BEST ANSWER

Don't use the dialog, create your own "dialog" instead. I have tried something similar to what you are trying and I found this the easiest.

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()
0
qurban On

I updated your code to arrange the items in vertical layout:

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_()

For more information on Qt Layouts go here