I'm using QGridLayout, and by using the setCentralWidget function, the row and column (0,0) start in the center of the window which leaves a lot of empty space.
How can I get it centered but beginning from the top of the window and not in the middle?
I'm pretty new to Qt, and was wondering if I'm handling it all wrong? Should I instead make a new class for QWidget?
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Data visualizing'
self.left = 50
self.top = 50
self.width = 300
self.height = 100
self.initUI()
def initUI(self):
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
# Create textbox
self.textbox = QLineEdit(self)
self.textbox.setReadOnly(True)
# Create textbox 2
self.textbox2 = QLineEdit(self)
self.textbox2.setReadOnly(True)
# Create button
self.button = QPushButton('Load file 1', self)
self.button.setToolTip('Click here to browse for the first data file')
self.button.clicked.connect(self.on_click)
# Create button 2
self.button2 = QPushButton('Load file 2', self)
self.button2.setToolTip('Click here to browse for the first data file')
self.button2.clicked.connect(self.on_click)
grid = QGridLayout()
grid.addWidget(self.textbox, 0, 0, 0, 3)
grid.addWidget(self.textbox2, 0, 3, 0, 3)
grid.addWidget(self.button, 1, 1, 1, 1)
grid.addWidget(self.button2, 1, 4, 1, 1)
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.centralWidget().setLayout(grid)
self.show()
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self, "QFileDialog.getOpenFileName()", "",
"All Files (*);;Comma seperated files (*.csv)", options=options)
if fileName:
self.textbox.setText(fileName)
print(fileName)
@pyqtSlot()
def on_click(self):
self.openFileNameDialog()
print('PyQt5 button click')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
if __name__ == '__main__':
main()