PyQt:setCentralWidget与QPushButton函数冲突

时间:2017-04-24 03:24:02

标签: python pyqt pyqt4 qwidget qpushbutton

所以我从我的项目中推断出这个问题,我在主窗口类中注入的Slides Widget类中的setCentralWidget导致从主窗口中按钮的功能(即打开一个新的Widget窗口)无法工作所有。

如果我删除了setCentralWidget,按钮工作正常,不会影响任何功能,我应该采用什么方法?我应该使用不同形式调用Slides Widget类吗?

任何帮助都将一如既往地受到赞赏!谢谢!

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_())

1 个答案:

答案 0 :(得分:1)

问题出现了,因为首先放置按钮并在其上放置CentralWidget,解决方案是将按钮放在CentralWidget中,即在幻灯片中。

你必须改变:

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_())