pySide QComboBox refresh - python

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)

Related

How to properly initialize a QWizard page?

I am having problems with sending data from one QWizard page to the next. I'm using a variable my_name of QWizard object as a container.
My approach is: whenever I change text of QLineEdit on Page1, the variable my_name of my QWizard object changes. And whenever I click Next button on Page1, Page2 is initialized using the method QWizard.initializePage(2). But the QLabel object on Page2 is not update based on the my_name variable of QWizard object. Even though I have initialized the Page2 also. What is wrong with my approach?
My code is:
import sys
from PyQt5.QtWidgets import *
class Window(QWizard):
def __init__(self):
super(Window, self).__init__()
self.firstPage = MainPage(parent=self)
self.my_name = 'Random'
self.secondPage = Page2(parent=self)
self.addPage(self.firstPage)
self.button(QWizard.NextButton).clicked.connect(lambda: self.initializePage(2))
self.addPage(self.secondPage)
class MainPage(QWizardPage):
def __init__(self, parent=None):
self.Parent = parent
super(MainPage, self).__init__(parent)
self.setTitle("Plz input your name?")
self.NameLabel = QLabel("&Name:")
self.NameLineEdit = QLineEdit()
self.NameLineEdit.textChanged.connect(self.assign)
self.NameLabel.setBuddy(self.NameLineEdit)
layout = QHBoxLayout()
layout.addWidget(self.NameLabel)
layout.addWidget(self.NameLineEdit)
self.setLayout(layout)
def assign(self):
self.Parent.my_name = self.NameLineEdit.text()
print(f'Parent text is: {self.Parent.my_name}')
class Page2(QWizardPage):
def __init__(self, parent=None):
super(Page2, self).__init__()
self.Parent = parent
vbox = QVBoxLayout()
label = QLabel()
label.setText(f'My name is : {self.Parent.my_name}')
vbox.addWidget(label)
self.setLayout(vbox)
def main():
app = QApplication(sys.argv)
app.setStyle('plastique')
window = Window()
window.setWizardStyle(1)
window.show()
app.exec_()
if __name__ == "__main__":
sys.exit(main())
Changing the value of the variable "my_name" does not change what the QLabel shows since QLabel copies the text. On the other hand you should not call initializePage(2) since it is a protected method that is called internally. The solution is to override the initializePage method of the QWizardPage:
class Page2(QWizardPage):
def __init__(self, parent=None):
super(Page2, self).__init__()
self.Parent = parent
vbox = QVBoxLayout(self)
self.label = QLabel()
self.label.setText(f'My name is : {self.Parent.my_name}')
vbox.addWidget(self.label)
def initializePage(self):
self.label.setText(f'My name is : {self.Parent.my_name}')
Although I see that you are reinventing the wheel since there is already that characteristic registering the fields:
class Window(QWizard):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.firstPage = MainPage()
self.secondPage = Page2()
self.addPage(self.firstPage)
self.addPage(self.secondPage)
class MainPage(QWizardPage):
def __init__(self, parent=None):
super(MainPage, self).__init__(parent)
self.setTitle("Plz input your name?")
self.NameLabel = QLabel("&Name:")
self.NameLineEdit = QLineEdit()
self.NameLabel.setBuddy(self.NameLineEdit)
layout = QHBoxLayout(self)
layout.addWidget(self.NameLabel)
layout.addWidget(self.NameLineEdit)
self.registerField("my_name", self.NameLineEdit)
class Page2(QWizardPage):
def __init__(self, parent=None):
super(Page2, self).__init__(parent)
vbox = QVBoxLayout(self)
self.label = QLabel()
vbox.addWidget(self.label)
def initializePage(self):
self.label.setText(f'My name is : {self.field("my_name")}')
super(Page2, self).initializePage()

Pyqt Add the same widget object in different layouts

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

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

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

How do I get the contents of a row of QTableWidget with click on the button in python and PyQt

If i click on any of the edit buttons, how do I get the contents of cells in the same row?
class EditButtonsWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(EditButtonsWidget,self).__init__(parent)
btnsave = QtGui.QPushButton('Save')
btnedit = QtGui.QPushButton('edit')
btndelete = QtGui.QPushButton('delete')
layout = QtGui.QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.setSpacing(0)
layout.addWidget(btnsave)
layout.addWidget(btnedit)
layout.addWidget(btndelete)
self.setLayout(layout)
class MainForm(QDialog):
def __init__(self,parent=None):
super(MainForm,self).__init__(parent)
self.resize(400,200)
self.tableWidget = QTableWidget()
layout = QVBoxLayout()
layout.addWidget( self.tableWidget)
self.setLayout(layout)
self.init_main_form()
def bb(self):
button = QtGui.QApplication.focusWidget()
index = self.tableWidget.indexAt(button.pos())
if index.isValid():
print(index.row(), index.column())
def init_main_form(self):
data =[['a','b','c','d'],['z','y','x','w']]
self.tableWidget.setColumnCount(5)
for row in data:
inx = data.index(row)
self.tableWidget.insertRow(inx)
self.tableWidget.setItem(inx,0,QTableWidgetItem(str(row[0])))
self.tableWidget.setItem(inx,1,QTableWidgetItem(str(row[1])))
self.tableWidget.setItem(inx,2,QTableWidgetItem(str(row[2])))
self.tableWidget.setItem(inx,3,QTableWidgetItem(str(row[3])))
self.tableWidget.setCellWidget(inx,4,EditButtonsWidget())
def main():
app = QApplication(sys.argv)
main_form = MainForm()
main_form.show()
app.exec_()
if __name__ == '__main__':
main()
Implemented signals and slots and now you will get idea how you can handle it
And mixing imports is not really a good idea
from PyQt4 import QtCore, QtGui
import sys
class EditButtonsWidget(QtGui.QWidget):
editCalled = QtCore.pyqtSignal(str)
def __init__(self, row, col, parent=None,):
super(EditButtonsWidget,self).__init__(parent)
self.row = row
self.col = col
self.parent = parent
btnsave = QtGui.QPushButton('Save')
btnedit = QtGui.QPushButton('edit')
btndelete = QtGui.QPushButton('delete')
layout = QtGui.QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.setSpacing(0)
layout.addWidget(btnsave)
layout.addWidget(btnedit)
layout.addWidget(btndelete)
self.setLayout(layout)
btnedit.clicked.connect(self.getAllCellVal)
#QtCore.pyqtSlot()
def getAllCellVal(self):
itmVal = {}
for col in range(0, 4):
itm = self.parent.item(self.row, col).text()
itmVal[col] = str(itm)
if itmVal:
self.editCalled.emit(str(itmVal))
class MainForm(QtGui.QDialog):
def __init__(self,parent=None):
super(MainForm,self).__init__(parent)
self.resize(400,200)
self.tableWidget = QtGui.QTableWidget()
layout = QtGui.QVBoxLayout()
layout.addWidget( self.tableWidget)
self.setLayout(layout)
self.init_main_form()
def bb(self):
button = QtGui.QApplication.focusWidget()
index = self.tableWidget.indexAt(button.pos())
if index.isValid():
print(index.row(), index.column())
def printEditVal(self, values):
print values
def init_main_form(self):
data =[['a','b','c','d'],['z','y','x','w']]
self.tableWidget.setColumnCount(5)
for row in data:
inx = data.index(row)
self.tableWidget.insertRow(inx)
self.tableWidget.setItem(inx,0,QtGui.QTableWidgetItem(str(row[0])))
self.tableWidget.setItem(inx,1,QtGui.QTableWidgetItem(str(row[1])))
self.tableWidget.setItem(inx,2,QtGui.QTableWidgetItem(str(row[2])))
self.tableWidget.setItem(inx,3,QtGui.QTableWidgetItem(str(row[3])))
buttonWid = EditButtonsWidget(inx,4, self.tableWidget)
buttonWid.editCalled.connect(self.printEditVal)
self.tableWidget.setCellWidget(inx,4,buttonWid)
def main():
app = QtGui.QApplication(sys.argv)
main_form = MainForm()
main_form.show()
app.exec_()
if __name__ == '__main__':
main()

Categories

Resources