Вопрос

Consider the minimal example below. It works perfectly until I uncomment the following lines:

# self.mainwi = QtGui.QWidget(self)
# self.lineEdit1 = QtGui.QLineEdit(self.mainwi)
# self.setCentralWidget(self.lineEdit1)

If those lines are uncommented, I can write text in the LineEdit-field, but the buttons don't react. Any idea what's wrong with it, how to fix this?

I should add that I am an absolute beginner in programming python.

#!/usr/bin/python

import mpylayer
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui



class DmplayerGUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.dirty = False
        self.mp = mpylayer.MPlayerControl()
        #Toolbar
        ## items

        ### Play
        self.play = QtGui.QAction(QtGui.QIcon('icons/play_32.png'), 'Play', self)
        self.play.setShortcut('Ctrl+A')
        self.connect(self.play, QtCore.SIGNAL('triggered()'), self.DPlay)

        ### Pause
        self.pause = QtGui.QAction(QtGui.QIcon('icons/pause_32.png'), 'Pause', self)
        self.pause.setShortcut('Ctrl+P')
        self.connect(self.pause, QtCore.SIGNAL('triggered()'), self.DPause)


        ## toolbar
        self.toolbar = self.addToolBar('Toolbar')
        self.toolbar.addAction(self.play)
        self.toolbar.addAction(self.pause)

        # self.mainwi = QtGui.QWidget(self)
        # self.lineEdit1 = QtGui.QLineEdit(self.mainwi)
        # self.setCentralWidget(self.lineEdit1)

    # play
    def DPlay(self):
        self.mp.loadfile('video.mp4')

    # pause
    def DPause(self):
        self.mp.pause(self)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    dp = DmplayerGUI()
    dp.show()
    sys.exit(app.exec_())
Это было полезно?

Решение

You do not need the mainwi at all in this simple example. Just do

self.lineEdit1 = QtGui.QLineEdit(self)
self.setCentralWidget(self.lineEdit1)

In case you really wanted it, then you need to set the mainwi as the centralwidget

self.mainwi = QtGui.QWidget(self)
self.lineEdit1 = QtGui.QLineEdit(self.mainwi)
self.setCentralWidget(self.mainwi)

do not forget to add some layout for mainwi, since this looks ugly :-)

Anyway, I have to admit, that I do not know why exactly does it "disable" the buttons. But the central widget has to be a child of the window as far as I know.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top