PyQt:setCentralWidget 与 QPushButton 函数冲突

PyQt: setCentralWidget clashes with QPushButton function

所以我从我的项目中推断出这个问题,我在 Main Window class 中注入的 Slides Widget class 中的 setCentralWidget 导致按钮的功能(即从 Main Window 打开一个新的 Widget window) 根本不起作用。

如果我删除 setCentralWidget,按钮可以正常工作,不会影响任何功能,我应该使用什么方法?我是否应该使用不同形式调用幻灯片小部件 class?

一如既往,我们将不胜感激!谢谢!

from PyQt4 import QtCore, QtGui
import sys
from functools import partial


class MainWindow(QtGui.QMainWindow):
    def __init__(self, image_files, parent=None):
        super(MainWindow, self).__init__()

        self.setupUi(self)
        self.slides_widget = Slides(image_files, self)
#If you enable this down below, pushButton will not function 
#and instead the slideshow will pop up and function correctly
        #self.setCentralWidget(self.slides_widget)

    def setupUi(self, MainWindow):

        MainWindow.resize(1278, 688)
        #MainWindow.setStyleSheet(self.styledata)

        self.groupBox = QtGui.QGroupBox(MainWindow)
        self.groupBox.setGeometry(QtCore.QRect(490, 220, 120, 371))
        self.groupBox.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
        self.groupBox.setFlat(False)
        self.groupBox.setCheckable(False)


        self.pushButton_Desc = QtGui.QPushButton(self.groupBox)
        self.pushButton_Desc.setGeometry(QtCore.QRect(20, 30, 71, 61))

        self.pushButton_Desc.clicked.connect(partial(self.DescWindow))

        self.groupBox.raise_()

        self.NewWindow = QtGui.QWidget()

    def DescWindow(self):
        self.NewWindow.show();


class Slides(QtGui.QWidget):
    def __init__(self, image_files, parent=None):
        super(Slides, self).__init__(parent)

        self.image_files = image_files
        self.label = QtGui.QLabel("", self)
        self.label.setGeometry(65, 225, 423, 363)

        #buttons to rewind and forward
        self.button = QtGui.QPushButton(". . .", self)
        self.button.setGeometry(200, 100, 140, 30)
        self.button.clicked.connect(self.timerEvent)

        self.timer = QtCore.QBasicTimer()
        self.step = 0
        self.delay = 3000 #ms

    def timerEvent(self, e=None):
        if self.step >= len(self.image_files):
            self.timer.start(self.delay, self)
            self.step = 0
            return
        self.timer.start(self.delay, self)
        file = self.image_files[self.step]
        image = QtGui.QPixmap(file)
        self.label.setPixmap(image)
        #self.setWindowTitle("{} --> {}".format(str(self.step), file))
        self.step += 1


image_files = ["images\slide1.jpg", "images\slide2.jpg", "images\slide3.jpg",
 "images\slide4.jpg", "images\slide5.jpg"]

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    Form = MainWindow(image_files)
    Form.show()
    sys.exit(app.exec_())

出现问题是因为先放按钮,然后在上面放CentralWidget,解决方法是把按钮放在CentralWidget,即Slides中。

您必须更改:

def __init__(self, image_files, parent=None):
    super(MainWindow, self).__init__()
    self.setupUi(self)
    self.slides_widget = Slides(image_files, self)

至:

def __init__(self, image_files, parent=None):
    super(MainWindow, self).__init__()
    self.slides_widget = Slides(image_files, self)
    self.setupUi(self)

self.groupBox = QtGui.QGroupBox(MainWindow)

至:

self.groupBox = QtGui.QGroupBox(MainWindow.slides_widget)

完整代码:

from PyQt4 import QtCore, QtGui
import sys


class MainWindow(QtGui.QMainWindow):
    def __init__(self, image_files, parent=None):
        super(MainWindow, self).__init__()
        self.slides_widget = Slides(image_files, self)
        self.setupUi(self)

    def setupUi(self, MainWindow):

        MainWindow.resize(1278, 688)
        #MainWindow.setStyleSheet(self.styledata)

        self.groupBox = QtGui.QGroupBox(MainWindow.slides_widget)
        self.groupBox.setGeometry(QtCore.QRect(490, 220, 120, 371))
        self.groupBox.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
        self.groupBox.setFlat(False)
        self.groupBox.setCheckable(False)


        self.pushButton_Desc = QtGui.QPushButton(self.groupBox)
        self.pushButton_Desc.setGeometry(QtCore.QRect(20, 30, 71, 61))

        self.pushButton_Desc.clicked.connect(self.DescWindow)
        self.setCentralWidget(self.slides_widget)

        self.groupBox.raise_()

        self.NewWindow = QtGui.QWidget()

    def DescWindow(self):
        self.NewWindow.show();


class Slides(QtGui.QWidget):
    def __init__(self, image_files, parent=None):
        super(Slides, self).__init__(parent)

        self.image_files = image_files
        self.label = QtGui.QLabel("", self)
        self.label.setGeometry(65, 225, 423, 363)

        #buttons to rewind and forward
        self.button = QtGui.QPushButton(". . .", self)
        self.button.setGeometry(200, 100, 140, 30)
        self.button.clicked.connect(self.timerEvent)

        self.timer = QtCore.QBasicTimer()
        self.step = 0
        self.delay = 3000 #ms

    def timerEvent(self, e=None):
        if self.step >= len(self.image_files):
            self.timer.start(self.delay, self)
            self.step = 0
            return
        self.timer.start(self.delay, self)
        file = self.image_files[self.step]
        image = QtGui.QPixmap(file)
        self.label.setPixmap(image)
        #self.setWindowTitle("{} --> {}".format(str(self.step), file))
        self.step += 1


image_files = ["images\slide1.jpg", "images\slide2.jpg", "images\slide3.jpg",
 "images\slide4.jpg", "images\slide5.jpg"]

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    Form = MainWindow(image_files)
    Form.show()
    sys.exit(app.exec_())