I have a simple pyside QMenu which is being populated with QActions when the application starts. Each menu action represents a class object. How can I create a new instance of the class object based on the Menu Item clicked, and append that new object to a list, which in this example is called
ACTIVE_BAKERS = []
import sys
from PySide import QtGui, QtCore
################################################################################
# Bakers
################################################################################
class Baker(QtGui.QWidget):
def __init__(self, name):
self.name = name
class Baker_John(Baker):
def __init__(self):
Baker.__init__(self, name='John')
class Baker_Amy(Baker):
def __init__(self):
Baker.__init__(self, name='Amy')
class Baker_Makela(Baker):
def __init__(self):
Baker.__init__(self, name='Makela')
class Baker_Jeff(Baker):
def __init__(self):
Baker.__init__(self, name='Jeff')
################################################################################
# Action
################################################################################
class MyAction(QtGui.QAction):
on_action = QtCore.Signal(dict)
def __init__(self, user_info, *args, **kwargs):
super(MyAction, self).__init__(*args, **kwargs)
self.ui = user_info
self.triggered.connect(self.on_triggered)
def on_triggered(self):
print('UI:', self.ui)
self.on_action.emit(self.ui)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(200, 300)
# OBJECTS - variable containing list of class objects created
ACTIVE_BAKERS = []
# CONTROLS
self.ui_items = QtGui.QListView()
self.ui_items.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.ui_items.customContextMenuRequested.connect(self.open_tasks_contextmenu)
self.setCentralWidget(self.ui_items)
self.create_context_menu_ui()
# dynamically create the menu
def create_context_menu_ui(self):
self.add_baker = QtGui.QMenu("Add")
AVAILABLE_BAKERS = [Baker_John(), Baker_Amy(), Baker_Makela(), Baker_Jeff()]
for x in AVAILABLE_BAKERS:
new_action = MyAction(x, x.name, self)
self.add_baker.addAction(new_action)
self._cmenu = QtGui.QMenu()
self._cmenu.addMenu(self.add_baker)
def open_tasks_contextmenu(self, position):
self._cmenu.exec_(QtGui.QCursor.pos())
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
the exec_() method of QMenu returns the selected QAction, through that QAction that is a MyAction that has as attribute ui that gives us the associated Barker object, using the Barker we can access the class through __class__ and create another one object:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(200, 300)
# OBJECTS - variable containing list of class objects created
self.ACTIVE_BAKERS = []
# CONTROLS
self.ui_items = QtGui.QListView()
self.ui_items.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.ui_items.customContextMenuRequested.connect(self.open_tasks_contextmenu)
self.setCentralWidget(self.ui_items)
self.create_context_menu_ui()
# dynamically create the menu
def create_context_menu_ui(self):
self.add_baker = QtGui.QMenu("Add")
AVAILABLE_BAKERS = [Baker_John(), Baker_Amy(), Baker_Makela(), Baker_Jeff()]
for x in AVAILABLE_BAKERS:
new_action = MyAction(x, x.name, self)
self.add_baker.addAction(new_action)
self._cmenu = QtGui.QMenu()
self._cmenu.addMenu(self.add_baker)
def open_tasks_contextmenu(self, position):
action = self._cmenu.exec_(QtGui.QCursor.pos())
if isinstance(action, MyAction):
obj = action.ui.__class__()
if obj not in self.ACTIVE_BAKERS:
self.ACTIVE_BAKERS.append(obj)
Related
I am making a GUI and have a code with several files and uses a controller file to switch between the files. However, I need several of the variables to be available in the other files and also want an own file where I can keep track of the values for all the variables.
I have now instantiated the variables on top of the file, and tried to change the values in the class below, but if I then import to another file it will only give the value which was instantiated first (which is fair since I did not call the class, but is a problem).
Please help.
Under i some of the code:
From file firstwindow
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
LEVELS = 2
NUM_ICE = 4
NUM_CONES = 8
class Login(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.setWindowTitle('First')
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("First")
FirstWindow.setEnabled(True)
FirstWindow.resize(675,776)
FirstWindow.setFocusPolicy(QtCore.Qt.TabFocus)
layout = QtWidgets.QGridLayout()
self.spinBoxNUM_ICE = QtWidgets.QSpinBox()
self.spinBoxNUM_CONES = QtWidgets.QSpinBox()
self.spinBoxLEVELS = QtWidgets.QSpinBox()
layout.addWidget(self.spinBoxNUM_MASTERS,1,2)
layout.addWidget(self.spinBoxNUM_SLAVES,2,2)
layout.addWidget(self.spinBoxPRIORITY_LEVELS,11,2)
#CONTINUE AND QUIT BUTTON
self.QuitButton = QtWidgets.QPushButton("Quit")
self.QContinueButton = QtWidgets.QPushButton("Continue")
#actions
self.QuitButton.clicked.connect(FirstWindow.close)
self.QContinueButton.clicked.connect(self.login)
def login(self):
#global NUM_ICE
self.NUM_ICE = self.spinBoxNUM_ICE.value()
global NUM_CONES
NUM_CONES = self.spinBoxNUM_CONES.value()
global LEVELS
LEVELS = self.spinBoxLEVELS.value()
self.switch_window.emit()
And in the controller file
class Controller:
def __init__(self):
pass
def show_login(self):
self.login = Login()
self.login.switch_window.connect(self.show_main)
self.login.show()
def show_main(self):
self.window = MainWindow()
self.window.switch_window.connect(self.show_window_two)
self.login.close()
self.window.show()
And in the MainWindow file where I want to use LEVELS
import sys
from PyQt5 import QtCore, QtWidgets
from firstwindow import LEVELS
class MainWindow(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal()
#switch_window = QtCore.pyqtSignal(str)
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self, LEVELS)
self.setWindowTitle('PriorityMap')
def setupUi(self, PriorityMap, LEVELS):
PriorityMap.setObjectName("First")
PriorityMap.setEnabled(True)
PriorityMap.resize(675,776)
PriorityMap.setFocusPolicy(QtCore.Qt.TabFocus)
layout = QtWidgets.QGridLayout()
#CREATING ELEMENTS
for i in range(0,LEVELS+2):
for j in range(0,5):
if (i==0 and j!=0):
layout.addWidget(QtWidgets.QLabel(str(j-1)),i,j)
elif (j==0 and i!=0):
layout.addWidget(QtWidgets.QLabel("LEVEL"+str(i-1)),i,j)
else:
layout.addWidget(QtWidgets.QPushButton(str(i)+","+str(j)),i,j)
#CONTINUE AND QUIT BUTTON
self.QuitButton = QtWidgets.QPushButton("Quit")
self.QContinueButton = QtWidgets.QPushButton("Continue")
#actions
self.QuitButton.clicked.connect(PriorityMap.close)
self.QContinueButton.clicked.connect(self.switch)
#LAYOUT
layout.addWidget(self.QuitButton,15,1)
layout.addWidget(self.QContinueButton,15,2)
self.setLayout(layout)
def switch(self):
self.switch_window.emit()
Avoid abusing the global variables(1), and in this case it is not necessary, you must make the dynamic creation of widgets a moment before making the change in the show_main method:
class Controller:
def show_login(self):
self.login = Login()
self.login.switch_window.connect(self.show_main)
self.login.show()
def show_main(self):
self.window = MainWindow()
levels = self.login.spinBoxLEVELS.value()
self.window.setLevels(levels)
self.window.switch_window.connect(self.show_window_two)
self.login.close()
self.window.show()
class MainWindow(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal()
# switch_window = QtCore.pyqtSignal(str)
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.setWindowTitle('PriorityMap')
def setupUi(self, PriorityMap):
PriorityMap.setObjectName("First")
PriorityMap.setEnabled(True)
PriorityMap.resize(675,776)
PriorityMap.setFocusPolicy(QtCore.Qt.TabFocus)
layout = QtWidgets.QVBoxLayout(self)
self.m_content_widget = QtWidgets.QWidget()
layout.addWidget(self.m_content_widget, stretch=1)
#CONTINUE AND QUIT BUTTON
self.QuitButton = QtWidgets.QPushButton("Quit")
self.QContinueButton = QtWidgets.QPushButton("Continue")
#actions
self.QuitButton.clicked.connect(PriorityMap.close)
self.QContinueButton.clicked.connect(self.switch)
w = QtWidgets.QWidget()
hlay = QtWidgets.QHBoxLayout(w)
hlay.addWidget(self.QuitButton)
hlay.addWidget(self.QContinueButton)
layout.addWidget(w, alignment=QtCore.Qt.AlignRight)
def setLevels(self, levels):
layout = QtWidgets.QGridLayout(self.m_content_widget)
for i in range(0,levels+2):
for j in range(0, 5):
if (i==0 and j!=0):
layout.addWidget(QtWidgets.QLabel(str(j-1)),i,j)
elif (j==0 and i!=0):
layout.addWidget(QtWidgets.QLabel("LEVEL"+str(i-1)),i,j)
else:
layout.addWidget(QtWidgets.QPushButton(str(i)+","+str(j)),i,j)
def switch(self):
self.switch_window.emit()
(1) Why are global variables evil?
Ended up doing this, which worked!
Instantiated all variables from login window before calling login.close. Also passed in variables needed in next function. In this way I was also able to create a function which prints out the parameters.
class Controller:
def __init__(self):
pass
def show_login(self):
self.login = Login()
self.login.switch_window.connect(self.show_main)
self.login.show()
def show_main(self):
self.LEVELS = self.login.LEVELS
self.window = MainWindow(self.LEVELS)
self.window.switch_window.connect(self.show_window_two)
self.login.close()
self.window.show()
def writetofile(Controller):
f = open("f.txt", "w+")
f.write("int LEVELS = %d;\n\n" %Controller.LEVELS)
f.close()
I'm new to signal and slots in python and currently designing a GUI with Pyqt5. User requires to select from a mix of qcombobox where each element is a qmenu. I'm trying to store the selection of the path user is taking using signals.
Image of selection GUI
Attempt:
Using the pathChanged in a class as:
pathChanged = QtCore.pyqtSignal(list)
and later in the init print the selected path with:
combo.pathChanged.connect(print)
But this would not save the value of my path to a variable but just showing it on the console. If I equate the above to a variable, then I just receive the memory location.
How can I store the selections in a list and the list gets appended after each user selection?
Trying to implement the idea from the original code: https://stackoverflow.com/a/52723148/6942881
And my code is:
from PyQt5 import QtCore, QtWidgets
data = {'Concept': {
"Aircraft":{
"Blended Wing":[],
"Lifting Body":["Flat Bottom","Round Bottom","Wing"],
"Wing Body":["Fixed","Rotary","Swept"]},
"Launch Vehicle":{
"SSTO":["Expendable","Reusable","Partially Reusable"],
"TSTO":["Expendable","Reusable","Partially Reusable"],
"MSTO":["Expendable","Reusable","Partially Reusable"]}
}
}
class ComboBoxExpandable(QtWidgets.QPushButton):
currentTextChanged = QtCore.pyqtSignal(str)
pathChanged = QtCore.pyqtSignal(list)
def setData(self, value):
menu = QtWidgets.QMenu(self)
self.setMenu(menu)
self.append_element(value, menu)
menu.triggered.connect(self.on_triggered)
def printer(self,path):
# print(path)
return path
#QtCore.pyqtSlot(QtWidgets.QAction)
def on_triggered(self, action):
self.setText(action.text())
path = [action.text()]
w = action.parentWidget()
while w.parentWidget() and isinstance(w.parentWidget(), QtWidgets.QMenu):
path.insert(0, w.title())
w = w.parentWidget()
self.pathChanged.emit(path)
self.currentTextChanged.emit(self.text())
self.printer(path)
#staticmethod
def append_element(value, menu):
if isinstance(value, list):
for e in value:
ComboBoxExpandable.append_element(e, menu)
elif isinstance(value, dict):
for k, v in value.items():
if v==[]:
menu.addAction(k)
else:
ComboBoxExpandable.append_element(v, menu.addMenu(k))
else:
menu.addAction(value)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
combo = ComboBoxExpandable()
combo.setData(data)
result_label = QtWidgets.QLabel()
combo.currentTextChanged.connect(result_label.setText)
combo.pathChanged.connect(print)
lay = QtWidgets.QFormLayout(self)
lay.addRow("Select Concept: ", combo)
lay.addRow("Concept Selected: ", result_label)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
The solution is to create a slot connected to that signal where the list that is transported through the signal is added to a member of the class:
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self._paths = [] # <---container
combo = ComboBoxExpandable()
combo.setData(data)
result_label = QtWidgets.QLabel()
combo.currentTextChanged.connect(result_label.setText)
combo.pathChanged.connect(self.on_pathChanged)
lay = QtWidgets.QFormLayout(self)
lay.addRow("Select Concept: ", combo)
lay.addRow("Concept Selected: ", result_label)
#QtCore.pyqtSlot(list)
def on_pathChanged(self, path):
self._paths.append(path) # append path
print(self._paths)
My PyQt application starts with Login screen. If password OK, a module-screen (with icons) appears. When user click some button, a QMainWindow will appears. But I can't do this because of qmainwindow object has no attribute '_exec' error. This is my code:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Main(QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
...
...
class Login(QDialog):
def __init__(self, parent=None):
super(Login, self).__init__(parent)
...
...
uyg=QApplication(sys.argv)
class icons(QDialog):
def __init__(self, parent=None):
super(icons, self).__init__(parent)
...
self.buton = QPushButton()
self.buton.pressed.connect(self.open)
...
def open(self):
dialogmain = Main()
dialogmain._exec() #or dialogmain.show() ???
self.accept()
self.close()
uyg.exec_()
if Login().exec_() == QDialog.Accepted:
dialog = icons()
dialog.exec_()
else:
uyg.quit()
What am I doing wrong? Thank you.
Lately i have done the similar work:I have a loging window and a main window ,and I used something like a FSM to switch between the loging and main window.
Let's say we have 3 state:loging,main,quit.
STATE_LOGING = 0
STATE_MAIN = 1
STATE_QUIT = 2
STATE_DESTROY = 3 #this is a flag
class CState():
sigSwitchState = pyqtSignal(int)
def __init__(self):
super(CState,self).__init__()
def start(self):
pass
def sendQuit(self,nextstate):
self.sigSwitch.emit(nextstate)
class CLoginState(CState):
def __init__(self):
super(CLoginState,self).__init__()
def start(self):
w = Loging()
w.show()
def whenPasswdOk(self):
self.sendQuit(STATE_MAIN)
class CMainState(CState):
def __init__(self):
super(CMainState,self).__init__()
def start(self):
w = MainWindow()
w.show()
def whenMainWindowQuit(self):
self.sendQuit(STATE_QUIT)
class CQuitState(CState):
def __init__(self):
super(CQuitState,self).__init__()
def start(self):
#do some clean stuff...
pass
def whenCleanDone(self):
self.sendQuit(STATE_DESTROY)
class CMainApp():
def __init__(self):
self.app = QApplication(sys.argv)
def __CreateState(state):
if state == STATE_LOGING:
s = CLoginState()
if state == STATE_MAIN:
s = CMainState()
#... same as other state
s.sigSwitchState.connect(self.procNextState)
def procNextState(self,state):
if state == STATE_DESTROY:
QApplication().exit()
s = self.__CreateState(state)
s.start()
def run(self):
self.procNextState(STATE_LOGING)
sys.exit(self.app.exec_())
if __name__ == "__main__":
app = CMainApp()
app.run()
Apart from the application object and QDrag, please pretend that exec() doesn't exist. It is an utterly confusing method that essentially never has to be used. Especially not by anyone new to Qt.
If you want to display any widget, simply show() it. If you want to be notified when a dialog was accepted, connect some code to its accepted() signal. That's all.
I have created the code to create multiple classes with different class names (I.e. Question1, Question2 etc.)
import sys
from PyQt4 import QtCore, QtGui
class StartTest(QtGui.QMainWindow):
def __init__(self, parent=None):
super(StartTest, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
question1 = Question1(self)
self.central_widget.addWidget(question1)
self.central_widget.setCurrentWidget(question1)
question1.proceed.clicked.connect(self.question2)
def question2(self):
question2 = Question2(self)
self.central_widget.addWidget(question2)
self.central_widget.setCurrentWidget(question2)
i = 0
while i<2:
class Question+i(QtGui.QWidget):
def __init__(self, parent=None):
super(Question+i, self).__init__(parent)
question = QtGui.QLabel('What is 5+5?')
self.proceed = QtGui.QPushButton("Proceed")
self.Answer = QtGui.QLineEdit(self)
layout = QtGui.QFormLayout()
layout.addRow(question, self.Answer)
layout2 = QtGui.QVBoxLayout()
layout2.addLayout(layout)
layout2.addWidget(self.proceed)
self.setLayout(layout2)
print('Question'+i)
if __name__ == '__main__':
User = ''
app = QtGui.QApplication([])
window = StartTest()
window.showFullScreen()
app.exec_()
However when I write:
i = 0
while i<2:
class Question+i(QtGui.QWidget):
i obtain a syntax error on the Question+i part error, which is undertandable. But how would i overcome this whilst still creating multiple classes with different class names???
See this related question with answers on how to set class names dynamically.
You need to implement a factory method. For you it might look like this:
def factory(num) :
BaseClass = QtGui.QWidget
class NewClass(BaseClass): pass
NewClass.__name__ = "factory_%s%d" % (BaseClass.__name__, num)
return NewClass
In Pyqt, I am trying to make the QHeaderView of a QTableWidget respond to right mouse clicks.
I have subclassed QHeaderView and i have overloaded the mousePressEvent.
Then i can set it as as the header of my custom QTableWidget, the DataTable class. However i don't understand how to set the labels of the header.
Thanks for helping!
Here is some code.
class Header( QtGui.QHeaderView ):
def __init__ ( self, parent ):
QtGui.QHeaderView.__init__( self, QtCore.Qt.Vertical, parent=parent )
def mousePressEvent( self, event ):
if event.button() == QtCore.Qt.RightButton:
do_stuff()
class DataTable( QtGui.QTableWidget ):
def __init__ ( self ):
QtGui.QTableWidget.__init__( self )
self.setShowGrid(True)
self.header = Header( parent = self )
self.header.setClickable(True)
self.setHorizontalHeader( self.header )
def set_header( self, labels ):
???
This sample code should be useful:
import sys
import string
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Header(QHeaderView):
def __init__(self, parent=None):
super(Header, self).__init__(Qt.Horizontal, parent)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.ctxMenu)
self.hello = QAction("Hello", self)
self.hello.triggered.connect(self.printHello)
self.currentSection = None
def printHello(self):
data = self.model().headerData(self.currentSection, Qt.Horizontal)
print data.toString()
def ctxMenu(self, point):
menu = QMenu(self)
self.currentSection = self.logicalIndexAt(point)
menu.addAction(self.hello)
menu.exec_(self.mapToGlobal(point))
class Table(QTableWidget):
def __init__(self, parent=None):
super(Table, self).__init__(parent)
self.setHorizontalHeader(Header(self))
self.setColumnCount(3)
self.setHorizontalHeaderLabels(['id', 'name', 'username'])
self.populate()
def populate(self):
self.setRowCount(10)
for i in range(10):
for j,l in enumerate(string.letters[:3]):
self.setItem(i, j, QTableWidgetItem(l))
if __name__ == '__main__':
app = QApplication(sys.argv)
t = Table()
t.show()
app.exec_()
sys.exit()
Short answer:
The QTableWidget has a function called "setHorizontalHeaderLabels", simply supply your headers to it. Like so:
your_table_widget.setHorizontalHeaderLabels(["name", "phone number", "email"])
I would have added this as a comment to pedrotech, but I don't have enough rep for comments.