change statusBar in QMainWindow from QWidget - python

Here I have a QMainWindow and QWidget class
class WifiHotspot(QMainWindow):
def __init__(self, parent=None):
super(WifiHotspot, self).__init__(parent)
self.title = 'WIFI HOTSPOT'
self.initUI()
def initUI(self):
self.virtual_wifi = VirtualWifi(self)
self.setCentralWidget(self.virtual_wifi)
# i want to dynamic set statusBar in VirtualWifi class
#self.statusBar().showMessage('message here') # it work only in WifiHotspot
self.setWindowTitle(self.title)
self.show()
class VirtualWifi(QWidget):
def __init__(self, parent):
super (VirtualWifi, self).__init__(parent)
self.initVirtualWifi()
def initVirtualWifi(self):
startButton = QPushButton('Start', self)
startButton.setToolTip('Start sharing wifi')
// when click
startButton.clicked.connect(self.start_hotspot)
#pyqtSlot()
def start_hotspot(self):
# show message in statusBar in QMainWindow
How can I show a statusBar message in WifiHotspot when click startButton from VirtualWifi

Here is an answer
** Answers:
Create a set_status_message in WifiHotspot
def set_status_message(self, message):
return self.statusBar().showMessage(message)
Call it from start_hotspot(self)
def start_hotspot(self):
self.parent().set_status_message('hello world')

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

How do i update a widget from different windows? [duplicate]

This question already has answers here:
change label from other class in a different file
(2 answers)
Closed 3 years ago.
How to update a Widget from Window A in Window B?
I have a widget that i want to change in my second window, how would i proceed from here?
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
#MyWidget
...
def openSecond(self):
self.SW = SecondWindow(#MyWidget)
self.SW.show()
class SecondWindow(QMainWindow):
def __init__(self, #MyWidget):
super(SecondWindow, self).__init__()
#MyWidget.changed
#Update to parent
...
Pass the self from class A as Argument when creating an instance of the class B, then you can access the items from class A in class B with self.parent.
from PySide2.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.button = ''
btn = QPushButton('Open Second', self)
btn.move(10, 10)
btn.clicked.connect(self.openSecond)
self.resize(420, 450)
button = QPushButton(self)
button.move(100,100)
button.setText("current text")
self.button = button
def openSecond(self):
self.SW = SecondWindow(self.button, parent=self)
self.SW.show()
class SecondWindow(QMainWindow):
def __init__(self, button, parent=None):
super(SecondWindow, self).__init__()
self.parent = parent
lbl = QLabel('Second Window', self)
button.setText("updated text")
self.parent.buttons = button
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
MW = MainWindow()
MW.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()

Signals And Slots: When simple example is not working

There are two widgets: the button and the label.
When the button is pressed I want to run the label's myFunction() method.
How to achieve this using signals and slots? The example below does not work.
from PyQt import QtCore, QtGui
class label(QtGui.QLabel):
def __init__(self, parent=None):
super(label, self).__init__(parent)
self.show()
def myFunction(self, arg=None):
print '...myFunction: received arg =', arg
class button(QtGui.QPushButton):
def __init__(self, parent=None):
super(button, self).__init__(parent)
self.clicked.connect(self.click)
self.show()
def click(self):
print 'emitted'
self.emit(QtCore.SIGNAL('buttonClicked'))
lbl = label()
btn = button()
Approach 1
One solution is to use a globally declared QObject variable connector. We use the connector.signal for both: to emit the signal and to connect the method to be executed on signal.emit().
To emit from first widget: connector.signal.emit()
To connect to second widget's method: connector.signal.connect(self.myFunction)
from PySide import QtCore, QtGui
class Communicate(QtCore.QObject):
signal = QtCore.Signal(str)
connector=Communicate()
class label(QtGui.QLabel):
def __init__(self, parent=None):
super(label, self).__init__(parent)
connector.signal.connect(self.labelFunction)
self.show()
def labelFunction(self, arg=None):
print '...labelFunction: arg =', arg
class button(QtGui.QPushButton):
def __init__(self, parent=None):
super(button, self).__init__(parent)
self.clicked.connect(self.click)
self.show()
def click(self):
connector.signal.emit("Hello World")
lbl = label()
btn = button()
Approach 2
We can achieve the same functionality by passing label.labelFunction as a callback argument. The button's connector is to be connected to it:
from PySide import QtCore, QtGui
class Communicate(QtCore.QObject):
signal = QtCore.Signal(str)
class label(QtGui.QLabel):
def __init__(self, parent=None):
super(label, self).__init__(parent)
self.show()
def labelFunction(self, arg=None):
print '...labelFunction: arg = %s'%arg
class button(QtGui.QPushButton):
def __init__(self, callback=None, parent=None):
super(button, self).__init__(parent)
self.callback=callback
self.clicked.connect(self.click)
self.show()
def click(self):
connector=Communicate()
connector.signal.connect(self.callback)
connector.signal.emit("Hello World")
lbl = label()
btn = button(callback=lbl.labelFunction)
Approach 3
Just like previously we still emit on button.clicked.
But a different syntax is used to connect label to the customSignal:
self.connect(btn, QtCore.SIGNAL('customSignal'), Function)
from PySide import QtCore, QtGui
def Function(arg=None):
print 'Function.arg: %r'%arg
class button(QtGui.QPushButton):
def __init__(self, parent=None):
super(button, self).__init__(parent)
self.clicked.connect(self.click)
self.show()
def click(self):
self.emit(QtCore.SIGNAL('customSignal'), 'String Arument')
btn = button()
class label(QtGui.QLabel):
def __init__(self, parent=None):
super(label, self).__init__(parent)
self.connect(btn, QtCore.SIGNAL('customSignal'), Function)
self.show()
lbl = label()

PyQT Button click doesn't work

So my problem is that instead of manually writing a ton of code for a bunch of buttons, I want to create a class for a QPushButton and then change so many variables upon calling that class to create my individual buttons.
My problem is that my button does not seem to be clickable despite calling the clicked.connect function and having no errors upon running the code. Here are the relevant parts of the button class:
class Button(QtGui.QPushButton):
def __init__(self, parent):
super(Button, self).__init__(parent)
self.setAcceptDrops(True)
self.setGeometry(QtCore.QRect(90, 90, 61, 51))
self.setText("Change Me!")
def retranslateUi(self, Form):
self.clicked.connect(self.printSomething)
def printSomething(self):
print "Hello"
Here is how I call the button class:
class MyWindow(QtGui.QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.btn = Button(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.btn)
self.setLayout(layout)
You should perform the connection to the clicked signal on the __init__ method:
from PyQt4 import QtGui,QtCore
class Button(QtGui.QPushButton):
def __init__(self, parent):
super(Button, self).__init__(parent)
self.setAcceptDrops(True)
self.setGeometry(QtCore.QRect(90, 90, 61, 51))
self.setText("Change Me!")
self.clicked.connect(self.printSomething) #connect here!
#no need for retranslateUi in your code example
def printSomething(self):
print "Hello"
class MyWindow(QtGui.QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.btn = Button(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.btn)
self.setLayout(layout)
app = QtGui.QApplication([])
w = MyWindow()
w.show()
app.exec_()
You can run it and will see the Hello printed on the console every time you click the button.
The retranslateUi method is for i18n. You can check here.

Categories

Resources