PyQT:LineEdit超过QTextEdit

时间:2013-09-17 00:13:02

标签: python qt user-interface pyqt

好的,我真的需要帮助...我有一个dockwidget,在那个dockwidget我有一个textedit。好的,到目前为止一切都很好,以下是代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'out/untitled.ui'
#
# Created: Mon Sep 16 19:33:15 2013
#      by: PyQt4 UI code generator 4.10.2
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(320, 240)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.dockWidget = QtGui.QDockWidget(MainWindow)
        self.dockWidget.setFeatures(QtGui.QDockWidget.NoDockWidgetFeatures)
        self.dockWidget.setObjectName(_fromUtf8("dockWidget"))

        self.textEdit = QtGui.QTextEdit()
        self.textEdit.setGeometry(QtCore.QRect(40, 10, 104, 71))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.dockWidget.setWidget(self.textEdit)

        MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(8), self.dockWidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))

if __name__ == "__main__":
   import sys
   app = QtGui.QApplication(sys.argv)
   Form = QtGui.QMainWindow()
   f = Ui_MainWindow()
   f.setupUi(Form)
   Form.show()
   sys.exit(app.exec_())

现在,我想要做的是在窗口(或dockwidget)的底部放置一个行编辑,它与文本区域重叠,在调整dockwidget大小时不会移动,并填充整个dockwidget。我试过这个:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'out/untitled.ui'
#
# Created: Mon Sep 16 19:33:15 2013
#      by: PyQt4 UI code generator 4.10.2
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(320, 240)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.dockWidget = QtGui.QDockWidget(MainWindow)
        self.dockWidget.setFeatures(QtGui.QDockWidget.NoDockWidgetFeatures)
        self.dockWidget.setObjectName(_fromUtf8("dockWidget"))

        self.textEdit = QtGui.QTextEdit()
        self.textEdit.setGeometry(QtCore.QRect(40, 10, 104, 71))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.dockWidget.setWidget(self.textEdit)

        QtGui.QLineEdit(self.dockWidget)  # Line edit

        MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(8), self.dockWidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))

if __name__ == "__main__":
   import sys
   app = QtGui.QApplication(sys.argv)
   Form = QtGui.QMainWindow()
   f = Ui_MainWindow()
   f.setupUi(Form)
   Form.show()
   sys.exit(app.exec_())

但这不是我想要的。我真的需要让这个工作,所以任何帮助都会很棒。谢谢。

1 个答案:

答案 0 :(得分:0)

以下是如何在另一个之上显示小部件的示例:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

#---------
# IMPORT
#---------
import sys

from PyQt4 import QtGui, QtCore

#---------
# DEFINE
#---------
class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.pushButtonInput = QtGui.QPushButton(self)
        self.pushButtonInput.setText("Toggle the QLineEdit widget!")
        self.pushButtonInput.clicked.connect(self.on_pushButtonInput_clicked)

        self.textEditInput = QtGui.QTextEdit(self)
        self.textEditInput.setText("This is a QTextEdit widget.")
        self.textEditInput.installEventFilter(self)

        self.lineEditInput = QtGui.QLineEdit(self)
        self.lineEditInput.setText("This is a QLineEdit widget.")
        self.lineEditInput.hide()

        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.pushButtonInput)
        self.layoutVertical.addWidget(self.textEditInput)

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.Resize:
            geometry = self.textEditInput.geometry()

            self.lineEditInput.setGeometry(geometry)

        return super(MyWindow, self).eventFilter(obj, event)

    @QtCore.pyqtSlot()
    def on_pushButtonInput_clicked(self):
        if self.lineEditInput.isVisible():
            self.lineEditInput.hide()
        else:
            self.lineEditInput.show()

#---------
# MAIN
#---------
if __name__ == "__main__":    
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    sys.exit(app.exec_())