QT中的文本框中的日历

时间:2015-12-07 19:51:10

标签: qt pyqt qt-designer

我只想在给定图片中自定义设计,如您在文本框中单击rigth侧时所见,日历打开并选择日期,然后日历关闭,文本框显示日期。无论如何,我能在QT Designer中做到吗?

sample image

2 个答案:

答案 0 :(得分:1)

我认为您需要重新实现lineEdit才能完成此操作,这是一个最小的实现

enter image description here

class CalWidget(QtGui.QLineEdit):
    def __init__(self, parent=None):
        super(CalWidget, self).__init__(parent)
        self.calButton = QtGui.QToolButton(self)
        self.calButton.setIcon(QtGui.QIcon('/usr/dropBox/calIcon.png'))
        self.calButton.setStyleSheet('border: 0px; padding: 0px;')
        self.calButton.setCursor(QtCore.Qt.ArrowCursor)
        self.calButton.clicked.connect(self.showCalWid)

    def resizeEvent(self, event):
        buttonSize = self.calButton.sizeHint()
        frameWidth = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth)
        self.calButton.move(self.rect().right() - frameWidth - buttonSize.width(),
                         (self.rect().bottom() - buttonSize.height() + 1)/2)
        super(CalWidget, self).resizeEvent(event)

    def showCalWid(self):
        self.calendar = QtGui.QCalendarWidget()
        self.calendar.setMinimumDate(QtCore.QDate(1900, 1, 1))
        self.calendar.setMaximumDate(QtCore.QDate(3000, 1, 1))
        self.calendar.setGridVisible(True)
        self.calendar.clicked.connect(self.updateDate)
        self.calendar.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.calendar.setStyleSheet('background: white; color: black')
        self.calendar.setGridVisible(True)
        pos = QtGui.QCursor.pos()
        self.calendar.setGeometry(pos.x(), pos.y(),300, 200)
        self.calendar.show()

    def updateDate(self,*args):
        getDate = self.calendar.selectedDate().toString()
        self.setText(getDate)
        self.calendar.deleteLater()

class MainDialog(QtGui.QMainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
        centralwidget = QtGui.QWidget(self)
        self.layout = QtGui.QHBoxLayout(centralwidget)
        self.calButton = CalWidget()
        self.layout.addWidget(self.calButton)
        self.setCentralWidget(centralwidget)


def main():
     app = QtGui.QApplication(sys.argv)
     form = MainDialog()
     form.show()
     app.exec_()

if __name__ == '__main__':
     main()

答案 1 :(得分:0)

For a date edit control use:

ui.dateEditExpiry->setCalendarPopup(true);