如何在PySide中显示和隐藏QtGui.QGridLayout

时间:2014-09-05 17:59:17

标签: python-2.7 pyqt pyside

这里我创建一个名为_grid的布局,并在布局中放置一些标签和滑块。

现在我希望首先隐藏此布局,然后onclick我想要显示它。

如果有任何与QtGui.QGridLayout关联的简单功能显示和隐藏。

我用Google搜索,发现QWidget

可以使用QWidget.hide()
class TimeSlider (QtGui.QVBoxLayout):
 def __init__ (self):
    QtGui.QVBoxLayout.__init__ (self)


    # Put everything in a grid
    _grid = QtGui.QGridLayout ()
    self.addLayout (_grid)

    # Slider limit labels

    _grid.addWidget (QtGui.QLabel (''), 0, 0)
    _labelLayout = QtGui.QHBoxLayout ()
    _grid.addLayout (_labelLayout, 0, 1)
    self.lower = QtGui.QLabel ('LOW')


    _labelLayout.addWidget (self.lower)
    self.higher = QtGui.QLabel ('HIGH')
    self.higher.setAlignment (QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)

    _labelLayout.addWidget (self.higher)

    # Slider

    _grid.addWidget (QtGui.QLabel (''), 1, 0)
    self.sliderBarLayout = QtGui.QHBoxLayout ()
    self.sliderBarLayout.setSpacing (0)
    _grid.addLayout (self.sliderBarLayout, 1, 1)

    self.sliderBarLayout.addWidget (self.leftEndstop)

    self.slider = QtGui.QSlider (QtCore.Qt.Horizontal)
    self.slider.setTickPosition (QtGui.QSlider.TicksBelow)
    self.slider.setTracking (True)
    self.slider.setMinimumWidth (40)
    self.slider.setPageStep (1)

    self.sliderBarLayout.addWidget (self.slider)

    self.rightEndstop = RightEndstop (self, self.root)

    self.sliderBarLayout.addWidget (self.rightEndstop)

1 个答案:

答案 0 :(得分:0)

import sys, time
from PyQt4 import QtGui, QtCore

class TimeSlider (QtGui.QVBoxLayout):
    def __init__ (self):
        QtGui.QVBoxLayout.__init__ (self)
        # Put everything in a grid
        _manLayout = QtGui.QVBoxLayout ()
        self._frame = QtGui.QFrame();
        _manLayout.addWidget(self._frame)
        _grid = QtGui.QGridLayout (self._frame)

        # Slider limit labels

        _grid.addWidget (QtGui.QLabel (''), 0, 0)
        _labelLayout = QtGui.QHBoxLayout ()
        _grid.addLayout (_labelLayout, 0, 1)
        self.lower = QtGui.QLabel ('LOW')


        _labelLayout.addWidget (self.lower)
        self.higher = QtGui.QLabel ('HIGH')
        self.higher.setAlignment (QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)

        _labelLayout.addWidget (self.higher)

        # Slider

        _grid.addWidget (QtGui.QLabel (''), 1, 0)
        self.sliderBarLayout = QtGui.QHBoxLayout ()
        self.sliderBarLayout.setSpacing (0)
        _grid.addLayout (self.sliderBarLayout, 1, 1)

        # self.sliderBarLayout.addWidget (self.leftEndstop)

        self.slider = QtGui.QSlider (QtCore.Qt.Horizontal)
        self.slider.setTickPosition (QtGui.QSlider.TicksBelow)
        self.slider.setTracking (True)
        self.slider.setMinimumWidth (40)
        self.slider.setPageStep (1)

        self.sliderBarLayout.addWidget (self.slider)
        self._frame.setLayout(_grid)
        self.addLayout (_manLayout)

        #self.rightEndstop = RightEndstop (self, self.root)

        # self.sliderBarLayout.addWidget (self.rightEndstop)
    def show(self):
        self._frame.show()
    def hide(self):
        self._frame.hide()
    def isHidden(self):
        return self._frame.isHidden()



class NewTimeSliderTest(QtGui.QWidget):
    def __init__(self, parent=None, total=20):
        super(NewTimeSliderTest, self).__init__(parent)
        self.newTimeSlider = TimeSlider()
        self.resize(841, 474)
        self.newTimeSlider.hide()
        self.button = QtGui.QPushButton('Show/Hide')
        self.button.clicked.connect(self.handleButton)
        main_layout = QtGui.QGridLayout()
        main_layout.addWidget(self.button, 0, 0)
        main_layout.addLayout(self.newTimeSlider, 0, 1)
        self.setLayout(main_layout)
        self.setWindowTitle('Test')
        self._active = False

    def handleButton(self):
        #self.newTimeSlider.show()
        isHidden = self.newTimeSlider.isHidden()
        if isHidden:
            self.newTimeSlider.show()
        else:
            self.newTimeSlider.hide()


app = QtGui.QApplication(sys.argv)
bar = NewTimeSliderTest(total=101)
bar.show()
sys.exit(app.exec_())

这个怎么样?

任何时候你都可以这样。

self.newTimeSlider.show()

self.newTimeSlider.hide()