pyqt dateEdit Max value
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have used pyqt designer to create a UI which has 2 dateEdit widgets which are meant to specify a time window. Both of them are set to popup a calendar to make the user life easier. I want to make sure that you can never obtain a time window where the "start" is bigger than the "end".
Example start and end are dateEdit widgets:
start.value = 20181010
end.value = 20181011
The user selects the start widget, the calendar pops up, and the user could select 20181012, which would lead to start.value > end.value, which would be nonsensical in my application.
I need to be able to enforce that this doesn't happen.
There are multiple ways I can approach the problem, but each one of them requires me to have something to hijack the callback temporarily, like ComboBox.currentIndexChanged in the case of ComboBox. I can't find a similar function for dateEdit.
The problem I face is to set the start date to be always lower or equal to the end date. I'm happy to have the end date always be fixed, so I only need to constrain one of the dateEdit.
The issue is that the only "Max" constraint is a static one, so I would need to hijack the callback that triggers the open of the calendar so that I can insert code that reads the other dateEdit widget and sets its value as the max in the current one.
Any clue on how to do the hijacking?
Alternative plans are well accepted too.
python datepicker pyqt5
add a comment |
I have used pyqt designer to create a UI which has 2 dateEdit widgets which are meant to specify a time window. Both of them are set to popup a calendar to make the user life easier. I want to make sure that you can never obtain a time window where the "start" is bigger than the "end".
Example start and end are dateEdit widgets:
start.value = 20181010
end.value = 20181011
The user selects the start widget, the calendar pops up, and the user could select 20181012, which would lead to start.value > end.value, which would be nonsensical in my application.
I need to be able to enforce that this doesn't happen.
There are multiple ways I can approach the problem, but each one of them requires me to have something to hijack the callback temporarily, like ComboBox.currentIndexChanged in the case of ComboBox. I can't find a similar function for dateEdit.
The problem I face is to set the start date to be always lower or equal to the end date. I'm happy to have the end date always be fixed, so I only need to constrain one of the dateEdit.
The issue is that the only "Max" constraint is a static one, so I would need to hijack the callback that triggers the open of the calendar so that I can insert code that reads the other dateEdit widget and sets its value as the max in the current one.
Any clue on how to do the hijacking?
Alternative plans are well accepted too.
python datepicker pyqt5
I do not understand you, it seems that you are doing a problem where there is not, you could provide a Minimal, Complete, and Verifiable example pointing out what you get in addition to what you expect
– eyllanesc
Nov 23 '18 at 16:34
@eyllanesc I rewrote the question, please give it another read.
– Dirich
Nov 23 '18 at 17:26
of what I understand you have 2 QDateEdit, both with preset values, the first must always be less than the second, and vice versa, am I correct?
– eyllanesc
Nov 23 '18 at 17:31
@eyllanesc yes, you are
– Dirich
Nov 23 '18 at 17:36
add a comment |
I have used pyqt designer to create a UI which has 2 dateEdit widgets which are meant to specify a time window. Both of them are set to popup a calendar to make the user life easier. I want to make sure that you can never obtain a time window where the "start" is bigger than the "end".
Example start and end are dateEdit widgets:
start.value = 20181010
end.value = 20181011
The user selects the start widget, the calendar pops up, and the user could select 20181012, which would lead to start.value > end.value, which would be nonsensical in my application.
I need to be able to enforce that this doesn't happen.
There are multiple ways I can approach the problem, but each one of them requires me to have something to hijack the callback temporarily, like ComboBox.currentIndexChanged in the case of ComboBox. I can't find a similar function for dateEdit.
The problem I face is to set the start date to be always lower or equal to the end date. I'm happy to have the end date always be fixed, so I only need to constrain one of the dateEdit.
The issue is that the only "Max" constraint is a static one, so I would need to hijack the callback that triggers the open of the calendar so that I can insert code that reads the other dateEdit widget and sets its value as the max in the current one.
Any clue on how to do the hijacking?
Alternative plans are well accepted too.
python datepicker pyqt5
I have used pyqt designer to create a UI which has 2 dateEdit widgets which are meant to specify a time window. Both of them are set to popup a calendar to make the user life easier. I want to make sure that you can never obtain a time window where the "start" is bigger than the "end".
Example start and end are dateEdit widgets:
start.value = 20181010
end.value = 20181011
The user selects the start widget, the calendar pops up, and the user could select 20181012, which would lead to start.value > end.value, which would be nonsensical in my application.
I need to be able to enforce that this doesn't happen.
There are multiple ways I can approach the problem, but each one of them requires me to have something to hijack the callback temporarily, like ComboBox.currentIndexChanged in the case of ComboBox. I can't find a similar function for dateEdit.
The problem I face is to set the start date to be always lower or equal to the end date. I'm happy to have the end date always be fixed, so I only need to constrain one of the dateEdit.
The issue is that the only "Max" constraint is a static one, so I would need to hijack the callback that triggers the open of the calendar so that I can insert code that reads the other dateEdit widget and sets its value as the max in the current one.
Any clue on how to do the hijacking?
Alternative plans are well accepted too.
python datepicker pyqt5
python datepicker pyqt5
edited Nov 23 '18 at 17:25
Dirich
asked Nov 23 '18 at 16:07
DirichDirich
766
766
I do not understand you, it seems that you are doing a problem where there is not, you could provide a Minimal, Complete, and Verifiable example pointing out what you get in addition to what you expect
– eyllanesc
Nov 23 '18 at 16:34
@eyllanesc I rewrote the question, please give it another read.
– Dirich
Nov 23 '18 at 17:26
of what I understand you have 2 QDateEdit, both with preset values, the first must always be less than the second, and vice versa, am I correct?
– eyllanesc
Nov 23 '18 at 17:31
@eyllanesc yes, you are
– Dirich
Nov 23 '18 at 17:36
add a comment |
I do not understand you, it seems that you are doing a problem where there is not, you could provide a Minimal, Complete, and Verifiable example pointing out what you get in addition to what you expect
– eyllanesc
Nov 23 '18 at 16:34
@eyllanesc I rewrote the question, please give it another read.
– Dirich
Nov 23 '18 at 17:26
of what I understand you have 2 QDateEdit, both with preset values, the first must always be less than the second, and vice versa, am I correct?
– eyllanesc
Nov 23 '18 at 17:31
@eyllanesc yes, you are
– Dirich
Nov 23 '18 at 17:36
I do not understand you, it seems that you are doing a problem where there is not, you could provide a Minimal, Complete, and Verifiable example pointing out what you get in addition to what you expect
– eyllanesc
Nov 23 '18 at 16:34
I do not understand you, it seems that you are doing a problem where there is not, you could provide a Minimal, Complete, and Verifiable example pointing out what you get in addition to what you expect
– eyllanesc
Nov 23 '18 at 16:34
@eyllanesc I rewrote the question, please give it another read.
– Dirich
Nov 23 '18 at 17:26
@eyllanesc I rewrote the question, please give it another read.
– Dirich
Nov 23 '18 at 17:26
of what I understand you have 2 QDateEdit, both with preset values, the first must always be less than the second, and vice versa, am I correct?
– eyllanesc
Nov 23 '18 at 17:31
of what I understand you have 2 QDateEdit, both with preset values, the first must always be less than the second, and vice versa, am I correct?
– eyllanesc
Nov 23 '18 at 17:31
@eyllanesc yes, you are
– Dirich
Nov 23 '18 at 17:36
@eyllanesc yes, you are
– Dirich
Nov 23 '18 at 17:36
add a comment |
1 Answer
1
active
oldest
votes
You have to use the dateChanged signal to update the maximunDate or minimumDate, adding the appropriate offset according to the case:
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.date_from = QtWidgets.QDateEdit()
self.date_to = QtWidgets.QDateEdit()
for w in (self.date_from, self.date_to, ):
w.setDisplayFormat("yyyyMMdd")
w.setCalendarPopup(True)
self.setup_date()
lay = QtWidgets.QHBoxLayout(self)
lay.addWidget(self.date_from)
lay.addWidget(self.date_to)
def setup_date(self):
self.date_from.dateChanged.connect(self.on_date_from_changed)
self.date_to.dateChanged.connect(self.on_date_to_changed)
self.date_from.setDate(QtCore.QDate(2018, 10, 10))
self.date_to.setDate(QtCore.QDate(2018, 10, 11))
@QtCore.pyqtSlot(QtCore.QDate)
def on_date_from_changed(self, date):
self.date_to.setMinimumDate(date.addDays(1))
@QtCore.pyqtSlot(QtCore.QDate)
def on_date_to_changed(self, date):
self.date_from.setMaximumDate(date.addDays(-1))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449844%2fpyqt-dateedit-max-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to use the dateChanged signal to update the maximunDate or minimumDate, adding the appropriate offset according to the case:
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.date_from = QtWidgets.QDateEdit()
self.date_to = QtWidgets.QDateEdit()
for w in (self.date_from, self.date_to, ):
w.setDisplayFormat("yyyyMMdd")
w.setCalendarPopup(True)
self.setup_date()
lay = QtWidgets.QHBoxLayout(self)
lay.addWidget(self.date_from)
lay.addWidget(self.date_to)
def setup_date(self):
self.date_from.dateChanged.connect(self.on_date_from_changed)
self.date_to.dateChanged.connect(self.on_date_to_changed)
self.date_from.setDate(QtCore.QDate(2018, 10, 10))
self.date_to.setDate(QtCore.QDate(2018, 10, 11))
@QtCore.pyqtSlot(QtCore.QDate)
def on_date_from_changed(self, date):
self.date_to.setMinimumDate(date.addDays(1))
@QtCore.pyqtSlot(QtCore.QDate)
def on_date_to_changed(self, date):
self.date_from.setMaximumDate(date.addDays(-1))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
add a comment |
You have to use the dateChanged signal to update the maximunDate or minimumDate, adding the appropriate offset according to the case:
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.date_from = QtWidgets.QDateEdit()
self.date_to = QtWidgets.QDateEdit()
for w in (self.date_from, self.date_to, ):
w.setDisplayFormat("yyyyMMdd")
w.setCalendarPopup(True)
self.setup_date()
lay = QtWidgets.QHBoxLayout(self)
lay.addWidget(self.date_from)
lay.addWidget(self.date_to)
def setup_date(self):
self.date_from.dateChanged.connect(self.on_date_from_changed)
self.date_to.dateChanged.connect(self.on_date_to_changed)
self.date_from.setDate(QtCore.QDate(2018, 10, 10))
self.date_to.setDate(QtCore.QDate(2018, 10, 11))
@QtCore.pyqtSlot(QtCore.QDate)
def on_date_from_changed(self, date):
self.date_to.setMinimumDate(date.addDays(1))
@QtCore.pyqtSlot(QtCore.QDate)
def on_date_to_changed(self, date):
self.date_from.setMaximumDate(date.addDays(-1))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
add a comment |
You have to use the dateChanged signal to update the maximunDate or minimumDate, adding the appropriate offset according to the case:
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.date_from = QtWidgets.QDateEdit()
self.date_to = QtWidgets.QDateEdit()
for w in (self.date_from, self.date_to, ):
w.setDisplayFormat("yyyyMMdd")
w.setCalendarPopup(True)
self.setup_date()
lay = QtWidgets.QHBoxLayout(self)
lay.addWidget(self.date_from)
lay.addWidget(self.date_to)
def setup_date(self):
self.date_from.dateChanged.connect(self.on_date_from_changed)
self.date_to.dateChanged.connect(self.on_date_to_changed)
self.date_from.setDate(QtCore.QDate(2018, 10, 10))
self.date_to.setDate(QtCore.QDate(2018, 10, 11))
@QtCore.pyqtSlot(QtCore.QDate)
def on_date_from_changed(self, date):
self.date_to.setMinimumDate(date.addDays(1))
@QtCore.pyqtSlot(QtCore.QDate)
def on_date_to_changed(self, date):
self.date_from.setMaximumDate(date.addDays(-1))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
You have to use the dateChanged signal to update the maximunDate or minimumDate, adding the appropriate offset according to the case:
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.date_from = QtWidgets.QDateEdit()
self.date_to = QtWidgets.QDateEdit()
for w in (self.date_from, self.date_to, ):
w.setDisplayFormat("yyyyMMdd")
w.setCalendarPopup(True)
self.setup_date()
lay = QtWidgets.QHBoxLayout(self)
lay.addWidget(self.date_from)
lay.addWidget(self.date_to)
def setup_date(self):
self.date_from.dateChanged.connect(self.on_date_from_changed)
self.date_to.dateChanged.connect(self.on_date_to_changed)
self.date_from.setDate(QtCore.QDate(2018, 10, 10))
self.date_to.setDate(QtCore.QDate(2018, 10, 11))
@QtCore.pyqtSlot(QtCore.QDate)
def on_date_from_changed(self, date):
self.date_to.setMinimumDate(date.addDays(1))
@QtCore.pyqtSlot(QtCore.QDate)
def on_date_to_changed(self, date):
self.date_from.setMaximumDate(date.addDays(-1))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
answered Nov 23 '18 at 18:01
eyllanesceyllanesc
87.3k103564
87.3k103564
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449844%2fpyqt-dateedit-max-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I do not understand you, it seems that you are doing a problem where there is not, you could provide a Minimal, Complete, and Verifiable example pointing out what you get in addition to what you expect
– eyllanesc
Nov 23 '18 at 16:34
@eyllanesc I rewrote the question, please give it another read.
– Dirich
Nov 23 '18 at 17:26
of what I understand you have 2 QDateEdit, both with preset values, the first must always be less than the second, and vice versa, am I correct?
– eyllanesc
Nov 23 '18 at 17:31
@eyllanesc yes, you are
– Dirich
Nov 23 '18 at 17:36