【问题标题】:adding QProgress bar within QLineEdit in PyQt or PySide在 PyQt 或 PySide 的 QLineEdit 中添加 QProgress 栏
【发布时间】:2014-04-22 08:43:57
【问题描述】:

嗨,我想在 QLIneEdit 后面添加 QProgressBar,就像在 Safari 浏览器或 IE 中一样,所以这是我的起点,我如何将 ProgressBar 和 MyLineEdit 挂钩在一起,以便当用户完成输入路径时进度栏应该在路径打开时显示进度!!!

from PyQt4 import QtGui, QtCore
import sys

class ProgressBar(QtGui.QProgressBar):
    """ docstring for ProgressBar
    """
    def __init__(self, parent=None):
        super(ProgressBar, self).__init__(parent)
        self.timer = QtCore.QBasicTimer()
        self.step = 0
        self.doAction()

    def timerEvent(self, e):

        if self.step >= 100:

            self.timer.stop()
            return

        self.step = self.step + 15
        self.setValue(self.step)

    def doAction(self):

        if self.timer.isActive():
            self.timer.stop()
        else:
            self.timer.start(100, self)




class MyLineEdit(QtGui.QLineEdit):
    """ docstring for MyLineEdit
    """
    def __init__(self, parent=None):
        super(MyLineEdit, self).__init__(parent)
        # I want to hook this bar at the backgroind of MyLineEdit
        pbar = ProgressBar()


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

        self.pbar = ProgressBar(self)
        self.editbx = MyLineEdit(self.pbar)
        newPalette = QtGui.QPalette()
        newPalette.setColor(self.editbx.backgroundRole(), QtCore.Qt.transparent)
        self.editbx.setPalette(newPalette)
        self.editbx.setText("Defaukt text set")
        self.editbx.setStyleSheet("QLineEdit { border:none;}")
        self.pbar.setStyleSheet("QProgressBar {border:none;}")

        self.initUI()

    def initUI(self):
        # self.pbar.setGeometry(30, 40, 200, 25)
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('QtGui.QProgressBar')
        self.show()



def main():
    app = QtGui.QApplication(sys.argv)
    win = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我也期待添加一个 QCombobox 来代替输入的文本,以便它可以列出其他现有文件夹,而不是 QCompleter 使用的方式,尽管它没有 QCombobox 的外观,而且我不想让用户输入任何内容不存在的。

任何帮助将不胜感激。

【问题讨论】:

  • 这与您在此处发布的问题基本相同吗? stackoverflow.com/questions/21395619/…我知道它也没有答案,只是想检查一下
  • aahh 是的,实际上我忘记了它,因为现在我才有时间所以开始做一些事情,我将删除你指出的旧问题。

标签: python pyqt custom-controls qlineedit qprogressbar


【解决方案1】:

我附上了一个 QLineEdit 的例子,后面有一个进度条。深受这篇帖子的影响:http://www.qtcentre.org/threads/54758-Progress-bar-form-QLineEdit-issue

基本上你必须自己管理绘画。不幸的是,当我尝试用 QComboBox 做同样的事情时,它似乎不起作用。我建议您发布一个新问题,专门关于在 QComboBox 上绘制进度条,一旦您开始使用它!

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MyLineEdit(QLineEdit):
    def __init__(self, parent=None):
        QLineEdit.__init__(self, parent)
        self.timer = QBasicTimer()
        self.step = 0
        self.doAction()

    def timerEvent(self, e):
        if self.step >= 100:
            self.timer.stop()
            return

        self.step = self.step + 10
        self.repaint()        

    def doAction(self):
        if self.timer.isActive():
            self.timer.stop()
        else:
            self.timer.start(1000, self)

    def generateGradient(self, color):
        gradient = QLinearGradient(0, 0, 0, self.height());
        m_defaultBaseColor = self.palette().color(QPalette.Base)
        gradient.setColorAt(0, m_defaultBaseColor)
        gradient.setColorAt(0.15, color.lighter(120))
        gradient.setColorAt(0.5, color)
        gradient.setColorAt(0.85, color.lighter(120))
        gradient.setColorAt(1, m_defaultBaseColor)
        return gradient

    def paintEvent(self, event):
        p = QPainter(self)
        panel = QStyleOptionFrameV2()
        self.initStyleOption(panel)
        self.style().drawPrimitive(QStyle.PE_PanelLineEdit, panel, p, self)

        # an alternative to painting the QLineEdit is to do it only when the widget has focus and the progress bar is finished
        #if self.hasFocus() or self.step >= 100: QLineEdit.paintEvent(self, event)

        # however I've chosen to paint it always
        QLineEdit.paintEvent(self, event)

        painter = QPainter(self)
        lenap = QStyleOptionFrameV2()
        self.initStyleOption(lenap)
        backgroundRect = self.style().subElementRect(QStyle.SE_LineEditContents, lenap, self)

        # some alternative if statements you might like to use instead...
        #
        # if not self.hasFocus() and self.step < 100:
        # if self.step < 100:
        if True:
            loadingColor = QColor(116,192,250)
            painter.setBrush(self.generateGradient(loadingColor))
            painter.setPen(Qt.transparent)
            mid = int(backgroundRect.width()/100.0*self.step)
            progressRect = QRect(backgroundRect.x(), backgroundRect.y(), mid, backgroundRect.height())
            painter.drawRect(progressRect)

            painter.setPen(Qt.SolidLine)
            painter.drawText(backgroundRect, Qt.AlignLeft|Qt.AlignVCenter, " " + self.text())


class Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self._control = QWidget()
        self.setCentralWidget(self._control)

        l = QVBoxLayout(self._control)
        e = MyLineEdit()
        l.addWidget(e)
        b = QPushButton('a')
        l.addWidget(b)

        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec_())

【讨论】:

  • 好吧,我在家里用 OSx 试过了,但它似乎不起作用,因为我也无法输入......
  • 嗯,虽然没有显示文本光标,但在 windows 中工作(也可以打字)。您可能想尝试一些我在代码中包含的替代 if 语句,看看是否有帮助
  • 是的,它也适用于 Linux,我认为 OSx 中的文本背景不起作用
  • 所以,我评论了self.style().drawPrimitive(QStyle.PE_PanelLineEdit, panel, p, self),它也适用于Osx,而且光标不可见,因为它位于进度条渐变后面,这似乎是为什么它不可见......
猜你喜欢
  • 1970-01-01
  • 2012-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
相关资源
最近更新 更多