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_())
Related
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)
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_())
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()
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()
from PySide import QtGui, QtCore
def listItems():
itemList = ("first","second","etc..")
return itemList
class tabDialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
tabWidget = QtGui.QTabWidget()
tabWidget.addTab(mainTab(), self.tr("Main"))
tabWidget.addTab(secondTab(), self.tr("Second "))
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(tabWidget)
self.setLayout(mainLayout)
class mainTab(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.createGroup = QtGui.QGroupBox(self.tr("Add Item to list"))
self.fileNameEdit = QtGui.QLineEdit(self)
self.fileNameEdit.setPlaceholderText("new item")
self.createButton = QtGui.QPushButton('add',self)
#self.createButton.clicked.connect(self.additemToList)
self.createLayout = QtGui.QGridLayout()
self.createLayout.addWidget(self.fileNameEdit,1,2)
self.createLayout.addWidget(self.createButton,1,3)
self.setLayout(self.createLayout)
self.createGroup.setLayout(self.createLayout)
self.addGroup = QtGui.QGroupBox(self.tr("list items"))
self.projectLabel = QtGui.QLabel(self.tr("item : "))
self.projectListCombo = QtGui.QComboBox(self)
self.projectListCombo.addItems(listItems())
self.addLayout = QtGui.QHBoxLayout()
self.addLayout.addWidget(self.projectLabel)
self.addLayout.addWidget(self.projectListCombo)
self.addGroup.setLayout(self.addLayout)
self.mainLayout = QtGui.QVBoxLayout()
self.mainLayout.addWidget(self.createGroup)
self.mainLayout.addWidget(self.addGroup)
self.setLayout(self.mainLayout)
class secondTab(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.initUI()
def initUI(self):
self.addGroup = QtGui.QGroupBox(self.tr("list items"))
self.projectLabel = QtGui.QLabel(self.tr("item : "))
self.projectListCombo = QtGui.QComboBox(self)
self.projectListCombo.addItems(listItems())
self.addLayout = QtGui.QHBoxLayout()
self.addLayout.addWidget(self.projectLabel)
self.addLayout.addWidget(self.projectListCombo)
self.addGroup.setLayout(self.addLayout)
self.mainLayout = QtGui.QVBoxLayout()
self.mainLayout.addWidget(self.addGroup)
self.setLayout(self.mainLayout)
tabdialog = tabDialog()
tabdialog.show()
Have 2 tabs .In first tab is line for text , button for add item and combo box and in second tab is just combo box.
Both combo box contain same list items.
If add item to list ,need refresh both combo box
You can use QComboBox::addItem:
from PySide import QtGui, QtCore
import sys
def listItems():
itemList = ("first","second","etc..")
return itemList
class tabDialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.tabWidget = QtGui.QTabWidget()
self.mainTab = mainTab(self)
self.secondTab = secondTab(self)
self.tabWidget.addTab(self.mainTab, self.tr("Main"))
self.tabWidget.addTab(self.secondTab, self.tr("Second "))
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(self.tabWidget)
self.setLayout(mainLayout)
def additemToList(self):
item = self.mainTab.fileNameEdit.text()
self.mainTab.projectListCombo.addItem(item)
self.secondTab.projectListCombo.addItem(item)
class mainTab(QtGui.QWidget):
def __init__(self, parent=tabDialog):
QtGui.QWidget.__init__(self, parent)
self.q = 1234
self.createGroup = QtGui.QGroupBox(self.tr("Add Item to list"))
self.fileNameEdit = QtGui.QLineEdit(self)
self.fileNameEdit.setPlaceholderText("new item")
self.createButton = QtGui.QPushButton('add',self)
self.createButton.clicked.connect(parent.additemToList)
self.createLayout = QtGui.QGridLayout()
self.createLayout.addWidget(self.fileNameEdit,1,2)
self.createLayout.addWidget(self.createButton,1,3)
self.setLayout(self.createLayout)
self.createGroup.setLayout(self.createLayout)
self.addGroup = QtGui.QGroupBox(self.tr("list items"))
self.projectLabel = QtGui.QLabel(self.tr("item : "))
self.projectListCombo = QtGui.QComboBox(self)
self.projectListCombo.addItems(listItems())
self.addLayout = QtGui.QHBoxLayout()
self.addLayout.addWidget(self.projectLabel)
self.addLayout.addWidget(self.projectListCombo)
self.addGroup.setLayout(self.addLayout)
self.mainLayout = QtGui.QVBoxLayout()
self.mainLayout.addWidget(self.createGroup)
self.mainLayout.addWidget(self.addGroup)
self.setLayout(self.mainLayout)
class secondTab(QtGui.QWidget):
def __init__(self, parent=tabDialog):
QtGui.QWidget.__init__(self, parent)
self.initUI()
def initUI(self):
self.addGroup = QtGui.QGroupBox(self.tr("list items"))
self.projectLabel = QtGui.QLabel(self.tr("item : "))
self.projectListCombo = QtGui.QComboBox(self)
self.projectListCombo.addItems(listItems())
self.addLayout = QtGui.QHBoxLayout()
self.addLayout.addWidget(self.projectLabel)
self.addLayout.addWidget(self.projectListCombo)
self.addGroup.setLayout(self.addLayout)
self.mainLayout = QtGui.QVBoxLayout()
self.mainLayout.addWidget(self.addGroup)
self.setLayout(self.mainLayout)
def main():
app = QtGui.QApplication(sys.argv)
tabdialog = tabDialog()
tabdialog.show()
app.exec_()
if __name__ == '__main__':
main()
I made the following changes:
mainTab and secondTab in tabDialog are instance attributes
mainTab and secondTab get tabDialog as a parent
Added additemToList in tabDialog
In mainTab.__init__(): self.createButton.clicked.connect(parent.additemToList)