PyQt4布局删除所有QLabel

时间:2013-12-24 10:23:09

标签: python pyqt4

我有一个带有左右控件的toolbar,我希望每次循环结束时显示2页,然后将它们添加到self.pdf_layout并删除旧的。关键是每次前进和后退两页。 如何替换(或删除并重置)self.pdf_layout小部件(QLabels-我将它们用作图像占位符)???

class pdfViewer(QtGui.QWidget):

def __init__(self, parent,filepath):
    QtGui.QWidget.__init__(self, parent)
    #temp = tempfile.gettempdir()
    #tempfilename=temp+'/unf120.pdf'
    self.filepath = filepath

    # global CurrentPage
    #global currentPage
    self.currentPage = 0

    self.doc = popplerqt4.Poppler.Document.load(self.filepath)
    self.doc.setRenderHint(popplerqt4.Poppler.Document.Antialiasing)
    self.doc.setRenderHint(popplerqt4.Poppler.Document.TextAntialiasing)
    self.scroll_area = QtGui.QScrollArea()
    self.scroll_area.setBackgroundRole(QtGui.QPalette.Dark)
    self.mainlayout = QtGui.QVBoxLayout()
    #PDF READER CONTROLS
    self.ControlsLayout = QtGui.QHBoxLayout()
    self.ControlsWidget = QtGui.QWidget()
    self.ControlsWidget.setLayout(self.ControlsLayout)
    self.pdf_widget = QtGui.QWidget()
    self.pdf_layout = QtGui.QHBoxLayout()
    self.pdf_widget.setLayout(self.pdf_layout)
    #toolBar
    self.toolbar = QtGui.QToolBar()
    #left Button action
    leftAction = QtGui.QAction(QtGui.QIcon('left.png'),'Left',self)
    self.toolbar.addAction(leftAction)
    #right Button Action
    rightAction = QtGui.QAction(QtGui.QIcon('right.png'),'Right',self)
    #rightRender = self.renderPages(1,currentPage,100)
    self.toolbar.addAction(rightAction)
    rightAction.triggered.connect(self.forwardPages)
    #start Button Action
    startAction = QtGui.QAction(QtGui.QIcon('start.png'),'Go to: Start',self)
    self.toolbar.addAction(startAction)
    #end Window Action
    endAction = QtGui.QAction(QtGui.QIcon('end.png'),'Go to: end',self)
    self.toolbar.addAction(endAction)
    self.ControlsLayout.addWidget(self.toolbar)
    #Zoom Controls 

    # Zoom Label
    zoomLabel = QtGui.QLabel("Zoom: ")
    zoomLabel.setMaximumWidth(50)
    self.ControlsLayout.addWidget(zoomLabel)
    #Zoom ComboBox Widget
    combo = QtGui.QComboBox()
    combo.addItem("50%")
    combo.addItem("60%")
    combo.addItem("70%")
    combo.addItem("80%")
    combo.addItem("90%")
    combo.addItem("100%")
    combo.addItem("120%")
    combo.addItem("150%")
    combo.addItem("170%")
    combo.addItem("200%")
    combo.setMaximumWidth(100)
    self.ControlsLayout.addWidget(combo)


    # add PDF READER Controls to mainlayout
    self.ControlsWidget.setMaximumWidth(300)
    self.mainlayout.addWidget(self.ControlsWidget)


    # by default goes to firstPage of the current document
    self.forwardPages(100)

    self.scroll_area.setWidget(self.pdf_widget)
    self.mainlayout.addWidget(self.scroll_area)
    self.setLayout(self.mainlayout)
    self.resize(1700,900)
    self.move(500,80)

def firstPage(self,zoomLevel=100):
    self.currentPage = 0

    pdfpage = self.doc.page(self.currentPage)
    image = pdfpage.renderToImage(zoomLevel,zoomLevel)
    pixmap = QtGui.QPixmap.fromImage(image)
    self.label = QtGui.QLabel()
    self.label.setScaledContents(True)
    currentWidth = self.scroll_area.frameGeometry().width()-100
    #print currentWidth
    pixScaled = pixmap.scaledToWidth(currentWidth,QtCore.Qt.FastTransformation)

    self.label.setPixmap(pixScaled)
    self.pdf_layout.addWidget(self.label)

    print 'set first page'

这是方法....

def forwardPages(self,zoomLevel=100):

    renderingTime = time.time()
    sum_pages = self.doc.numPages()

    currentPage = self.currentPage  
    for currentPage in range(1):
        print currentPage
        pdfpage = self.doc.page(self.currentPage+1)
        image = pdfpage.renderToImage(zoomLevel,zoomLevel)
        pixmap = QtGui.QPixmap.fromImage(image)
        label = QtGui.QLabel()
        label.setScaledContents(True)
        currentWidth = self.scroll_area.frameGeometry().width()-100
        #print currentWidth
        pixScaled = pixmap.scaledToWidth(currentWidth,QtCore.Qt.FastTransformation)

        label.setPixmap(pixScaled)
        for i in range(self.pdf_layout.count()): self.pdf_layout.itemAt(i).widget().close()
        self.pdf_layout.addWidget(label)

        print "rendering time = {0}".format(time.time()-renderingTime)  
        self.scroll_area.setWidget(self.pdf_widget)
    self.currentPage = currentPage  

1 个答案:

答案 0 :(得分:-1)

你可以稍微改变你的代码。

首先存储这样的当前页面

currentLeftPage = self.currentLeftPage
currentRightPage = self.currentRightPage

您可以像

一样更改pdf页面
pdfLeftPage = self.doc.page(self.currentLeftPage+2)
pdfRightPage = self.doc.page(self.currentRightPage+2)

然后你分别渲染leftImagerightImage

注意:您可以修改或优化上述内容。