How can I force the splitter to be positioned in the center of the window at the start? As you can see in the code below it favors the right side because of the button being small. however I would like to have the splitter always appear in the middle of the window as shown in image two.
Current
Goal
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
# formatting
self.resize(550, 400)
self.setWindowTitle("Cameras")
# widgets
self.ListA = QtGui.QTreeWidget()
self.ListB = QtGui.QTreeWidget()
self.Button = QtGui.QPushButton()
# layout Splitter
self.mainLayout = QtGui.QHBoxLayout(self)
self.mainLayout.setContentsMargins(5,5,5,5)
self.leftPanel = QtGui.QFrame(self)
# self.leftPanel.setFrameShape(QtGui.QFrame.StyledPanel)
self.leftPanelLayout = QtGui.QHBoxLayout(self.leftPanel)
self.leftPanelLayout.setContentsMargins(0,0,0,0)
self.leftPanelLayout.addWidget(self.ListA)
self.rightPanel = QtGui.QFrame(self)
# self.rightPanel.setFrameShape(QtGui.QFrame.StyledPanel)
self.rightPanelLayout = QtGui.QHBoxLayout(self.rightPanel)
self.rightPanelLayout.setContentsMargins(0,0,0,0)
self.rightPanelLayout.addWidget(self.Button)
self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
self.splitter.addWidget(self.leftPanel)
self.splitter.addWidget(self.rightPanel)
self.mainLayout.addWidget(self.splitter)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QtGui.QSplitter')
self.show()
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Bam! got it.
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
# formatting
self.resize(1000, 1000)
self.setWindowTitle("Cameras")
# widgets
self.ListA = QtGui.QTreeWidget()
self.ListB = QtGui.QTreeWidget()
self.Button = QtGui.QPushButton()
# layout Splitter
# QHBoxLayout
self.mainLayout = QtGui.QGridLayout(self)
self.mainLayout.setContentsMargins(5,5,5,5)
self.leftPanel = QtGui.QFrame(self)
# self.leftPanel.setFrameShape(QtGui.QFrame.StyledPanel)
self.leftPanelLayout = QtGui.QHBoxLayout(self.leftPanel)
self.leftPanelLayout.setContentsMargins(0,0,0,0)
self.leftPanelLayout.addWidget(self.ListA)
self.rightPanel = QtGui.QFrame(self)
# self.rightPanel.setFrameShape(QtGui.QFrame.StyledPanel)
self.rightPanelLayout = QtGui.QHBoxLayout(self.rightPanel)
self.rightPanelLayout.setContentsMargins(0,0,0,0)
self.rightPanelLayout.addWidget(self.Button)
self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
self.splitter.addWidget(self.leftPanel)
self.splitter.addWidget(self.rightPanel)
self.splitter.setCollapsible(0,False)
self.splitter.setCollapsible(1,False)
self.mainLayout.addWidget(self.splitter,0,0)
self.setWindowTitle('QtGui.QSplitter')
self.show()
self.set_panel_sizes(self.splitter)
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
def set_panel_sizes(self, ctrl):
width = ctrl.frameSize().width() / 2.0
ctrl.setSizes( [width,width] )
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Related
I want to move the position of groupbox in window but menubar and tools are also moved.
how do I fix it and just move the position of GroupBox? (The move command does not work)
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
V = QtWidgets.QApplication.desktop().screenGeometry()
h, w, x, y = V.height(), V.width(), 1000, 600
self.setGeometry(h/4, w/20, x, y)
self.setFixedSize(x, y)
self.setWindowTitle('Main Window')
OpenFile = QtWidgets.QAction('Open', self)
OpenFile.setShortcut('Ctrl+O')
OpenFile.setStatusTip('Open Restore File...')
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
fileMenu.addAction(OpenFile)
self.home()
def home(self):
self.tools_in_home()
self.show()
def tools_in_home(self):
self.groupBox = QtWidgets.QGroupBox('Test')
self.groupBox.setFixedSize(800, 400)
self.setContentsMargins(100, 100, 100, 100) # <=== HERE
hBoxLayout = QtWidgets.QHBoxLayout()
self.groupBox.setLayout(hBoxLayout)
self.setCentralWidget(self.groupBox)
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
if __name__ == '__main__':
run()
Try it:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
V = QtWidgets.QApplication.desktop().screenGeometry()
h, w, x, y = V.height(), V.width(), 1000, 600
self.setGeometry(h/4, w/20, x, y)
self.setFixedSize(x, y)
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
centralWidget = QtWidgets.QWidget()
self.setCentralWidget(centralWidget)
self.grid = QtWidgets.QGridLayout(centralWidget)
left, top, right, bottom = 100, 100, 100, 100
centralWidget.setContentsMargins(left, top, right, bottom)
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OpenFile = QtWidgets.QAction('Open', self)
OpenFile.setShortcut('Ctrl+O')
OpenFile.setStatusTip('Open Restore File...')
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
fileMenu.addAction(OpenFile)
self.home()
def home(self):
self.tools_in_home()
self.show()
def tools_in_home(self):
self.groupBox = QtWidgets.QGroupBox('Test')
self.groupBox.setFixedSize(800, 400)
self.grid.addWidget(self.groupBox)
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = Window()
GUI.setWindowTitle('Main Window')
sys.exit(app.exec_())
if __name__ == '__main__':
run()
Update
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
V = QtWidgets.QApplication.desktop().screenGeometry()
h, w, x, y = V.height(), V.width(), 1000, 670 # 600
self.setGeometry(h/4, w/20, x, y)
self.setFixedSize(x, y)
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
centralWidget = QtWidgets.QWidget()
self.setCentralWidget(centralWidget)
self.grid = QtWidgets.QGridLayout(centralWidget)
left, top, right, bottom = 50, 50, 50, 50
centralWidget.setContentsMargins(left, top, right, bottom)
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OpenFile = QtWidgets.QAction('Open', self)
OpenFile.setShortcut('Ctrl+O')
OpenFile.setStatusTip('Open Restore File...')
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
fileMenu.addAction(OpenFile)
self.home()
def home(self):
self.tools_in_home()
self.show()
def tools_in_home(self):
self.groupBox = QtWidgets.QGroupBox('Test')
self.groupBox.setFixedSize(400, 200) #(800, 400)
self.grid.addWidget(self.groupBox, 0, 0)
# +++ vvvvv
self.groupBox1 = QtWidgets.QGroupBox('Test 1')
self.groupBox1.setFixedSize(400, 100)
self.groupBox2 = QtWidgets.QGroupBox('Test 2')
self.groupBox2.setFixedSize(400, 100)
self.groupBox3 = QtWidgets.QGroupBox('Test 3')
self.groupBox3.setFixedSize(400, 200)
self.grid.addWidget(self.groupBox1, 1, 0)
self.grid.addWidget(self.groupBox2, 1, 1)
self.grid.addWidget(self.groupBox3, 2, 1)
# +++ ^^^^^
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = Window()
GUI.setWindowTitle('Main Window')
sys.exit(app.exec_())
if __name__ == '__main__':
run()
Here is my code, i want to display the minimum and maximum range values for slider.I tried many ways but i didn't get anything.Can anyone please help me how to display the slider as shown in the bellow image.
Given bellow is my code:
from pyface.qt import QtGui, QtCore
import sys
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.vbox = QtGui.QVBoxLayout()
self.label3 = QtGui.QLabel()
self.slider = QtGui.QSlider()
self.slider.setMinimum(0)
self.slider.setMaximum(100)
self.slider.setTickPosition(QtGui.QSlider.TicksLeft)
self.slider.setOrientation(QtCore.Qt.Horizontal)
self.slider.setOrientation(QtCore.Qt.Horizontal)
self.vbox.addWidget(self.slider,QtCore.Qt.AlignBottom)
self.vbox.addWidget(self.label3)
self.setLayout(self.vbox)
self.setGeometry(300, 300, 300, 150)
self.slider.valueChanged.connect(self.valuechange)
self.show()
def valuechange(self):
txt = str(self.slider.value())
self.label3.setText(txt)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You can use 2 QLabels with QHBoxLayout and QVBoxLayout:
from pyface.qt import QtGui, QtCore
import sys
class Slider(QtGui.QSlider):
minimumChanged = QtCore.Signal(int)
maximumChanged = QtCore.Signal(int)
def setMinimum(self, minimum):
self.minimumChanged.emit(minimum)
super(Slider, self).setMinimum(minimum)
def setMaximum(self, maximum):
self.maximumChanged.emit(maximum)
super(Slider, self).setMaximum(maximum)
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.label = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
self.slider = Slider(tickPosition=QtGui.QSlider.TicksLeft,
orientation=QtCore.Qt.Horizontal)
slider_vbox = QtGui.QVBoxLayout()
slider_hbox = QtGui.QHBoxLayout()
slider_hbox.setContentsMargins(0, 0, 0, 0)
slider_vbox.setContentsMargins(0, 0, 0, 0)
slider_vbox.setSpacing(0)
label_minimum = QtGui.QLabel(alignment=QtCore.Qt.AlignLeft)
self.slider.minimumChanged.connect(label_minimum.setNum)
label_maximum = QtGui.QLabel(alignment=QtCore.Qt.AlignRight)
self.slider.maximumChanged.connect(label_maximum.setNum)
slider_vbox.addWidget(self.slider)
slider_vbox.addLayout(slider_hbox)
slider_hbox.addWidget(label_minimum, QtCore.Qt.AlignLeft)
slider_hbox.addWidget(label_maximum, QtCore.Qt.AlignRight)
slider_vbox.addStretch()
self.slider.setMinimum(0)
self.slider.setMaximum(100)
vbox = QtGui.QVBoxLayout(self)
vbox.addLayout(slider_vbox)
vbox.addWidget(self.label)
self.setGeometry(300, 300, 300, 150)
self.slider.valueChanged.connect(self.label.setNum)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I want to emit a signal from a QGraphicsItem when it is doubled-clicked, in order to change a widget in the main window. The graphics-scene/-item does not provide an emit() method, but I was just wondering if there is an alternate way to do this. The code below has a function within a QGraphicsView class that will print to the terminal when an item is double-clicked. How can I make that into a slot/signal instead (if QGraphicsItem does not support signal/slots)?
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class MyFrame(QGraphicsView):
def __init__( self, parent = None ):
super(MyFrame, self).__init__(parent)
scene = QGraphicsScene()
self.setScene(scene)
self.setFixedSize(500, 500)
pen = QPen(QColor(Qt.green))
brush = QBrush(pen.color().darker(150))
item = scene.addEllipse(0, 0, 45, 45, pen, brush)
item.setPos(0,0)
def mouseDoubleClickEvent(self, event):
print("Circle Clicked!")
# this double click event prints to terminal but how to setup
# signal/slot to update the QWidget QLabel text instead?
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
top = QLabel("Double Click Green Circle (Howto change this QWidget Label with signals?)")
bottom = MyFrame()
splitter = QSplitter(Qt.Vertical)
splitter.addWidget(top)
splitter.addWidget(bottom)
hbox.addWidget(splitter)
self.setLayout(hbox)
self.setGeometry(0, 0, 500, 600)
self.show()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Below is a simple example showing one way to emit signals from a graphics-item. This defines a custom signal on a subclass of QGraphicsScene and then uses the scene() method of graphics-items to emit it:
import sys
from PySide import QtCore, QtGui
class GraphicsScene(QtGui.QGraphicsScene):
itemDoubleClicked = QtCore.Signal(object)
class GraphicsRectangle(QtGui.QGraphicsRectItem):
def mouseDoubleClickEvent(self, event):
self.scene().itemDoubleClicked.emit(self)
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.view = QtGui.QGraphicsView()
self.scene = GraphicsScene(self)
self.view.setScene(self.scene)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.view)
for i in range(1, 4):
self.scene.addItem(GraphicsRectangle(50 * i, 50 * i, 20, 20))
self.scene.itemDoubleClicked.connect(self.handleItemDoubleClicked)
def handleItemDoubleClicked(self, item):
print(item.boundingRect())
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 300, 200)
window.show()
sys.exit(app.exec_())
UPDATE:
Below is a an example based on the code in your question. The basic idea is the same: define a custom signal on an available QObject (the graphics-view in this case), and use that to emit the double-click notification.
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class MyFrame(QGraphicsView):
itemDoubleClicked = Signal(object)
def __init__(self, parent=None):
super(MyFrame, self).__init__(parent)
scene = QGraphicsScene()
self.setScene(scene)
self.setFixedSize(500, 500)
for i, color in enumerate('red blue green'.split()):
pen = QPen(QColor(color))
brush = QBrush(pen.color().darker(150))
item = scene.addEllipse(i * 50, i * 50, 45, 45, pen, brush)
item.setData(0, color.upper())
def mouseDoubleClickEvent(self, event):
item = self.itemAt(event.pos())
if item is not None:
self.itemDoubleClicked.emit(item)
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
top = QLabel('Double Click a Circle')
bottom = MyFrame()
bottom.itemDoubleClicked.connect(
lambda item, top=top:
top.setText('Double Clicked: %s' % item.data(0)))
splitter = QSplitter(Qt.Vertical)
splitter.addWidget(top)
splitter.addWidget(bottom)
hbox.addWidget(splitter)
self.setLayout(hbox)
self.setGeometry(0, 0, 500, 600)
self.show()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I'm making a small PyQt4 application and so far I have this:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.initUi()
def initUi(self):
self.setFixedSize(500, 300)
self.text = QtGui.QLabel('Text to be changed')
self.text2 = QtGui.QLabel('Also to be changed')
self.textCheckBox = QtGui.QCheckBox()
self.textCheckBox.stateChanged.connect(self.check(self.text))
self.show()
def check(self, state, theText):
if state == QtCore.Qt.Checked:
theText.setStyleSheet("color: pink")
else:
theText.setStyleSheet("color: black")
def main():
q = QtGui.QApplication(sys.argv)
w = Window()
sys.exit(q.exec())
if __name__=='__main__':
main()
The problem is, my .connect line doesn't seem to want to recognize the parameter I pass into it and I'm not sure why. I can't reference the text directly in the function as I would like to pass many QLabel arguments to it hence I made a theText parameter. I'd appreciate any help.
Version that works without parameters:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.initUi()
def initUi(self):
self.setFixedSize(500, 300)
self.text = QtGui.QLabel('Text to be changed')
self.text2 = QtGui.QLabel('Also to be changed')
self.textCheckBox = QtGui.QCheckBox()
self.textCheckBox.stateChanged.connect(self.check)
self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.text)
self.hbox.addWidget(self.textCheckBox)
self.setLayout(self.hbox)
self.show()
def check(self, state):
if state == QtCore.Qt.Checked:
self.text.setStyleSheet("color: pink")
else:
self.text.setStyleSheet("color: black")
def main():
q = QtGui.QApplication(sys.argv)
w = Window()
sys.exit(q.exec())
if __name__=='__main__':
main()
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.initUi()
def initUi(self):
self.setFixedSize(500, 300)
self.text = QtGui.QLabel('Text to be changed')
self.text2 = QtGui.QLabel('Also to be changed')
self.textCheckBox = QtGui.QCheckBox()
test = self.textCheckBox.checkState()
self.textCheckBox.stateChanged.connect(lambda: self.check(self.textCheckBox.checkState(), self.text))
self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.text)
self.hbox.addWidget(self.textCheckBox)
self.setLayout(self.hbox)
self.show()
def check(self, state, theText):
if state == QtCore.Qt.Checked:
self.setWindowTitle("test")
else:
theText.setStyleSheet("color: black")
def main():
q = QtGui.QApplication(sys.argv)
w = Window()
sys.exit(q.exec())
if __name__=='__main__':
main()
Seem to have fixed it. Not too sure why it works though.
I have some buttons that I want to keep seperated from other elements in a widget. I'd like to put a frame around them but I'm not sure how.
import sys
from PyQt4 import QtGui, QtCore
class PasswordPrompt(QtGui.QWidget):
def __init__(self):
super(PasswordPrompt, self).__init__()
self.initUi()
def initUi(self):
self.setFixedSize(1000, 500)
self.setWindowTitle('Please enter the password...')
hbox = QtGui.QHBoxLayout()
vbox = QtGui.QVBoxLayout()
btn1 = QtGui.QPushButton("1")
btn2 = QtGui.QPushButton("2")
btn3 = QtGui.QPushButton("3")
vbox.addWidget(btn1)
vbox.addWidget(btn2)
vbox.addWidget(btn3)
vbox.setSpacing(0)
hbox.addLayout(vbox)
self.setLayout(hbox)
self.center()
self.show()
def center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def main():
application = QtGui.QApplication(sys.argv)
p = PasswordPrompt()
sys.exit(application.exec())
if __name__=='__main__':
main()
As an example, how would I add a black frame around these buttons? Thanks for any help.
QGroupBox can be used to set the outlines.
import sys
from PyQt4 import QtGui, QtCore
class PasswordPrompt(QtGui.QWidget):
def __init__(self):
super(PasswordPrompt, self).__init__()
self.initUi()
def initUi(self):
layout = QtGui.QVBoxLayout()
self.setLayout(layout)
groupBox1 = QtGui.QGroupBox('Button 1')
groupBox1Layout=QtGui.QVBoxLayout()
groupBox1.setLayout(groupBox1Layout)
btn1 = QtGui.QPushButton("1")
groupBox1Layout.addWidget(btn1)
groupBox2 = QtGui.QGroupBox('Button 2')
groupBox2Layout=QtGui.QVBoxLayout()
groupBox2.setLayout(groupBox2Layout)
btn2 = QtGui.QPushButton("2")
groupBox2Layout.addWidget(btn2)
groupBox3 = QtGui.QGroupBox('Button 3')
groupBox3Layout=QtGui.QVBoxLayout()
groupBox3.setLayout(groupBox3Layout)
btn3 = QtGui.QPushButton("3")
groupBox3Layout.addWidget(btn3)
layout.addWidget(groupBox1)
layout.addWidget(groupBox2)
layout.addWidget(groupBox3)
self.resize(300, 100)
self.show()
def main():
application = QtGui.QApplication(sys.argv)
p = PasswordPrompt()
sys.exit(application.exec_())
if __name__=='__main__':
main()