Pyqt Add the same widget object in different layouts - python

I now want to use Pyqt to design such a set of logic, add two different widget in two layouts, these two different widget use the same widget, because I want to share the data of the same widget in different layouts But unfortunately my design failed, in the case, I can't show two PYQT on the display,can anyone help me
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Series(QWidget):
def __init__(self):
super(Series, self).__init__()
self.lb = QLabel('PYQT')
class SeriesHBox1(QWidget):
def __init__(self, series):
super(SeriesHBox1, self).__init__()
self.vbox = QVBoxLayout()
self.setLayout(self.vbox)
self.vbox.addWidget(series.lb)
class SeriesHBox2(QWidget):
def __init__(self, series):
super(SeriesHBox2, self).__init__()
self.hbox = QHBoxLayout()
self.setLayout(self.hbox)
self.hbox.addWidget(series.lb)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 500, 300)
box = QHBoxLayout()
self.setLayout(box)
box1 = QHBoxLayout()
box2 = QHBoxLayout()
box.addLayout(box1)
box.addLayout(box2)
series = Series()
box1.addWidget(SeriesHBox1(series))
box2.addWidget(SeriesHBox2(series))
# box2.addWidget(SeriesHBox2(Series()))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

A widget cannot be in two different widgets at the same time. So, you have to create two Series instances (one for each SeriesHBox).
The simpliest way to share the data (let's say the content of your label) is to extract the state (the text) in another object that will be shared by the Series instance and will update them when the content has changed.
A quick example :
class SeriesModel(QObject):
def __init__(self, parent=None):
super(SeriesModel, self).__init__(parent)
self._content = "PYQT"
contentChanged = pyqtSignal(str)
#pyqtProperty(str, notify=contentChanged)
def content(self):
return self._content
#content.setter
def content(self, value):
self._content = value
class Series(QWidget):
def __init__(self, model):
super(Series, self).__init__()
self.model = model
self.lb = QLabel(model.content)
self.model.contentChanged.connect(self.lb.setText)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 500, 300)
box = QHBoxLayout()
self.setLayout(box)
box1 = QHBoxLayout()
box2 = QHBoxLayout()
box.addLayout(box1)
box.addLayout(box2)
model = SeriesModel(self)
series1 = Series(model)
series2 = Series(model)
box1.addWidget(SeriesHBox1(series1))
box2.addWidget(SeriesHBox2(series2))
self.show()
If you change the content in SeriesModel the two labels will be updated, also.

Sorry, I have PyQt5.
Create two instances of the Series class
#from PyQt4.QtGui import *
#from PyQt4.QtCore import *
from PyQt5.Qt import *
import random
class Series(QWidget):
def __init__(self):
super(Series, self).__init__()
self.lb = QLabel('PYQT')
self.lb.setStyleSheet("""background-color: {};""".format(
QColor(*random.sample(range(255), 3)).name()
)
)
class SeriesHBox1(QWidget):
def __init__(self, series):
super(SeriesHBox1, self).__init__()
self.vbox = QVBoxLayout()
self.setLayout(self.vbox)
self.vbox.addWidget(series.lb)
class SeriesHBox2(QWidget):
def __init__(self, series):
super(SeriesHBox2, self).__init__()
self.hbox = QHBoxLayout()
self.setLayout(self.hbox)
self.hbox.addWidget(series.lb)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 500, 300)
box = QHBoxLayout()
self.setLayout(box)
box1 = QHBoxLayout()
box2 = QHBoxLayout()
box.addLayout(box1)
box.addLayout(box2)
# series = Series()
series1 = Series() # +
series2 = Series() # +
box1.addWidget(SeriesHBox1(series1))
box2.addWidget(SeriesHBox2(series2))
# box2.addWidget(SeriesHBox2(Series()))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())

This is the answer i want
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Series(QWidget):
def __init__(self, parent=None):
super(Series, self).__init__(parent)
self.lb = QLabel('PYQT')
class SeriesHBox1(QWidget):
def __init__(self, series):
super(SeriesHBox1, self).__init__()
self.vbox = QVBoxLayout()
self.setLayout(self.vbox)
self.vbox.addWidget(series.lb)
class SeriesHBox2(QWidget):
def __init__(self, series):
super(SeriesHBox2, self).__init__()
self.hbox = QHBoxLayout()
self.setLayout(self.hbox)
self.hbox.addWidget(series.lb)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 500, 300)
box = QHBoxLayout()
self.setLayout(box)
box1 = QHBoxLayout()
box2 = QHBoxLayout()
box.addLayout(box1)
box.addLayout(box2)
s = Series()
h = SeriesHBox1(s)
s.__init__(h)
print(s)
box1.addWidget(h)
b = SeriesHBox2(s)
s.__init__(b)
print(s)
box2.addWidget(b)
c = QPushButton()
c.clicked.connect(self.close_a)
d = QPushButton()
d.clicked.connect(self.close_b)
box.addWidget(c)
box.addWidget(d)
self.show()
def close_a(self):
self.a.hide()
def close_b(self):
self.b.hide()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

Related

Pyside- Add Widget from another class

I am trying to build a layout using PySide and I have run into a situation where I would like to keep a widget group in a separate class and call it dynamically.
I looked at few examples from this site which sort of gives me an idea but still I am not able to resolve this issue on my own. Here is the sample code of the layout I am building and it will be very helpful if someone can help me solve this issue.
-Thanks in advance
import PySide2.QtCore as QtCore
import PySide2.QtGui as QtGui
import PySide2.QtWidgets as QtGuiWidgets
class TabDialog(QtGuiWidgets.QDialog):
def __init__(self, parent=None):
super(TabDialog, self).__init__(parent)
argument = "Temp"
calldisplay = display_elements()
tabWidget = QtGuiWidgets.QTabWidget()
tabWidget.addTab(tab1(argument), "tab1")
tabWidget.addTab(tab2(argument), "tab2")
buttonBox = QtGuiWidgets.QDialogButtonBox(
QtGuiWidgets.QDialogButtonBox.Ok | QtGuiWidgets.QDialogButtonBox.Cancel)
buttonBox.accepted.connect(self.accept)
buttonBox.rejected.connect(self.reject)
mainLayout = QtGuiWidgets.QVBoxLayout()
mainLayout.addWidget(tabWidget)
mainLayout.addWidget(buttonBox)
self.setLayout(mainLayout)
self.setWindowTitle("Load All Elements")
self.show()
class tab1(QtGuiWidgets.QWidget):
def __init__(self, calldisplay, parent=None):
super(tab1, self).__init__(parent)
self.layerfilterGroup = QtGuiWidgets.QLabel("Filter 1")
self.peopleGroup = QtGuiWidgets.QGroupBox("Filter 2")
self.dateGroup = QtGuiWidgets.QGroupBox("Filter 3")
self.loadGroup = QtGuiWidgets.QLabel("Load elements")
self.filterGroup = QtGuiWidgets.QGroupBox("Filters")
self.filterGroup.setGeometry(100, 100, 700, 550)
self.filterLayout = QtGuiWidgets.QVBoxLayout()
for key in range(15):
self.btn = QtGuiWidgets.QCheckBox(str(key))
self.btn.setChecked(True)
self.filterLayout.addWidget(self.btn)
self.filterGroup.setLayout(self.filterLayout)
self.filterscroll = QtGuiWidgets.QScrollArea()
self.fslayout = QtGuiWidgets.QVBoxLayout()
self.filterscroll.setWidget(self.filterGroup)
self.fslayout.addWidget(self.filterscroll)
self.artist = ["All", "N/A", "N/A", "N/A"]
self.acombos = QtGuiWidgets.QComboBox()
self.acombos.addItems(self.artist)
self.all_label = QtGuiWidgets.QLabel()
self.all_label.setText('All')
self.all_label.setGeometry(160, 40, 80, 30)
self.late_label = QtGuiWidgets.QLabel()
self.late_label.setText('Latest')
self.late_label.setGeometry(160, 40, 80, 30)
self.dslider = QtGuiWidgets.QSlider(QtCore.Qt.Horizontal, self)
self.artistlayout = QtGuiWidgets.QVBoxLayout()
self.artistlayout.addWidget(self.acombos)
self.peopleGroup.setLayout(self.artistlayout)
self.datelayout = QtGuiWidgets.QHBoxLayout()
self.datelayout.addWidget(self.all_label)
self.datelayout.addWidget(self.dslider)
self.datelayout.addWidget(self.late_label)
self.dateGroup.setLayout(self.datelayout)
self.rgroup = QtGuiWidgets.QGroupBox("Elements")
self.rgroup.setGeometry(100, 100, 700, 750)
self.rlayout = QtGuiWidgets.QVBoxLayout()
########## This is Working###############
for key in range(15):
self.btn = QtGuiWidgets.QCheckBox(str(key))
self.btn.setChecked(True)
self.rlayout.addWidget(self.btn)
self.rgroup.setLayout(self.rlayout)
############ want to do it this way############
#self.rlayout.addWidget(calldisplay)
#self.rgroup.setLayout(self.rlayout)
self.rscroll = QtGuiWidgets.QScrollArea()
self.rlayout = QtGuiWidgets.QVBoxLayout()
self.rscroll.setWidget(self.rgroup)
self.rlayout.addWidget(self.rscroll)
self.mainLayout = QtGuiWidgets.QVBoxLayout()
self.mainLayout.addWidget(self.layerfilterGroup)
self.mainLayout.addWidget(self.filterscroll)
self.mainLayout.addWidget(self.peopleGroup)
self.mainLayout.addWidget(self.dateGroup)
self.mainLayout.addWidget(self.loadGroup)
self.mainLayout.addWidget(self.rscroll)
self.setLayout(self.mainLayout)
##### call this class dynamically######
class display_elements(QtGuiWidgets.QWidget):
def __init__(self, parent=None):
super(display_elements, self).__init__(parent)
self.rgroup = QtGuiWidgets.QGroupBox("Available Elements")
self.rgroup.setGeometry(400, 400, 700, 750)
self.rlayout = QtGuiWidgets.QVBoxLayout()
for key in range(15):
self.btn = QtGuiWidgets.QCheckBox(str(key))
self.btn.setChecked(True)
self.rlayout.addWidget(self.btn)
self.rgroup.setLayout(self.rlayout)
class tab2(QtGuiWidgets.QWidget):
def __init__(self, fileInfo, parent=None):
super(tab2, self).__init__(parent)
ex = TabDialog()
One of the errors I see is that you point as the first argument of tab1 to calldisplay that I guess is the widget you want to set but you are passing it as an argument to "argument" which is a string, so a first change is to change it .
On the other hand if you are going to use layouts forget about setGeometry() since now the geometry is handled by the QLayouts.
And finally, your display_elements class is not implemented correctly since the rgroup is not added to the widget and for this you must use a layout.
Considering the above, the solution is the following:
import PySide2.QtCore as QtCore
import PySide2.QtGui as QtGui
import PySide2.QtWidgets as QtGuiWidgets
class TabDialog(QtGuiWidgets.QDialog):
def __init__(self, parent=None):
super(TabDialog, self).__init__(parent)
argument = "Temp"
calldisplay = display_elements()
tabWidget = QtGuiWidgets.QTabWidget()
tabWidget.addTab(tab1(calldisplay), "tab1")
tabWidget.addTab(tab2(argument), "tab2")
buttonBox = QtGuiWidgets.QDialogButtonBox(
QtGuiWidgets.QDialogButtonBox.Ok | QtGuiWidgets.QDialogButtonBox.Cancel)
buttonBox.accepted.connect(self.accept)
buttonBox.rejected.connect(self.reject)
mainLayout = QtGuiWidgets.QVBoxLayout()
mainLayout.addWidget(tabWidget)
mainLayout.addWidget(buttonBox)
self.setLayout(mainLayout)
self.setWindowTitle("Load All Elements")
self.show()
class tab1(QtGuiWidgets.QWidget):
def __init__(self, calldisplay, parent=None):
super(tab1, self).__init__(parent)
self.layerfilterGroup = QtGuiWidgets.QLabel("Filter 1")
self.peopleGroup = QtGuiWidgets.QGroupBox("Filter 2")
self.dateGroup = QtGuiWidgets.QGroupBox("Filter 3")
self.loadGroup = QtGuiWidgets.QLabel("Load elements")
self.filterGroup = QtGuiWidgets.QGroupBox("Filters")
self.filterGroup.setGeometry(100, 100, 700, 550)
self.filterLayout = QtGuiWidgets.QVBoxLayout()
for key in range(15):
self.btn = QtGuiWidgets.QCheckBox(str(key))
self.btn.setChecked(True)
self.filterLayout.addWidget(self.btn)
self.filterGroup.setLayout(self.filterLayout)
self.filterscroll = QtGuiWidgets.QScrollArea()
self.fslayout = QtGuiWidgets.QVBoxLayout()
self.filterscroll.setWidget(self.filterGroup)
self.fslayout.addWidget(self.filterscroll)
self.artist = ["All", "N/A", "N/A", "N/A"]
self.acombos = QtGuiWidgets.QComboBox()
self.acombos.addItems(self.artist)
self.all_label = QtGuiWidgets.QLabel()
self.all_label.setText('All')
self.all_label.setGeometry(160, 40, 80, 30)
self.late_label = QtGuiWidgets.QLabel()
self.late_label.setText('Latest')
self.late_label.setGeometry(160, 40, 80, 30)
self.dslider = QtGuiWidgets.QSlider(QtCore.Qt.Horizontal, self)
self.artistlayout = QtGuiWidgets.QVBoxLayout()
self.artistlayout.addWidget(self.acombos)
self.peopleGroup.setLayout(self.artistlayout)
self.datelayout = QtGuiWidgets.QHBoxLayout()
self.datelayout.addWidget(self.all_label)
self.datelayout.addWidget(self.dslider)
self.datelayout.addWidget(self.late_label)
self.dateGroup.setLayout(self.datelayout)
self.rgroup = QtGuiWidgets.QGroupBox("Elements")
self.rlayout = QtGuiWidgets.QVBoxLayout()
self.rlayout.addWidget(calldisplay)
self.rgroup.setLayout(self.rlayout)
self.rscroll = QtGuiWidgets.QScrollArea()
self.rlayout = QtGuiWidgets.QVBoxLayout()
self.rscroll.setWidget(self.rgroup)
self.rlayout.addWidget(self.rscroll)
self.mainLayout = QtGuiWidgets.QVBoxLayout()
self.mainLayout.addWidget(self.layerfilterGroup)
self.mainLayout.addWidget(self.filterscroll)
self.mainLayout.addWidget(self.peopleGroup)
self.mainLayout.addWidget(self.dateGroup)
self.mainLayout.addWidget(self.loadGroup)
self.mainLayout.addWidget(self.rscroll)
self.setLayout(self.mainLayout)
class display_elements(QtGuiWidgets.QWidget):
def __init__(self, parent=None):
super(display_elements, self).__init__(parent)
self.rgroup = QtGuiWidgets.QGroupBox("Available Elements")
lay = QtGuiWidgets.QVBoxLayout()
for key in range(15):
btn = QtGuiWidgets.QCheckBox(str(key))
btn.setChecked(True)
lay.addWidget(btn)
rlayout = QtGuiWidgets.QVBoxLayout(self)
rlayout.setContentsMargins(0, 0, 0, 0)
rlayout.addWidget(self.rgroup)
self.rgroup.setLayout(lay)
class tab2(QtGuiWidgets.QWidget):
def __init__(self, fileInfo, parent=None):
super(tab2, self).__init__(parent)
if __name__ == '__main__':
import sys
app = QtGuiWidgets.QApplication(sys.argv)
ex = TabDialog()
ex.show()
sys.exit(app.exec_())

how to display the range values in slider

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()

PYQT - Qframe - Show/Hide without shifting the rest of the window

I got the following Test-GUI.
There is a left layout and a right layout where i want to put buttons and other things onto. The button on the right should make a QFrame unhide or hide and all the widgets in it. This works.
But after the first two clicks, the layout is different.
The TableWidget on the left layout gets resized and the button is a bit more south.
Is there an easy way to fix this?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.main_widget = MainWidget()
self.setCentralWidget(self.main_widget)
self.show()
class MainWidget(QWidget):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout()
self.tab_widget = TabWidget()
self.debugger = Dialog()
self.layout.addWidget(self.tab_widget)
self.layout.addWidget(self.debugger)
self.setLayout(self.layout)
class TabWidget(QTabWidget):
def __init__(self):
super().__init__()
self.tab1 = Tab_1()
self.addTab(self.tab1, "Tab1")
class Tab_1(QWidget):
def __init__(self):
super().__init__()
# LEFT LAYOUT BEGIN
self.table = QTableWidget()
self.table.setRowCount(1)
self.table.setColumnCount(2)
self.table.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)
self.table.resizeColumnsToContents()
left_hlayout = QHBoxLayout()
left_hlayout.addWidget(self.table)
# # LEFT LAYOUT END
#
# # RIGHT LAYOUT BEGIN
self.button_options = QPushButton('Options')
self.button_options.setCheckable(True)
self.button_options.toggled.connect(self.option_pressed)
right_vlayout = QVBoxLayout()
right_vlayout.addWidget(self.button_options)
# # RIGHT LAYOUT END
# MAIN LAYOUT BEGING
self.main_layout = QVBoxLayout()
self.horizontal_layout = QHBoxLayout()
self.horizontal_layout.addLayout(left_hlayout)
self.horizontal_layout.addLayout(right_vlayout)
self.main_layout.addLayout(self.horizontal_layout)
self.option = Options()
self.main_layout.addWidget(self.option)
self.setLayout(self.main_layout)
# MAIN LAYOUT END
def option_pressed(self):
if self.button_options.isChecked():
self.option.setVisible(True)
else:
self.option.setVisible(False)
class Options(QFrame):
def __init__(self):
super().__init__()
self.hide()
self.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
self.options_layout = QFormLayout()
self.options_label = QLabel('One')
self.options_lineedit = QLineEdit('Two')
self.options_layout.addRow(self.options_label, self.options_lineedit)
self.setLayout(self.options_layout)
class Dialog(QPlainTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedHeight(100)
pal = QPalette()
bgc = QColor(210, 210, 210)
pal.setColor(QPalette.Base, bgc)
self.setPalette(pal)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())

Passing variables from login form

I'm trying to pass two variables from a login form.Not sure is it the right way. My login form and main one created on QstackedWidget. However, as result I'm getting an empty list. Where is my mistake? Code below:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.central_widget = QStackedWidget()
self.setCentralWidget(self.central_widget)
self.start_screen = Start(self)
self.second_screen = Second(self)
self.central_widget.addWidget(self.start_screen)
self.central_widget.addWidget(self.second_screen)
self.central_widget.setCurrentWidget(self.start_screen)
self.start_screen.clicked.connect(lambda: self.myshort(self.second_screen))
self.second_screen.clicked.connect(lambda: self.central_widget.setCurrentWidget(self.start_screen))
def myshort(self, your_function):
print(self.my_notes[0], self.my_notes[1]) #empty list
return self.central_widget.setCurrentWidget(your_function)
class Start(QWidget):
clicked = pyqtSignal()
def __init__(self, parent=None):
super(Start, self).__init__(parent)
self.textName = QLineEdit(self)
self.textPass = QLineEdit(self)
self.buttonSave = QPushButton('Save Details', self)
layout = QVBoxLayout(self)
layout.addWidget(self.textName)
layout.addWidget(self.textPass)
layout.addWidget(self.buttonSave)
self.buttonSave.clicked.connect(self.my_notes())
def my_notes(self):
MainWindow.my_notes = []
MainWindow.my_notes.append(str(self.textName.text()))
MainWindow.my_notes.append(str(self.textPass.text()))
return self.clicked.emit
class Second(QWidget):
clicked = pyqtSignal()
def __init__(self, parent=None):
super(Second, self).__init__(parent)
layout = QHBoxLayout()
button = QPushButton(text=QString('Back to Start!'))
layout.addWidget(button)
self.setLayout(layout)
button.clicked.connect(self.clicked.emit)
app = QApplication(sys.argv)
myWindow = MainWindow(None)
myWindow.show()
app.exec_()
Try following:
in class Start, method __init__
self.buttonSave.clicked.connect(self.my_notes())
remove bracelets:
self.buttonSave.clicked.connect(self.my_notes)
and in class Start, method my_notes change:
return self.clicked.emit
to:
self.clicked.emit()

force python pyside splitter to start window center

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()

Categories

Resources