2014-04-22 3 views
0

안녕하세요. QLIneEdit 뒤에 QProgressBar를 추가하고 싶습니다. Safari 브라우저 또는 IE와 마찬가지로 여기에 내 출발점이 어떻게 있습니까? ProgressBar와 MyLineEdit을 함께 연결하면됩니다. 사용자가 경로를 입력하면 경로가 열려있는 동안 진행률 막대가 진행률을 표시해야합니다!QLineEdit 내에서 PyQt 또는 PySide의 QLineEdit을 추가하십시오.

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의 모습, 그리고 허용하지 않으려는 나던 그 becuase 불구하고 사용자가 존재하지 않는 것을 입력하십시오.

도움을 주시면 감사하겠습니다.

+0

기본적으로 여기에 올린 것과 같은 질문이 아닙니까? http://stackoverflow.com/questions/21395619/how-to-place-qcombobox-ontop-of-qprogressbar-in-pyqt 내가 대답을 가지고 있지 않다는 것을 안다. 그냥 확인하고 싶다. –

+0

아아아, 사실 나는 그것을 잊어 버렸고 그 시간 이후로 나는 그것에 대해 뭔가를 시작한 시간을 얻었으므로, 당신이 지적한 오래된 질문은 삭제할 것입니다. –

답변

2

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

글쎄, 나는 OSX에서 집에서 노력했는데 둘 중 하나를 타이핑 할 수 없기 때문에 작동하지 않는 것 같아 .. –

+0

리눅스에서 시도해보고 그것에 대해 알려줄 것이며 작동 할 것이다. –

+0

흠, 텍스트 커서가 나타나지 않았지만 창문에서도 잘 작동했습니다. 코드에 포함 된 대체 if 문을 사용하여 도움이되는지 확인하십시오. –