Pergunta

Look at the code

import sys
from PySide import QtCore, QtGui


class MyWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.quit = QtGui.QPushButton("Quit", self)
        self.setGeometry(300, 300, 250, 150)
        self.statusBar().showMessage('Ready')


app = QtGui.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())

Now all I want to do , is add a Status. Now status is available in QtGui.QMainWindow How can i use this fact to add it in the above program? In pyside coding , seems like for every component , we need to make a class and some connect to the main class..whata s the theory here?

I tried myself like this , but it did not work.

import sys
from PySide import QtCore, QtGui


class MyWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.quit = QtGui.QPushButton("Quit", self)
        self.setGeometry(300, 300, 250, 150)
        self.statusBar().showMessage('Ready')
        self.s = MyStatus()

class MyStatus(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)

        self.statusBar().showMessage('Ready')

app = QtGui.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
Foi útil?

Solução

You do not need to make a class for every component, but if you want to modify/override each component's built-in function you need to make a class for it.

To add something to your main window, you simply have to create an object and add it to the layout. As follow:

import sys
from PySide import QtGui , QtCore

class MyStatusBar(QtGui.QStatusBar):
    def __init__(self, parent=None):
        super(MyStatusBar, self).__init__(parent)
    #Override any functions, or define new function for our status bar here

class MyMainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        #Set the size of the window
        self.setMinimumSize(300,300)
        #Create a status bar, from our OWN class
        self.status_bar = MyStatusBar(self)
        self.status_bar.setGeometry(QtCore.QRect(0, 0, 50, 50))
        self.status_bar.showMessage('Ready')
        #Add a simple quit button, from the DEFAULT class
        self.quit_button = QtGui.QPushButton(self)
        self.quit_button.clicked.connect(self.close)
        self.quit_button.setGeometry(QtCore.QRect(100, 100, 100, 35))
        self.quit_button.setText("Close")


#Start the application    
app = QtGui.QApplication(sys.argv)
top = MyMainWindow()
top.show()
app.exec_()

Outras dicas

If you want a window with a status-bar, use QMainWindow: it has one built-in (and also a menu-bar, tool-bars, dock-widgets, etc). Other widgets don't have these built-in features, and so, quite naturally, they don't have things like a statusBar method. If you insist on doing things the hard way by not using QMainWindow, you will have to add all these features yourself.

Although I wouldn't recommend doing things this way, here is a simple demo that adds a status-bar to a QWidget:

import sys
from PySide import QtCore, QtGui

class MyWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.central_widget = QtGui.QWidget(self)

        self.quit = QtGui.QPushButton("Quit", self)
        self.setGeometry(300, 300, 250, 150)

        layout = QtGui.QVBoxLayout(self.central_widget)
        layout.addWidget(self.quit)

        self.status = QtGui.QStatusBar(self)

        layout = QtGui.QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.widget)
        layout.addStretch()
        layout.addWidget(self.status)

        self.status.showMessage('Ready')

app = QtGui.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top