Can I make a global variable in QMainWindow and pass a value for it, and then get the value and print it in another form [QDialog] that i can open it with a button in QMainWindow?
Asked
Active
Viewed 4,297 times
0
-
Include snippets of your code with some comments explaining what you want to happen where please. – Woohoojin Aug 01 '18 at 19:28
-
i want to make global variable for all forms in pyqt5 , i mean that i can use it in any form – Mostafa M. Mead Aug 01 '18 at 19:30
1 Answers
0
Try it:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowIcon(self.style().standardIcon(QStyle.SP_TitleBarMenuButton))
self.resize(300, 150)
centerWidget = QWidget()
layout = QVBoxLayout()
self.setStyleSheet('background-color : rgb(255,255,255);')
self.setWindowTitle('MainWindow')
self.lineEdit = QLineEdit("Type here what you want to transfer for [qdialog].")
self.pushButton = QPushButton(self)
self.pushButton.setStyleSheet('background-color: rgb(255,0,0); color: #fff')
self.pushButton.setText('Pass a value for [qdialog]')
self.pushButton.clicked.connect(self.A)
layout.addWidget(self.lineEdit)
layout.addWidget(self.pushButton)
centerWidget.setLayout(layout)
self.setCentralWidget(centerWidget)
def A(self):
self.cams = Form(self.lineEdit.text())
self.cams.show()
self.close()
class Form(QDialog):
def __init__(self, value, parent=None):
super().__init__(parent)
self.setWindowTitle('Form(QDialog)')
self.setWindowIcon(self.style().standardIcon(QStyle.SP_FileDialogInfoView))
label1 = QLabel(value)
self.button = QPushButton()
self.button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding)
self.button.setIcon(self.style().standardIcon(QStyle.SP_ArrowLeft))
self.button.setIconSize(QSize(200, 200))
layoutV = QVBoxLayout()
self.pushButton = QPushButton(self)
self.pushButton.setStyleSheet('background-color: rgb(0,0,255); color: #fff')
self.pushButton.setText('Click me!')
self.pushButton.clicked.connect(self.B)
layoutV.addWidget(self.pushButton)
layoutH = QHBoxLayout()
layoutH.addWidget(label1)
layoutH.addWidget(self.button)
layoutV.addLayout(layoutH)
self.setLayout(layoutV)
def B(self):
self.cams = MainWindow()
self.cams.show()
self.close()
if __name__ == '__main__':
myApp = QApplication(sys.argv)
myMainWindow = MainWindow()
myMainWindow.show()
myApp.exec_()

S. Nick
- 12,879
- 8
- 25
- 33