I created a Main window with 2 button by pyqt5 and now i want to add a combo box to it. But if i keep App(QMainWindow), the combo box wont show. Only if i write App(QWidgets) it will show. Are there any way to add a combo box to main window ? here is my code:
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Phan mem quan ly tai lieu- Tuan tien mom'
self.left = 200
self.top = 200
self.width = 320
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('chaychuongtrinh', self)
button.setToolTip('bam vao nut nay de chay chuong trinh')
button.move(100, 70)
button.clicked.connect(self.on_click)
button1 = QPushButton('kiemtra', self)
button1.setToolTip('kiem tra thong tin')
button1.move(200, 70)
button1.clicked.connect(self.on_click)
layout = QHBoxLayout()
self.cb = QComboBox()
self.cb.addItem("C")
self.cb.addItem("C++")
self.cb.addItems(["Java", "C#", "Python"])
self.cb.currentIndexChanged.connect(self.selectionchange)
layout.addWidget(self.cb)
self.setLayout(layout)
self.setWindowTitle("combo box demo")
self.show()
def selectionchange(self,i):
print ("Items in the list are :")
print(self.cb.currentText())
QMainWindow
is a special widget, it already has a layout, so we should not add another layout as shown in the image:
In this case you must create a new widget and place it in the central widget, in this new widget we must place the widget like the QPushButton or the QComboBox.
In your case the QPushButton
are visible because if a widget passed a parent, it will be located in the position (0, 0) of the parent, and then you have moved it to a specific position with move()
. In contrast, the combobox has no parent so it is not visible.
Also if you are going to use a layout, you should not use the move () function since the layout will expand throughout the widget. In the following example without using the layout:
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.widget = QWidget(self)
self.setCentralWidget(self.widget)
button = QPushButton('chaychuongtrinh', self.widget)
button.move(100, 70)
button.setToolTip('bam vao nut nay de chay chuong trinh')
button.clicked.connect(self.on_click)
button1 = QPushButton('kiemtra', self.widget)
button1.setToolTip('kiem tra thong tin')
button1.clicked.connect(self.on_click)
button1.move(200, 70)
self.cb = QComboBox(self.widget)
self.cb.addItem("C")
self.cb.addItem("C++")
self.cb.addItems(["Java", "C#", "Python"])
self.cb.currentIndexChanged.connect(self.selectionchange)
self.cb.move(300, 70)
self.setWindowTitle("combo box demo")
with layouts:
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.widget = QWidget(self)
self.setCentralWidget(self.widget)
layout = QHBoxLayout(self.widget)
layout.addItem(QSpacerItem(139, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
vlay = QVBoxLayout()
vlay.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
layout.addLayout(vlay)
layout.addItem(QSpacerItem(139, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
button = QPushButton('chaychuongtrinh', self)
button.setToolTip('bam vao nut nay de chay chuong trinh')
button.clicked.connect(self.on_click)
button1 = QPushButton('kiemtra', self)
button1.setToolTip('kiem tra thong tin')
button1.clicked.connect(self.on_click)
self.cb = QComboBox(self)
self.cb.addItem("C")
self.cb.addItem("C++")
self.cb.addItems(["Java", "C#", "Python"])
self.cb.currentIndexChanged.connect(self.selectionchange)
vlay.addWidget(button)
vlay.addWidget(button1)
vlay.addWidget(self.cb)
vlay.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
self.setWindowTitle("combo box demo")
self.show()