C++ 如何获取QCalendarWidget中显示的所有日期
我在应用程序中使用了一个QCalendarWidget,我重载了updateCells方法,以便在满足特定条件的每个日期上放置一个红色背景 我的问题是我不知道如何在日历中显示第一个日期(不是当月的第一个日期),以及最后一个日期。 示例:在2月份,第一个显示的日期是1月25日,最后一个显示的日期是3月7日 QCalendarWidget中没有任何有用的方法,我想不出一个算法来解决这个问题。C++ 如何获取QCalendarWidget中显示的所有日期,c++,qt,calendar,C++,Qt,Calendar,我在应用程序中使用了一个QCalendarWidget,我重载了updateCells方法,以便在满足特定条件的每个日期上放置一个红色背景 我的问题是我不知道如何在日历中显示第一个日期(不是当月的第一个日期),以及最后一个日期。 示例:在2月份,第一个显示的日期是1月25日,最后一个显示的日期是3月7日 QCalendarWidget中没有任何有用的方法,我想不出一个算法来解决这个问题。 您知道怎么做吗?由于您可以访问当前显示的月份和年份,您可以在显示内容的第一个和最后一个日期使用。考虑到这一点
您知道怎么做吗?由于您可以访问当前显示的月份和年份,您可以在显示内容的第一个和最后一个日期使用。考虑到这一点,您无法决定您必须来回走多远。由于您可以访问当前显示的月份和年份,您可以在显示内容的第一个和最后一个日期使用。考虑到这一点,您可能无法决定要来回走多远。QCalendarWidget类是更简单的Widget的组合,您可以查看Qt源代码以获取帮助。只需使用循环即可获取以下日期之间的所有日期:
class myQCalendar(QCalendarWidget):
"""custum QCalendarWidget"""
def __init__(self, parent=None):
self.table = self.findChild(QTableView)
def return_first_last_dates(self) -> Tuple[QDate, QDate]:
# first row(0) and col(0) - headers, so we use second(1,1)
first_date = self.date_by_index(
self.table.model().index(1, 1))
last_date = self.date_by_index(
self.table.model().index(6, 7))
return first_date, last_date
# self.table.model().dateForCell(1,1) didn't work in python
# maybe it will work in c++
def date_by_index(self, index: QModelIndex) -> QDate:
""" Return QDate by index of model of QTableView """
date = self.selectedDate()
day = int(index.data())
mnth = date.month() # current month always the same as current date
if day > 15 and index.row() < 3: # qcalendar always display 6 rows( and 0 - is header)
mnth = date.month() - 1
if day < 15 and index.row() > 4:
mnth = date.month() + 1
return QDate(date.year(), mnth, day)
类myQCalendar(QCalendarWidget):
“custum QCalendarWidget”
def uuu init uuu(self,parent=None):
self.table=self.findChild(QTableView)
def return\u first\u last\u dates(self)->元组[QDate,QDate]:
#第一行(0)和列(0)-标题,所以我们使用第二行(1,1)
first\u date=self.date\u by\u索引(
self.table.model().index(1,1))
last_date=self.date_by_索引(
self.table.model()索引(6,7))
返回第一个\u日期、最后一个\u日期
#self.table.model().dateForCell(1,1)在python中不起作用
也许它会在C++中工作
定义日期索引(self,索引:QModelIndex)->QDate:
“”“按QTableView模型的索引返回QDate”“”
日期=self.selectedDate()
day=int(index.data())
mnth=日期.月份()#当前月份始终与当前日期相同
如果day>15且index.row()<3:#qcalendar始终显示6行(0-为标题)
mnth=日期.月份()-1
如果日期<15且index.row()大于4:
mnth=日期.月份()+1
返回QDate(date.year(),mnth,day)
QCalendarWidget类是更简单的Widget的组合,您可以查看Qt源代码以获取帮助。只需使用循环即可获取以下日期之间的所有日期:
class myQCalendar(QCalendarWidget):
"""custum QCalendarWidget"""
def __init__(self, parent=None):
self.table = self.findChild(QTableView)
def return_first_last_dates(self) -> Tuple[QDate, QDate]:
# first row(0) and col(0) - headers, so we use second(1,1)
first_date = self.date_by_index(
self.table.model().index(1, 1))
last_date = self.date_by_index(
self.table.model().index(6, 7))
return first_date, last_date
# self.table.model().dateForCell(1,1) didn't work in python
# maybe it will work in c++
def date_by_index(self, index: QModelIndex) -> QDate:
""" Return QDate by index of model of QTableView """
date = self.selectedDate()
day = int(index.data())
mnth = date.month() # current month always the same as current date
if day > 15 and index.row() < 3: # qcalendar always display 6 rows( and 0 - is header)
mnth = date.month() - 1
if day < 15 and index.row() > 4:
mnth = date.month() + 1
return QDate(date.year(), mnth, day)
类myQCalendar(QCalendarWidget):
“custum QCalendarWidget”
def uuu init uuu(self,parent=None):
self.table=self.findChild(QTableView)
def return\u first\u last\u dates(self)->元组[QDate,QDate]:
#第一行(0)和列(0)-标题,所以我们使用第二行(1,1)
first\u date=self.date\u by\u索引(
self.table.model().index(1,1))
last_date=self.date_by_索引(
self.table.model()索引(6,7))
返回第一个\u日期、最后一个\u日期
#self.table.model().dateForCell(1,1)在python中不起作用
也许它会在C++中工作
定义日期索引(self,索引:QModelIndex)->QDate:
“”“按QTableView模型的索引返回QDate”“”
日期=self.selectedDate()
day=int(index.data())
mnth=日期.月份()#当前月份始终与当前日期相同
如果day>15且index.row()<3:#qcalendar始终显示6行(0-为标题)
mnth=日期.月份()-1
如果日期<15且index.row()大于4:
mnth=日期.月份()+1
返回QDate(date.year(),mnth,day)