oiyio
oiyio

Reputation: 5935

How to delete QFrame in pyqt

i try to build a software center GUI similar to ubuntu software center. I'm using pyqt. I created three QFrame object in my GUI. Each QFrame object has a different purpose, one for categories, one for top rated softwares, one for recommended softwares. These three frames are displayed on the main GUI screen. After searching a keyword , i want to show only one frame which contains results on the screen. Hence i need to delete the three frames when i clicked a button. I didn't find any information about deleting frames.

All i found on the web is about deleting widgets from Layout. I already know and implemented this in one of my frames. In this frame , when i clicked a button, i remove some widgets from the frame,etc. My code is like the following: r self.topRatedAppsFrame=QtGui.QFrame(self.centralwidget) ...

self.horFortopRatedApps = QtGui.QWidget(self.topRatedAppsFrame)
...

self.topRatedAppsList = QtGui.QGridLayout(self.horFortopRatedApps)
...


self.app1 = QtGui.QPushButton(self.horFortopRatedApps)
self.topRatedAppsList.addWidget(self.app1,0,0)


#########this deletes only widget app1 from topRatedAppsList#########
#########but frame still exists and appears in the screen.i #########
######### want to destroy or delete frame topRatedAppsFrame #########
self.topRatedAppsList.removeWidget(self.app1)
self.app1.deleteLater()
self.app1 = None
####################################################################

Hint: What i want to implement is implemented in Ubuntu Software Center. Many frames exists in Ubuntu software center. HOwever when we searched something, everything disappears,only search results are displayed. I want to do the same thing. I downloaded the Ubuntu Software Center and examined the source code.Since it is so complex,and gtk is used ,this source code didn't help me. How can i do it? Any guidance or idea will be appreciated.

Upvotes: 0

Views: 3080

Answers (2)

ekhumoro
ekhumoro

Reputation: 120718

Deleting or removing the other frames is probably the wrong approach.

It's much more efficient to simply hide/show the frames as needed. There are several ways to achieve this, but a QStackedLayout sounds like the best fit for your puposes. All you need to do is add each set of frames as a separate layer/page in the stack, then use setCurrentIndex to bring the required item to the top. A stacked-layout is just like a tab-widget, but without the tab-bar and outer frame.

EDIT:

Here's a simple working demo which shows how to use a stacked layout:

from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        def make_frame(text, parent):
            frame = QtGui.QFrame(parent)
            frame.setFrameStyle(QtGui.QFrame.StyledPanel)
            layout = QtGui.QHBoxLayout(frame)
            label = QtGui.QLabel(text, frame)
            label.setAlignment(QtCore.Qt.AlignCenter)
            layout.addWidget(label)
            return frame
        self.splitter = QtGui.QSplitter(self)
        for text in 'ONE TWO THREE'.split():
            self.splitter.addWidget(make_frame(text, self.splitter))
        self.stack = QtGui.QStackedLayout()
        self.stack.addWidget(self.splitter)
        self.stack.addWidget(make_frame('FOUR', self))
        self.button = QtGui.QPushButton('Switch', self)
        self.button.clicked.connect(
            lambda: self.stack.setCurrentIndex(
                int(self.stack.currentIndex() == 0)))
        layout = QtGui.QVBoxLayout(self)
        layout.addLayout(self.stack)
        layout.addWidget(self.button)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 800, 500)
    window.show()
    sys.exit(app.exec_())

Upvotes: 1

oiyio
oiyio

Reputation: 5935

QFrame is a child of QWidget class. Deleting QWidget is already explained in the question. Thus, QFrame can be deleted in the similar way.

Using QStackLayout is not the solution of this problem. Because, there must be more than one active frame in the window at the same time. However, using QStackLayout only one QFrame(QWidget) can be active at once which is not suitable for this situation.

Upvotes: 0

Related Questions