1

このコードは、 と を使用して 1 つのダイアログを作成しQLineEditますQPushButtonQLineEditボタンをクリックすると、ボタンを押すとトリガーされるプロセスの進行状況を示す進行状況バーに変わりたいと思います。プロセスが完了するQLineEditと、通常の「LineEdit」の外観に戻るはずです。これを達成する方法は?

ここに画像の説明を入力

Photoshop で編集したアイデアは次のとおりです。

ここに画像の説明を入力

プログレス バーは、QLineEdit の下部にある細い線である可能性があります。 ここに画像の説明を入力

from PyQt4 import QtCore, QtGui
import time

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog .__init__(self, parent)
        mainLayout = QtGui.QVBoxLayout()
        lineEdit = QtGui.QLineEdit('ITEM 001')
        mainLayout.addWidget(lineEdit)
        button = QtGui.QPushButton('Push Button')
        button.clicked.connect(self.buttonClicked)
        mainLayout.addWidget(button)
        self.setLayout(mainLayout)

    def buttonClicked(self):
        print 'button clicked'
        for i in range(3):
            time.sleep(1)
            print '...processing %s'%i

if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = Dialog()
    window.resize(300, 50)
    window.show()
    app.exec_()
4

2 に答える 2

3

PyQt4 ではQLinearGradient、水平方向を取得します。PySide は垂直グラデーションのように処理しているようです。このコードは、QLineEditを介して設定された背景色で を作成しpalette.setBrush(QPalette.Base, QBrush(QLinearGradient))ます。ボタンを押すと、プログレス バーの値が 10% 上昇します。

ここに画像の説明を入力

from PyQt4 import QtCore, QtGui
import time


class Dialog(QtGui.QDialog):
    value = 0.001
    def __init__(self, parent=None):
        QtGui.QDialog .__init__(self, parent)
        mainLayout = QtGui.QVBoxLayout()

        self.lineedit = QtGui.QLineEdit()
        self.setValues()
        mainLayout.addWidget(self.lineedit)
        button = QtGui.QPushButton('Calculate')
        button.clicked.connect(self.buttonClicked)
        mainLayout.addWidget(button)
        self.setLayout(mainLayout)

    def setValues(self):
        self.lineedit.setText('progress %s'%self.value)
        palette = self.lineedit.palette()
        QRectF = QtCore.QRectF(self.lineedit.rect())
        gradient = QtGui.QLinearGradient(QRectF.topLeft(), QRectF.topRight())
        gradient.setColorAt(self.value-0.001, QtGui.QColor('#f99e41'))
        gradient.setColorAt(self.value, QtGui.QColor('#ffffff'))
        gradient.setColorAt(self.value+0.001, QtGui.QColor('#ffffff'))
        palette.setBrush(QtGui.QPalette.Base, QtGui.QBrush(gradient))
        self.lineedit.setPalette(palette)

    def buttonClicked(self):
        if self.value >0.9:
            self.value = 0.001
        else:
            self.value=self.value+0.1

        self.setValues()
        time.sleep(1)

if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = Dialog()
    window.resize(300, 50)
    window.show()
    app.exec_()

グラデーションが水平になる可能性のある PySide の場合:

import PySide.QtCore as QtCore
import PySide.QtGui as QtGui

class Dialog(QtGui.QDialog):
    value = 1.00
    def __init__(self, parent=None):
        QtGui.QDialog .__init__(self, parent)
        mainLayout = QtGui.QVBoxLayout()

        self.lineedit = QtGui.QLineEdit()
        self.setValues()
        mainLayout.addWidget(self.lineedit)
        button = QtGui.QPushButton('Calculate')
        button.clicked.connect(self.buttonClicked)
        mainLayout.addWidget(button)
        self.setLayout(mainLayout)

    def setValues(self):
        self.lineedit.setText('progress %s'%self.value)
        palette = self.lineedit.palette()
        QRectF = QtCore.QRectF(self.lineedit.rect())
        gradient = QtGui.QLinearGradient(QRectF.topLeft(), QRectF.topRight())
        gradient.setColorAt(self.value-0.001, QtGui.QColor('#ffffff'))
        gradient.setColorAt(self.value, QtGui.QColor('#f99e41'))
        gradient.setColorAt(self.value+0.001, QtGui.QColor('#f99e41'))
        palette.setBrush(QtGui.QPalette.Base, QtGui.QBrush(gradient))
        self.lineedit.setPalette(palette)

    def buttonClicked(self):
        if self.value <0.1:
            self.value = 1.00
        else:
            self.value=self.value-0.1

        self.setValues()


if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = Dialog()
    window.resize(300, 50)
window.show()
app.exec_()
于 2016-05-02T05:32:42.573 に答える
0
if progress < 99:
    percent = progress / 100
    self.lineedit.setStyleSheet('background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop: 0 #1bb77b, stop: ' + str(percent) + ' #1bb77b, stop: ' + str(percent+ 0.001) + ' rgba(0, 0, 0, 0), stop: 1 white)')

else:
    self.lineedit.setStyleSheet('background-color: white')
于 2019-06-19T05:20:19.367 に答える