Align center elements in a QGroupBox PyQT - python

I need to align center some labels that are inside of a QGroupBox ( I want that labels to be centered even on resizing), I tried many "solutions" but none of them worked, the QGroupBox is inside of a QGridLayout and has expanding width.

from PyQt5 import QtCore, QtWidgets, QtGui
import sys
class TestWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QHBoxLayout()
self.setLayout(layout)
group = QtWidgets.QGroupBox()
layout.addWidget(group)
group_layout = QtWidgets.QHBoxLayout()
group.setLayout(group_layout)
labelContainerWidget = QtWidgets.QWidget()
labelContainer_layout = QtWidgets.QHBoxLayout()
labelContainerWidget.setLayout(labelContainer_layout)
label1 = QtWidgets.QLabel('test1')
label2 = QtWidgets.QLabel('test2')
group_layout.setAlignment(QtCore.Qt.AlignCenter)
group_layout.addWidget(label1)
group_layout.addWidget(label2)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
form = TestWidget()
form.show()
app.exec_()
Use layout.setAlignment(QtCore.Qt.AlignCenter)

Related

Move texts to the center

I want to create a log in screen. But I cant move these 2 Labels in the mid of the screen. I'm sure you can help me guys :) See pics in the Link.
https://ibb.co/vP1ydvk
https://ibb.co/crWh94n
def show_welcome_message(self):
vertical_layout = QVBoxLayout()
horizontal_layout = QHBoxLayout()
group_box = QGroupBox()
welcome_label = QLabel("Welcome!")
user_label = QLabel("Logged in as test test test")
welcome_label.setFont(QFont("Times", 20))
vertical_layout.addStretch(1)
vertical_layout.addWidget(welcome_label)
vertical_layout.addWidget(user_label)
vertical_layout.addStretch(1)
horizontal_layout.addStretch(1)
horizontal_layout.addLayout(vertical_layout)
horizontal_layout.addStretch(1)
group_box.setLayout(horizontal_layout)
self.MainWindow.setCentralWidget(group_box)
Here is an example of a widget with two centered labels:
from PyQt5 import QtWidgets, QtCore
if __name__ == "__main__":
app = QtWidgets.QApplication([])
label1 = QtWidgets.QLabel('This is a label')
label2 = QtWidgets.QLabel('This is another label')
# center text within labels
label1.setAlignment(QtCore.Qt.AlignCenter)
label2.setAlignment(QtCore.Qt.AlignCenter)
widget = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout(widget)
# center labels within layout
layout.setAlignment(QtCore.Qt.AlignCenter)
layout.addWidget(label1)
layout.addWidget(label2)
widget.resize(400,400)
widget.show()
app.exec()
Try it:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
grid = QGridLayout(centralWidget)
group_box = QGroupBox()
grid.addWidget(group_box)
self.vertical_layout = QVBoxLayout()
group_box.setLayout(self.vertical_layout)
self.show_welcome_message()
def show_welcome_message(self):
welcome_label = QLabel("Welcome!", alignment=Qt.AlignCenter)
welcome_label.setFont(QFont("Times", 20))
user_label = QLabel("Logged in as test test test", alignment=Qt.AlignCenter)
self.vertical_layout.addStretch(1)
self.vertical_layout.addWidget(welcome_label)
self.vertical_layout.addWidget(user_label)
self.vertical_layout.addStretch(1)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

pyqt5 sizing correctly a tableview element in a tabbed widget

I'm not able to size correctly a table view element (self.table_View_1) inside a tab,
I tried different approaches like containers, layout and so on without reaching the objective.
The code refer to a model class that allows to show pandas data frame inside the table view.
here's the code:
import sys
import Mod_PX
from PandasModel import PandasModel
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QApplication, QPushButton, QHBoxLayout, QTabWidget, QVBoxLayout, QWidget, QMainWindow, QGroupBox
from PyQt5.QtCore import pyqtSlot
class ProgramWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setup_main_window()
self.set_window_layout()
def setup_main_window(self):
self.centralwidget = QWidget()
self.setCentralWidget(self.centralwidget)
self.resize( 800, 350 )
self.setWindowTitle( "ProjectX" )
def set_window_layout(self):
self.vbox_1 = QVBoxLayout(self.centralwidget)
self.hgroup_2 = QGroupBox()
self.hlayout_2 = QHBoxLayout()
self.hgroup_2.setLayout(self.hlayout_2)
self.btn_Analisi = QPushButton('Analisi', self)
self.btn_Help = QPushButton('Help')
self.btn_Wiz = QPushButton('Wizard')
self.btn_Analisi.clicked.connect(self.loadFile)
self.hlayout_2.addWidget(self.btn_Analisi)
self.hlayout_2.addWidget(self.btn_Help)
self.hlayout_2.addWidget(self.btn_Wiz)
self.vbox_1.addWidget(self.hgroup_2)
# Initialize tabs_1 screen
self.tabs_1 = QTabWidget()
self.tab1_1 = QWidget()
self.tab2_1 = QWidget()
self.tab3_1 = QWidget()
# Add tabs
self.tabs_1.addTab(self.tab1_1, "WWA")
self.tabs_1.addTab(self.tab2_1, "WTP")
self.tabs_1.addTab(self.tab3_1, "WTW")
# Add tabs to widget
self.vbox_1.addWidget(self.tabs_1)
# Initialize tabs_2 screen
self.tabs_2 = QTabWidget(self.tab1_1)
self.tab1_2 = QWidget()
self.tab2_2 = QWidget()
self.tabs_2.setTabPosition(QtWidgets.QTabWidget.West)
self.vbox_2 = QVBoxLayout(self.tab1_1)
# Add tabs
self.tabs_2.addTab(self.tab1_2, "Foglio 1")
self.tabs_2.addTab(self.tab2_2, "Foglio 2")
#Add tabs to widget
self.table_View_1 = QtWidgets.QTableView(self.tab1_2)
self.table_View_1.setObjectName("table_View_1")
font = QtGui.QFont()
font.setPointSize(10)
self.table_View_1.setFont(font)
self.table_View_1.setGeometry(0,0,self.tab1_2.width(),self.tab1_2.height())
self.vbox_2.addWidget(self.tabs_2)
self.show()
#pyqtSlot()
def loadFile(self):
df = Mod_PX.sheet_b()
model = PandasModel(df)
self.table_View_1.setModel(model)
def main():
app = QApplication(sys.argv)
programWindow = ProgramWindow()
programWindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Here the GUI with the table not correctly sized:
Thank you in advance for your support!
First you must correctly state the layouts and you should not use setGeometry since that is the task of the layouts. I have modified the structure of your code to have an order so at the end I point out the relationship between the layouts.
def set_window_layout(self):
#buttons
self.btn_Analisi = QtWidgets.QPushButton('Analisi')
self.btn_Help = QtWidgets.QPushButton('Help')
self.btn_Wiz = QtWidgets.QPushButton('Wizard')
# Initialize tabs_1 screen
self.tabs_1 = QtWidgets.QTabWidget()
self.tab1_1 = QtWidgets.QWidget()
self.tab2_1 = QtWidgets.QWidget()
self.tab3_1 = QtWidgets.QWidget()
# Add tabs
self.tabs_1.addTab(self.tab1_1, "WWA")
self.tabs_1.addTab(self.tab2_1, "WTP")
self.tabs_1.addTab(self.tab3_1, "WTW")
# Initialize tabs_2 screen
self.tabs_2 = QtWidgets.QTabWidget()
self.tab1_2 = QtWidgets.QWidget()
self.tab2_2 = QtWidgets.QWidget()
self.tabs_2.setTabPosition(QtWidgets.QTabWidget.West)
# Add tabs
self.tabs_2.addTab(self.tab1_2, "Foglio 1")
self.tabs_2.addTab(self.tab2_2, "Foglio 2")
self.table_View_1 = QtWidgets.QTableView()
font = QtGui.QFont()
font.setPointSize(10)
self.table_View_1.setFont(font)
# connections
self.btn_Analisi.clicked.connect(self.loadFile)
# layouts
vbox = QtWidgets.QVBoxLayout(self.centralwidget)
hgroup = QtWidgets.QGroupBox()
vbox.addWidget(hgroup)
hlay_group = QtWidgets.QHBoxLayout(hgroup)
hlay_group.addWidget(self.btn_Analisi)
hlay_group.addWidget(self.btn_Help)
hlay_group.addWidget(self.btn_Wiz)
vbox.addWidget(self.tabs_1)
lay = QtWidgets.QVBoxLayout(self.tab1_1)
lay.addWidget(self.tabs_2)
lay_tableview = QtWidgets.QVBoxLayout(self.tab1_2)
lay_tableview.addWidget(self.table_View_1)

How do I set layout's fixed height in PyQt5?

I am trying to set a fixed height for QHBoxLayout. To elaborate more on that, I need to set a specific height for my Horizontal layout. However, I cannot find the proper way to do so. What should I do to make this happen?
hbox1 = QHBoxLayout()
As noted by #ekhumuro in QHBoxLayout you can not set the fixed height, you must do that to the widget where it will be contained as I show below:
import random
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setFixedHeight(300)
lay = QtWidgets.QHBoxLayout(self)
for letter in "ABCDEFG":
label = QtWidgets.QLabel(letter, alignment=QtCore.Qt.AlignCenter)
color = QtGui.QColor(*[random.randint(0, 255) for _ in range(3)])
label.setStyleSheet("background-color: {}".format(color.name()))
lay.addWidget(label)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

pyqt5 Is there a limit to loading widgets using clicked.connect?

I'm using the QPushButton to load the UI. First -> Jumin -> Department -> next -> next I want to create the UI in order. The problem is that I can not load the third Department into the QMainwindow window. I do not know why
When you create a widget in QVBoxLayout, it changes the size of the widget according to the wallpaper like wxpython layout (wx.all). Can not change the position (move) and size (resize) by automatic centering?
import sys
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.center_widget = QWidget()
self.setCentralWidget(self.center_widget)
self.FirstUI()
def FirstUI(self):
self.btn1 = QPushButton('test1', self)
self.btn1.move(50, 50)
self.btn1.clicked.connect(self.btn1_click)
def JuminUI(self):
self.ju1 = QLineEdit('13')
self.btn2 = QPushButton('^^^^^^^^^^')
self.ju_text = QLabel('asd')
self.jumim_layout = QVBoxLayout()
self.jumim_layout.addWidget(self.ju_text)
self.jumim_layout.addWidget(self.ju1)
self.jumim_layout.addWidget(self.btn2)
self.centralWidget().setLayout(self.jumim_layout)
self.btn2.clicked.connect(self.btn2_click)
def DepartmentUI(self):
self.depart_layout = QVBoxLayout()
self.depart_layout.addWidget(QPushButton('sdfsdf'))
self.centralWidget().setLayout(self.depart_layout)
def btn1_click(self):
self.btn1.deleteLater()
self.JuminUI()
def btn2_click(self):
self.ju1.deleteLater()
self.btn2.deleteLater()
self.ju_text.deleteLater()
self.DepartmentUI()
if __name__ == "__main__":
app = QApplication(sys.argv)
fream = MainWindow()
fream.show()
app.exec_()
creating and removing widgets is almost always a bad idea, and your code falls into those bad ideas, it's always best to hide the widgets and for that you should use the QStackedWidget, what QStackedWidget does is just make a widget visible on all widgets that you have been assigned by changing the currentIndex.
import sys
from functools import partial
from PyQt5 import QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.center_widget = QtWidgets.QStackedWidget()
self.setCentralWidget(self.center_widget)
self.FirstUI()
self.JuminUI()
self.DepartmentUI()
def FirstUI(self):
widget = QtWidgets.QWidget()
self.btn1 = QtWidgets.QPushButton('test1', widget)
self.btn1.move(50, 50)
self.center_widget.addWidget(widget)
self.btn1.clicked.connect(partial(self.center_widget.setCurrentIndex, 1))
def JuminUI(self):
widget = QtWidgets.QWidget()
lay = QtWidgets.QVBoxLayout(widget)
self.ju1 = QtWidgets.QLineEdit('13')
self.btn2 = QtWidgets.QPushButton('^^^^^^^^^^')
self.ju_text = QtWidgets.QLabel('asd')
lay.addWidget(self.ju_text)
lay.addWidget(self.ju1)
lay.addWidget(self.btn2)
self.center_widget.addWidget(widget)
self.btn2.clicked.connect(partial(self.center_widget.setCurrentIndex, 2))
def DepartmentUI(self):
widget = QtWidgets.QWidget()
lay = QtWidgets.QVBoxLayout(widget)
lay.addWidget(QtWidgets.QPushButton('sdfsdf'))
self.center_widget.addWidget(widget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
fream = MainWindow()
fream.show()
sys.exit(app.exec_())

PyQt5 QScrollArea widget with dynamically created GroupBoxes

I'm trying to make a toolbox widget that will do various different things. But I'm having trouble with the layout management regarding the QScrollArea. Following the stripped version of the code I have:
from PyQt5 import QtWidgets
import sys
class MyScrollWidget(QtWidgets.QWidget):
def __init__(self):
super(MyScrollWidget, self).__init__()
scrollArea = QtWidgets.QScrollArea(self)
top_widget = QtWidgets.QWidget()
top_layout = QtWidgets.QVBoxLayout()
for i in range(10):
group_box = QtWidgets.QGroupBox()
group_box.setTitle('GroupBox For Item {0}'.format(i))
layout = QtWidgets.QHBoxLayout(group_box)
label = QtWidgets.QLabel()
label.setText('Label For Item {0}'.format(i))
layout.addWidget(label)
push_button = QtWidgets.QPushButton(group_box)
push_button.setText('Run Button')
push_button.setFixedSize(100, 32)
layout.addWidget(push_button)
group_box.setLayout(layout)
top_layout.addWidget(group_box)
top_widget.setLayout(top_layout)
scrollArea.setWidget(top_widget)
self.resize(200, 500)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widget = MyScrollWidget()
widget.show()
sys.exit(app.exec_())
But this only gives me a small fixed subsection of the widget that scrolled. But what I really wants is the whole widget to be scrollable if the widget/window is smaller than the total size of all the group boxes. I.e I'd like the widget to be used as if it was all fixed width, but if the user resized the widget smaller than that, it would scroll appropriately. I've tried various different methods with no good results so now I'm deferring to those who have more experience with layout management than I. Thank you for your time.
You have to set the scrollArea to MyScrollWidget using a layout.
from PyQt5 import QtWidgets
import sys
class MyScrollWidget(QtWidgets.QWidget):
def __init__(self):
super(MyScrollWidget, self).__init__()
lay = QtWidgets.QVBoxLayout(self)
scrollArea = QtWidgets.QScrollArea()
lay.addWidget(scrollArea)
top_widget = QtWidgets.QWidget()
top_layout = QtWidgets.QVBoxLayout()
for i in range(10):
group_box = QtWidgets.QGroupBox()
group_box.setTitle('GroupBox For Item {0}'.format(i))
layout = QtWidgets.QHBoxLayout(group_box)
label = QtWidgets.QLabel()
label.setText('Label For Item {0}'.format(i))
layout.addWidget(label)
push_button = QtWidgets.QPushButton(group_box)
push_button.setText('Run Button')
push_button.setFixedSize(100, 32)
layout.addWidget(push_button)
top_layout.addWidget(group_box)
top_widget.setLayout(top_layout)
scrollArea.setWidget(top_widget)
self.resize(200, 500)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widget = MyScrollWidget()
widget.show()
sys.exit(app.exec_())

Categories

Resources