GridLayout and VerticalLayout in PyQT5 - python

I am new to PyQT5 in Python2.7.
I like to have Verticle Layout with two components.
The first area is for Dispaly and the second area is for Control Widgets.
Now problem is (1)Control Widgets are not equally spaced and (2)First area needs more space than the second area.
How can I do that?
My code is as follow.
class Thread(QThread):
changePixmap = pyqtSignal(QImage)
def run(self):
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if ret:
rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
convertToQtFormat = QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0], QImage.Format_RGB888)
p = convertToQtFormat.scaled(640, 480, Qt.KeepAspectRatio)
self.changePixmap.emit(p)
class PlayStreaming(QWidget):
def __init__(self):
super(PlayStreaming,self).__init__()
self.initUI()
#pyqtSlot(QImage)
def setImage(self, image):
self.label.setPixmap(QPixmap.fromImage(image))
def initUI(self):
self.setWindowTitle("Image")
self.setGeometry(100, 100, 600, 400)
self.resize(1800, 1200)
# create a label
self.label = QLabel(self)
self.label.move(280, 120)
self.label.resize(640, 480)
th = Thread(self)
th.changePixmap.connect(self.setImage)
th.start()
class UIWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.tabs.resize(800,600)
# Add tabs
self.tabs.addTab(self.tab1,"Face")
self.tabs.addTab(self.tab2,"Human")
self.tabs.addTab(self.tab3,"Vehicle")
# Create first tab
self.createGridLayout()
self.tab1.layout = QVBoxLayout(self)
self.display=PlayStreaming()
self.tab1.layout.addWidget(self.display)
self.tab1.layout.addWidget(self.horizontalGroupBox)
self.tab1.setLayout(self.tab1.layout)
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
def createGridLayout(self):
self.horizontalGroupBox = QGroupBox("Control")
layout = QGridLayout()
layout.setColumnStretch(2, 3)
layout.addWidget(QPushButton('Test'),0,0)
layout.addWidget(QPushButton('Run'),0,1)
layout.addWidget(QPushButton('Set Faces'),0,2)
layout.addWidget(QPushButton('Recognize'),1,0)
layout.addWidget(QPushButton('Rescale'),1,1)
layout.addWidget(QPushButton('FacePose'),1,2)
self.horizontalGroupBox.setLayout(layout)
The current status is
How can I do that?

You must use the strech to set the weights, by default the stretch of each widget is 0 so you just need to set a stretch of 1 to self.display. On the other hand, it is not necessary to use setColumnStretch() since by default all buttons will have the same size:
from PyQt5 import QtCore, QtGui, QtWidgets
import cv2
class Thread(QtCore.QThread):
changePixmap = QtCore.pyqtSignal(QtGui.QImage)
def run(self):
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if ret:
rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
convertToQtFormat = QtGui.QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0], QtGui.QImage.Format_RGB888)
p = convertToQtFormat.scaled(640, 480, QtCore.Qt.KeepAspectRatio)
self.changePixmap.emit(p)
class PlayStreaming(QtWidgets.QWidget):
def __init__(self):
super(PlayStreaming,self).__init__()
self.initUI()
#QtCore.pyqtSlot(QtGui.QImage)
def setImage(self, image):
self.label.setPixmap(QtGui.QPixmap.fromImage(image))
def initUI(self):
self.setWindowTitle("Image")
# create a label
self.label = QtWidgets.QLabel(self)
th = Thread(self)
th.changePixmap.connect(self.setImage)
th.start()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
class UIWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(UIWidget, self).__init__(parent)
# Initialize tab screen
self.tabs = QtWidgets.QTabWidget()
self.tab1 = QtWidgets.QWidget()
self.tab2 = QtWidgets.QWidget()
self.tab3 = QtWidgets.QWidget()
# Add tabs
self.tabs.addTab(self.tab1,"Face")
self.tabs.addTab(self.tab2,"Human")
self.tabs.addTab(self.tab3,"Vehicle")
# Create first tab
self.createGridLayout()
self.tab1.layout = QtWidgets.QVBoxLayout()
self.display = PlayStreaming()
self.tab1.layout.addWidget(self.display, stretch=1)
self.tab1.layout.addWidget(self.horizontalGroupBox)
self.tab1.setLayout(self.tab1.layout)
# Add tabs to widget
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.tabs)
def createGridLayout(self):
self.horizontalGroupBox = QtWidgets.QGroupBox("Control")
layout = QtWidgets.QGridLayout()
layout.addWidget(QtWidgets.QPushButton('Test'),0,0)
layout.addWidget(QtWidgets.QPushButton('Run'),0,1)
layout.addWidget(QtWidgets.QPushButton('Set Faces'),0,2)
layout.addWidget(QtWidgets.QPushButton('Recognize'),1,0)
layout.addWidget(QtWidgets.QPushButton('Rescale'),1,1)
layout.addWidget(QtWidgets.QPushButton('FacePose'),1,2)
self.horizontalGroupBox.setLayout(layout)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = UIWidget()
w.show()
sys.exit(app.exec_())

Related

Trouble in retrieving window size and widget size

Here is the code for a camera surveillance system. In the configuration_page.py file I'm getting the size of the window as 100x30 despite using self.showMaximized() and resizeEvent(). The widgets present in it are also showing the size as 640x480. I'm particularly interested in retrieving the size of a widget named self.mid_frame in CameraDisplay class present in config.py file.
Earlier I was facing the same issue in dashboard.py file but I found a workaround by explicitly passing width and height in that case. But this time I'm stuck because I need to get the size of the widget named self.mid_frame. I don't understand why even resizeEvent is showing the wrong size.
main.py
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt, QRect
from threading import Thread, RLock, Lock
from collections import deque
from datetime import datetime
import time
import sys
import cv2
import imutils
from dashboard import *
from configuration_page import *
from global_widgets import *
class CameraWidget(QtGui.QWidget):
"""Independent camera feed
Uses threading to grab IP camera frames in the background
#param width - Width of the video frame
#param height - Height of the video frame
#param stream_link - IP/RTSP/Webcam link
#param aspect_ratio - Whether to maintain frame aspect ratio or force into fraame
"""
def __init__(self, stream_link=0, stacked_widget=None, width=0, height=0, btn_text=None, idx=None, aspect_ratio=False, parent=None, deque_size=1):
super(CameraWidget, self).__init__(parent)
# Initialize deque used to store frames read from the stream
self.deque = deque(maxlen=deque_size)
self.maintain_aspect_ratio = aspect_ratio
self.camera_stream_link = stream_link
self.stacked_widget = stacked_widget
self.idx = idx
# Flag to check if camera is valid/working
self.online = False
self.capture = None
self.video_frame = QtGui.QLabel()
self.video_frame_1 = QtGui.QLabel()
self.load_network_stream()
# Start background frame grabbing
self.get_frame_thread = Thread(target=self.get_frame, args=())
self.get_frame_thread.daemon = True
self.get_frame_thread.start()
# Periodically set video frame to display
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.set_frame)
self.timer.start(.5)
print('Started camera: {}'.format(self.camera_stream_link))
def load_network_stream(self):
"""Verifies stream link and open new stream if valid"""
def load_network_stream_thread():
if self.verify_network_stream(self.camera_stream_link):
self.capture = cv2.VideoCapture(self.camera_stream_link)
self.online = True
self.load_stream_thread = Thread(target=load_network_stream_thread, args=())
self.load_stream_thread.daemon = True
self.load_stream_thread.start()
def verify_network_stream(self, link):
"""Attempts to receive a frame from given link"""
cap = cv2.VideoCapture(link)
if not cap.isOpened():
return False
cap.release()
return True
def get_frame(self):
"""Reads frame, resizes, and converts image to pixmap"""
while True:
try:
if self.capture.isOpened() and self.online:
# Read next frame from stream and insert into deque
status, frame = self.capture.read()
if status:
self.deque.append(frame)
else:
self.capture.release()
self.online = False
else:
# Attempt to reconnect
print('attempting to reconnect', self.camera_stream_link)
self.load_network_stream()
self.spin(2)
self.spin(.001)
except AttributeError:
pass
def spin(self, seconds):
"""Pause for set amount of seconds, replaces time.sleep so program doesnt stall"""
time_end = time.time() + seconds
while time.time() < time_end:
QtGui.QApplication.processEvents()
def set_frame(self):
"""Sets pixmap image to video frame"""
if not self.online:
self.spin(1)
return
if self.deque and self.online:
# Grab latest frame
frame = self.deque[-1]
frame_1 = self.deque[-1]
# Display frames on dashboard and configuration pages
# Frame for dashboard
# Keep frame aspect ratio
if self.maintain_aspect_ratio:
self.frame = imutils.resize(frame, width=self.screen_width)
# Force resize
else:
self.frame = cv2.resize(frame, (self.screen_width, self.screen_height))
self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
h, w, ch = self.frame.shape
bytesPerLine = ch * w
# Frame for configuration page-----------------------------------------------------------------------------------------
# print('2: ', self.screen_width_1, self.screen_height_1)
self.frame_1 = cv2.resize(frame_1, (self.screen_width_1, self.screen_height_1))
self.frame_1 = cv2.cvtColor(self.frame_1, cv2.COLOR_BGR2RGB)
h_1, w_1, ch_1 = self.frame_1.shape
bytesPerLine_1 = ch_1 * w_1
# Convert to pixmap and set to video frame
self.img = QtGui.QImage(self.frame, w, h, bytesPerLine, QtGui.QImage.Format_RGB888)
self.pix = QtGui.QPixmap.fromImage(self.img)
self.video_frame.setPixmap(self.pix)
self.img_1 = QtGui.QImage(self.frame_1, w_1, h_1, bytesPerLine_1, QtGui.QImage.Format_RGB888)
self.pix_1 = QtGui.QPixmap.fromImage(self.img_1)
self.video_frame_1.setPixmap(self.pix_1)
def set_frame_params(self, width, height, btn_text=None, idx=0):
self.screen_width = width
self.screen_height = height
self.btn_text = btn_text
self.idx = idx
def set_frame_params_1(self, width, height):
self.screen_width_1 = width
self.screen_height_1 = height
def get_video_display_frame(self):
self.video_display_frame = QtGui.QFrame()
self.video_layout = QtGui.QVBoxLayout()
self.video_btn = QtGui.QPushButton(self.btn_text)
self.video_btn.setStyleSheet("background-color: rgb(128, 159, 255);")
self.video_btn.clicked.connect(self.on_clicked)
# self.video_frame = QtGui.QLabel()
self.video_frame.setScaledContents(True)
self.video_layout.addWidget(self.video_btn)
self.video_layout.addWidget(self.video_frame)
self.video_layout.setContentsMargins(0,0,0,0)
self.video_layout.setSpacing(0)
self.video_display_frame.setLayout(self.video_layout)
return self.video_display_frame
def get_video_frame(self):
self.video_frame_1.setScaledContents(True)
return self.video_frame_1
#QtCore.pyqtSlot()
def on_clicked(self):
GlobalObject().dispatchEvent("hello", args=(self.idx,))
self.stacked_widget.setCurrentIndex(1)
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.stacked_widget = QtGui.QStackedWidget()
layout = QtGui.QVBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.addWidget(self.stacked_widget)
widget = QtGui.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.cam1 = CameraWidget(camera1, self.stacked_widget, idx=1)
widget_1 = DashBoard(self.cam1, self.stacked_widget)
widget_2 = ZoneConfig(self.cam1, self.stacked_widget)
self.stacked_widget.addWidget(widget_1)
self.stacked_widget.addWidget(widget_2)
self.showMaximized()
camera1 = '../streams/Fog.avi'
if __name__ == '__main__':
app = QtGui.QApplication([])
app.setStyle(QtGui.QStyleFactory.create("plastique"))
# app.setStyle(QtGui.QStyleFactory.create("Cleanlooks"))
window = Window()
window.show()
sys.exit(app.exec_())
dashboard.py
from PyQt4 import QtGui, QtCore
from global_widgets import *
class DashBoard(QtGui.QWidget):
def __init__(self, cam1, stacked_widget, parent=None):
super(DashBoard, self).__init__(parent)
self.showMaximized()
self.screen_width = self.width()
self.screen_height = self.height()
self.stacked_widget = stacked_widget
# Layouts and frames
layout = QtGui.QVBoxLayout()
top_frame = QtGui.QFrame()
top_frame.setStyleSheet("background-color: rgb(208, 208, 225)")
mid_frame = QtGui.QFrame()
mid_frame.setStyleSheet("background-color: rgb(153, 187, 255)")
layout.addWidget(top_frame, 1)
layout.addWidget(QHLine())
layout.addWidget(mid_frame, 20)
layout.setContentsMargins(0,0,0,0)
layout.setSpacing(0)
self.setLayout(layout)
# Top frame
label = QtGui.QLabel('Dashboard')
label.setFont(QtGui.QFont('Times New Roman', 20))
top_layout = QtGui.QHBoxLayout()
top_layout.addWidget(label, alignment=QtCore.Qt.AlignCenter)
top_layout.setContentsMargins(5,5,5,5)
top_frame.setLayout(top_layout)
# Middle frame
self.mid_layout = QtGui.QStackedLayout()
# Create camera widgets
print('Creating Camera Widgets...')
cam_widget = Cam1(cam1, self.screen_width, self.screen_height, self)
self.mid_layout.addWidget(cam_widget)
self.mid_layout.setCurrentWidget(cam_widget)
mid_frame.setLayout(self.mid_layout)
class Cam1(QtGui.QWidget):
def __init__(self, cam1, screen_width, screen_height, parent=None):
super(Cam1, self).__init__(parent)
cam1.set_frame_params(screen_width, screen_height, 'VIDS 01', 1)
# Add widgets to layout
print('Adding widgets to layout...')
layout = QtGui.QGridLayout()
layout.addWidget(cam1.get_video_display_frame(),0,0,1,1)
layout.setContentsMargins(5,5,5,5)
self.setLayout(layout)
configuration_page.py
from PyQt4 import QtGui, QtCore
from global_widgets import *
from ui_main import *
class ZoneConfig(QtGui.QWidget):
def __init__(self, cam1, stacked_widget, parent=None):
super(ZoneConfig, self).__init__(parent)
GlobalObject().addEventListener("hello", self.set_cam)
self.layout = QtGui.QVBoxLayout()
self.frame = QtGui.QFrame()
self.frame_layout = QtGui.QStackedLayout()
# self.setLayout(self.frame_layout)
self.screen_width = self.width()
self.screen_height = self.height()
cam_widget_1 = CameraDisplay(cam1, 1, self.frame_layout, stacked_widget, self)
self.frame_layout.addWidget(cam_widget_1)
self.frame.setLayout(self.frame_layout)
self.layout.addWidget(self.frame)
self.layout.setContentsMargins(0,0,0,0)
self.setLayout(self.layout)
#QtCore.pyqtSlot()
def set_cam(self, index):
if index == 1:
self.frame_layout.setCurrentIndex(0)
class CameraDisplay(QtGui.QWidget):
def __init__(self, cam, idx, frame_layout, stacked_widget, parent=None):
super(CameraDisplay, self).__init__(parent)
self.showMaximized()
self.screen_width = self.width()
self.screen_height = self.height()
self.frame_layout = frame_layout
self.stacked_widget = stacked_widget
print('size: ', self.screen_width, self.screen_height)
# Layouts and frames
layout = QtGui.QVBoxLayout()
top_frame = QtGui.QFrame()
top_frame.setStyleSheet("background-color: rgb(208, 208, 225)")
self.mid_frame = QtGui.QFrame() ## Size of this frame is needed
self.mid_frame.setStyleSheet("background-color: rgb(153, 187, 255)")
btm_frame = QtGui.QFrame()
btm_frame.setStyleSheet("background-color: rgb(208, 208, 225)")
# Top frame
label = QtGui.QLabel('Configuration')
label.setFont(QtGui.QFont('Times New Roman', 20))
top_layout = QtGui.QHBoxLayout()
top_layout.addWidget(label, alignment=QtCore.Qt.AlignCenter)
top_layout.setContentsMargins(5,5,5,5)
top_frame.setLayout(top_layout)
# Middle frame
mid_layout = QtGui.QHBoxLayout()
# Create camera widgets
print('Creating Camera Widgets...')
mid_layout = QtGui.QHBoxLayout()
self.video_frame = cam.get_video_frame()
mid_layout.addWidget(self.video_frame)
mid_layout.setContentsMargins(5,5,5,5)
self.mid_frame.setLayout(mid_layout)
# Bottom frame
btn = QtGui.QPushButton('Dashboard')
btn.clicked.connect(self.goMainWindow)
btm_layout = QtGui.QHBoxLayout()
btm_layout.addStretch()
btm_layout.addWidget(btn)
btm_layout.setContentsMargins(5,5,5,5)
btm_frame.setLayout(btm_layout)
layout.addWidget(top_frame, 1)
layout.addWidget(QHLine())
layout.addWidget(self.mid_frame, 50)
layout.addWidget(QHLine())
layout.addWidget(btm_frame, 1)
layout.setContentsMargins(0,0,0,0)
layout.setSpacing(0)
self.setLayout(layout)
# self.showMaximized()
cam.set_frame_params_1(self.mid_frame.width(), self.mid_frame.height()-10) ## here I want the size
def resizeEvent(self, event):
QtGui.QWidget.resizeEvent(self, event)
def goMainWindow(self):
self.stacked_widget.setCurrentIndex(0)
I think the problem is only in configuration_page.py file but I'm pasting entire code in case anyone wants to go throught the entire logic.
Add the following to your code.
def event(self, e):
if e.type() in (QEvent.Show, QEvent.Resize):
widget_w = self.mid_frame.frameGeometry().width()
widget_h = self.mid_frame.frameGeometry().height()
screen_w = self.width()
screen_h = self.height()
# For "debugging" purposes:
print("Widget - width: %s height: %s" % (widget_w, widget_h)
print("screen - width: %s height: %s" % (screen_w, screen_h)
return ((widget_w, widget_h), (screen_w, screen_h))
#return {"widget": {"width": widget_w, "height": widget_h}, "screen": {"width": screen_w, "height": screen_h}}

How to set width QVboxLayout

import sys
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import QApplication,QDialog,QPushButton,QVBoxLayout,QWidget
class Main(QDialog):
def __init__(self):
super(Main, self).__init__()
self.ui()
# Group Of Drage Event
def mousePressEvent(self,event):
self.offset = event.pos()
def mouseMoveEvent(self, e):
x = e.globalX()
y = e.globalY()
x_w = self.offset.x()
y_w = self.offset.y()
self.move(x - x_w, y - y_w)
def ui(self):
# TitleBar
self.setWindowFlags(Qt.FramelessWindowHint)
# Window Size
self.setGeometry(600,300,400,500)
# Window Background Color
self.BackGroundColor = QPalette()
self.BackGroundColor.setColor(QPalette.Background, QColor(255,255,255))
self.setPalette(self.BackGroundColor)
# NavBar Button
self.btn = QPushButton('Test',self)
self.btn1 = QPushButton("Test1",self)
# NavBar Layout
self.layout = QVBoxLayout(self)
self.layout.addWidget(self.btn)
self.layout.addWidget(self.btn1)
self.layout.set
self.setLayout(self.layout)
# Close img
self.closeBtn = QPushButton(self)
self.closeBtn.setGeometry(368,0,32,32)
self.closeBtn.setFlat(True)
self.closeBtn.setStyleSheet('QPushButton{background-color: rgba(0,0,0,0.0)}')
self.closeBtn.setIcon(QIcon('img/close.png'))
self.closeBtn.setIconSize(QSize(10,10))
self.closeBtn.clicked.connect(QCoreApplication.instance().quit)
# Maximize icon
self.maxBtn = QPushButton(self)
self.maxBtn.setGeometry(self,336,0,32,32)
self.maxBtn.setFlat(True)
self.maxBtn.setStyleSheet('QPushButton{background-color: rgba(0,0,0,0.0)}')
self.maxBtn.setIcon(QIcon('img/max.png'))
self.maxBtn.setIconSize(QSize(14,14))
# Minimize Incon
self.minBtn = QPushButton(self)
self.minBtn.setGeometry(304,0,32,32)
self.minBtn.setFlat(True)
self.minBtn.setStyleSheet('QPushButton{background-color: rgba(0,0,0,0.0)}')
self.minBtn.setIcon(QIcon('img/min.png'))
self.minBtn.setIconSize(QSize(10,10))
def main():
app = QApplication()
win = Main()
win.show()
app.exec_()
if __name__ == "__main__":
main()
I want to fixed navbar on the left. So, I create instance of QVBoxLayout and add widget to my Layout. and I had searched google, stackoverflow. i Don't get any information about my problem
but I don't know how to set layout widget. please teach me. Thank you.
if you don't understand my text please tell me i will describe that
Version:
PySide 5.14.2.1
Python 3.7.7
The layouts are not visual elements, so they do not have any geometric element associated with them, such as the width size, the layout is a handle of size and positions.
In this case the solution is to establish a container with a fixed size and in that container place the buttons with the help of a layout:
class Main(QDialog):
def __init__(self):
super(Main, self).__init__()
self.ui()
# Group Of Drage Event
def mousePressEvent(self, event):
self.offset = event.pos()
def mouseMoveEvent(self, e):
x = e.globalX()
y = e.globalY()
x_w = self.offset.x()
y_w = self.offset.y()
self.move(x - x_w, y - y_w)
def ui(self):
# TitleBar
self.setWindowFlags(Qt.FramelessWindowHint)
# Window Size
self.setGeometry(600, 300, 400, 500)
# Window Background Color
self.BackGroundColor = QPalette()
self.BackGroundColor.setColor(QPalette.Background, QColor(255, 255, 255))
self.setPalette(self.BackGroundColor)
# NavBar Button
self.btn = QPushButton("Test")
self.btn1 = QPushButton("Test1")
left_container = QWidget(self)
left_container.setFixedWidth(100)
# NavBar layout
self.layout = QVBoxLayout(left_container)
self.layout.addWidget(self.btn)
self.layout.addWidget(self.btn1)
hlay = QHBoxLayout(self)
hlay.addWidget(left_container)
hlay.addStretch()
# Close img
self.closeBtn = QPushButton(self)
self.closeBtn.setGeometry(368, 0, 32, 32)
self.closeBtn.setFlat(True)
self.closeBtn.setStyleSheet("QPushButton{background-color: rgba(0,0,0,0.0)}")
self.closeBtn.setIcon(QIcon("img/close.png"))
self.closeBtn.setIconSize(QSize(10, 10))
self.closeBtn.clicked.connect(QCoreApplication.instance().quit)
# Maximize icon
self.maxBtn = QPushButton(self)
self.maxBtn.setGeometry(336, 0, 32, 32)
self.maxBtn.setFlat(True)
self.maxBtn.setStyleSheet("QPushButton{background-color: rgba(0,0,0,0.0)}")
self.maxBtn.setIcon(QIcon("img/max.png"))
self.maxBtn.setIconSize(QSize(14, 14))
# Minimize Incon
self.minBtn = QPushButton(self)
self.minBtn.setGeometry(304, 0, 32, 32)
self.minBtn.setFlat(True)
self.minBtn.setStyleSheet("QPushButton{background-color: rgba(0,0,0,0.0)}")
self.minBtn.setIcon(QIcon("img/min.png"))
self.minBtn.setIconSize(QSize(10, 10))

Using QStackedWidget in PyQt5

I have QStackedWidget in ApplicationWindow class and buttons which are going to point to different QWidgets in MenuWindow. I need a help with writing a function which would change the CurrentWidget according to button clicked - e.g. login_button would change the CurrentWidget to LoginWindow.
When trying to do it myself I ran into recursion problems as I have just started with learning Python.
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class ApplicationWindow(QWidget):
def __init__(self):
super(ApplicationWindow, self).__init__()
# stack = Controller()
self.menu = MenuWindow()
self.login = LoginWindow()
self.setGeometry(0, 0, 800, 600)
self.setWindowTitle('Finance tracker')
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.green)
self.setPalette(p)
self.stack = QStackedWidget()
self.stack.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
self.stack.addWidget(self.menu)
self.stack.addWidget(self.login)
self.stack.setCurrentWidget(self.menu)
layout = QVBoxLayout()
layout.addWidget(self.stack)
layout.setAlignment(Qt.AlignCenter)
self.setLayout(layout)
class MenuWindow(QWidget):
def __init__(self):
super(MenuWindow, self).__init__()
self.setGeometry(0, 0, 250, 200)
box = QVBoxLayout()
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.red)
self.setPalette(p)
label = QLabel('Welcome to finance tracker')
label.setStyleSheet('font: 24pt')
box.addWidget(label, alignment=Qt.AlignCenter)
login_button = QPushButton('Login')
login_button.clicked.connect(qApp.exit)
new_button = QPushButton('Create a new account')
new_button.clicked.connect(qApp.exit)
exit_button = QPushButton('Exit')
exit_button.clicked.connect(qApp.exit)
for button in [login_button, new_button, exit_button]:
button.setStyleSheet('font: 14pt')
button.setFixedSize(200, 50)
box.addWidget(button, alignment=Qt.AlignCenter)
self.setLayout(box)
self.show()
class LoginWindow(QWidget):
def __init__(self):
super(LoginWindow, self).__init__()
self.setGeometry(0, 0, 10, 250)
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.blue)
self.setPalette(p)
label = QLabel('Welcome to finance tracker')
box = QVBoxLayout()
box.addWidget(label)
self.setLayout(box)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ApplicationWindow()
window.show()
sys.exit(app.exec())
Since you are using QPushButtons to switch pages I would add them to a QButtonGroup. This way you can connect the QButtonGroup.buttonClicked[int] signal to QStackedWidget.setCurrentIndex. Keep a pointer to the QButtonGroup in your MenuWindow.
class MenuWindow(QWidget):
def __init__(self):
...
login_button = QPushButton('Login')
new_button = QPushButton('Create a new account')
exit_button = QPushButton('Exit')
exit_button.clicked.connect(qApp.exit)
self.btn_group = QButtonGroup()
for i, button in enumerate([login_button, new_button, exit_button]):
button.setStyleSheet('font: 14pt')
button.setFixedSize(200, 50)
box.addWidget(button, alignment=Qt.AlignCenter)
self.btn_group.addButton(button)
self.btn_group.setId(button, i + 1)
...
And now you can connect the signal and slot in your ApplicationWindow.
class ApplicationWindow(QWidget):
def __init__(self):
...
self.menu.btn_group.buttonClicked[int].connect(self.stack.setCurrentIndex)

AttributeError: 'Settings' object has no attribute 'scene'

I need to add pixmap to my rectangles. When I press the click button then my pixmap will be added to one by one in my rectangles but I got this attribute error. Can any one please guide me how to solve this error? I tried so many ways but I didn't get the proper output.
Thank you in advance.
Given below is my code:
from pyface.qt import QtGui, QtCore
import sys
global X,Y
class ScanView(QtGui.QGraphicsView):
def __init__(self,X=5, Y=5, parent=None):
super(ScanView, self).__init__(parent)
self.row = X
self.cols = Y
self.squareLB = 50
self.width = Y*self.squareLB+2*self.squareLB
self.height = X*self.squareLB+2*self.squareLB
self.List = []
if self.width>708:
self.scene = QtGui.QGraphicsScene(0,0,self.width,self.height)
for i in range(self.row):
for j in range(self.cols):
item = self.scene.addRect(QtCore.QRectF(0,0,self.squareLB,self.squareLB))
item.setPos(self.squareLB+j*self.squareLB, X*self.squareLB-(i*self.squareLB))
self.List.append(item)
else:
self.scene = QtGui.QGraphicsScene(0,0,708,self.height)
self.marginLR = (708.0-Y*self.squareLB)/2.0
for i in range(self.row):
for j in range(self.cols):
item = self.scene.addRect(QtCore.QRectF(0,0,self.squareLB,self.squareLB))
item.setPos(self.marginLR+j*self.squareLB, X*self.squareLB-(i*self.squareLB))
self.List.append(item)
self.setScene(self.scene)
class Settings(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Settings, self).__init__(parent)
spacer = QtGui.QWidget(self)
spacer.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
self.vbox = QtGui.QVBoxLayout()
self.save = QtGui.QPushButton("save")
self.open= QtGui.QPushButton("open")
self.folder= QtGui.QPushButton("Folder")
self.folder.clicked.connect(self.showSettings)
self.vbox.addWidget(self.save)
self.vbox.addWidget(self.open)
self.vbox.addWidget(self.folder)
self.grid = QtGui.QGridLayout()
self.grid.addLayout(self.vbox,0,0)
self.scrollArea = QtGui.QScrollArea()
self.scrollArea.setBackgroundRole(QtGui.QPalette.Light)
self.scrollArea.setWidgetResizable(True)
self.grid.addWidget(self.scrollArea,0,1)
self.setCentralWidget(QtGui.QWidget(self))
self.centralWidget().setLayout(self.grid)
self.setGeometry(200,100,300,300)
self.show()
def showSettings(self):
self.MyView = ScanView(5,5)
self.vbox2 = QtGui.QVBoxLayout()
self.Scanbtn1 = QtGui.QPushButton(("click"))
self.Scanbtn1.clicked.connect(self.on_clicked)
self.vbox2.addWidget(self.Scanbtn1)
self.newwidget = QtGui.QWidget()
self.glayout = QtGui.QGridLayout(self.newwidget)
self.glayout.addWidget(self.MyView,0,0)
self.Sw = QtGui.QWidget()
self.Sw.setLayout(self.vbox2)
# self.Sw.setFixedWidth(width - self.scrollArea.viewport().width())
self.glayout.addWidget(self.Sw,0,1)
self.scrollArea.setWidget(self.newwidget)
def on_clicked(self):
print "hellloooooo"
filename1 = "./img/tick.png"
pixmap = QtGui.QPixmap(filename1)
if not pixmap.isNull():
self.MyView.add_pixmap(pixmap)
# pic = QtGui.QPixmap("./img/tick.png")
# scene.addItem(QtGui.QGraphicsPixmapItem(pic))
# # view = self.gv
# self.MyView.setScene(scene)
# self.MyView.setRenderHint(QtGui.QPainter.Antialiasing)
# self.MyView.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Settings()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You have that error because scene does not exist in the Settings class, self.scene is a member of the ScanView class plus self.scene is different from scene.
Going to the problem, you must add as a child of the rectangles and you must also change the size of the pixmap is necessary. To obtain the first rectangle you must store it in a list and then create an iterator.
import sys
from pyface.qt import QtGui, QtCore
class ScanView(QtGui.QGraphicsView):
def __init__(self,X=5, Y=5, parent=None):
super(ScanView, self).__init__(parent)
self._squares = []
n_rows, n_cols = X, Y
squareLB = 50
width, height = (Y + 2)*squareLB, (X + 2)*squareLB
self._scene = QtGui.QGraphicsScene(0, 0, max(708, width), height)
p = squareLB if width > 708 else (708.0-Y*squareLB)/2.0
for i in range(n_rows):
for j in range(n_cols):
it = self._scene.addRect(0, 0, squareLB, squareLB)
it.setPos(p + j*squareLB, i*squareLB)
self._squares.append(it)
self.setScene(self._scene)
class Settings(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Settings, self).__init__(parent)
self.save = QtGui.QPushButton("save")
self.open = QtGui.QPushButton("open")
self.folder = QtGui.QPushButton("Folder", clicked=self.showSettings)
central_widget = QtGui.QWidget()
self.setCentralWidget(central_widget)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.save)
vbox.addWidget(self.open)
vbox.addWidget(self.folder)
self.scrollArea = QtGui.QScrollArea(widgetResizable=True)
self.scrollArea.setBackgroundRole(QtGui.QPalette.Light)
hlay = QtGui.QHBoxLayout(central_widget)
hlay.addLayout(vbox)
hlay.addWidget(self.scrollArea)
self.setGeometry(200, 100, 300, 300)
def showSettings(self):
self.view = ScanView(5, 5)
self.scanbtn = QtGui.QPushButton("click", clicked=self.on_clicked)
self.newwidget = QtGui.QWidget()
hlay = QtGui.QHBoxLayout(self.newwidget)
hlay.addWidget(self.view)
hlay.addWidget(self.scanbtn)
self.scrollArea.setWidget(self.newwidget)
self._iter_squares = iter(self.view._squares)
def on_clicked(self):
filename = "./img/tick.png"
pixmap = QtGui.QPixmap(filename)
if pixmap.isNull():
return
try:
it = next(self._iter_squares)
except StopIteration:
pass
else:
pixmap = pixmap.scaled(it.rect().size().toSize())
pixmap_it = QtGui.QGraphicsPixmapItem(pixmap, it)
def main():
app = QtGui.QApplication(sys.argv)
ex = Settings()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

QFrame is not drawn

I am having a problem diplaying a QFrame in PyQt4 Python2.7.
When I display the QLineEdit, the Qframe self.videoframeRemote is not drawn.
If I remove the QlineEdit, it is drawn.
I cannot find whats going wrong. Here is the code:
class Player(QtGui.QMainWindow):
def __init__(self, master=None):
QtGui.QMainWindow.__init__(self, master)
self.setWindowTitle("Media Player")
self.instance1 = vlc.Instance()
self.instance2 = vlc.Instance()
self.mediaplayerLocal = self.instance1.media_player_new()
self.mediaplayerRemote = self.instance2.media_player_new()
self.createUI()
self.isPaused = False
self.OpenWebCamMic()
def createUI(self):
self.widget = QtGui.QWidget(self)
self.setCentralWidget(self.widget)
self.vboxlayout = QtGui.QVBoxLayout()
self.hboxlayout = QtGui.QHBoxLayout()
# In this widget, the video will be drawn
self.videoframeLocal = QtGui.QFrame()
self.palette1 = self.videoframeLocal.palette()
self.palette1.setColor (QtGui.QPalette.Window, QtGui.QColor(0,0,0))
self.videoframeRemote = QtGui.QFrame()
self.palette2 = self.videoframeRemote.palette()
self.palette2.setColor (QtGui.QPalette.Window, QtGui.QColor(0,0,0))
self.videoframeLocal.setPalette(self.palette1)
self.videoframeLocal.setAutoFillBackground(True)
self.videoframeLocal.setMaximumSize(160, 120)
self.vboxlayout.addWidget(self.videoframeLocal)
self.URIbox = QtGui.QLineEdit("type SIP URI here")
self.URIbox.setMaximumSize(160, 30)
self.vboxlayout.addWidget(self.URIbox)
self.callButton = QtGui.QPushButton("Call")
self.callButton.setMaximumSize(160, 30)
self.vboxlayout.addWidget(self.callButton)
self.connect(self.callButton, QtCore.SIGNAL("clicked()"), self.establishCall)
self.byeButton = QtGui.QPushButton("Bye")
self.byeButton.setMaximumSize(160, 30)
self.vboxlayout.addWidget(self.byeButton)
self.connect(self.byeButton, QtCore.SIGNAL("clicked()"), self.Bye)
self.hboxlayout.addLayout(self.vboxlayout)
self.videoframeRemote.setPalette(self.palette2)
self.videoframeRemote.setAutoFillBackground(True)
self.videoframeRemote.setMaximumSize(640, 480)
self.hboxlayout.addWidget(self.videoframeRemote)
self.widget.setLayout(self.hboxlayout)
self.timer = QtCore.QTimer(self)
self.timer.setInterval(200)
self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.updateUI)
Any suggestions?
regards,
Ck
Thank you Bakuriu,
adding self.videoframeRemote.setMinimumSize(640, 480) made the videoframe visible!

Categories

Resources