Dear,
I come before you with a problem that is currently bothering me. I'm trying to create a custom calendar in Qt, since the QCalendarWidget
can not be modified or stylized, I'm doing my own.
The problem is when I try to connect the date buttons. To try to get the button I try to list them from the main container of them which is a QGirdLayout
, but in doing so I always return the same widget regardless of the position of this one.
I attach the code and console image.
import calendar
import sys
import time
from PyQt4.QtCore import QSize
from PyQt4.QtGui import QApplication
from PyQt4.QtGui import QGridLayout
from PyQt4.QtGui import QPushButton
from PyQt4.QtGui import QWidget
class DayButton(QPushButton):
def __init__(self, day):
QPushButton.__init__(self)
self.setText(str(day))
self.setCheckable(True)
self.setFixedSize(QSize(40, 40))
class Calendar(QWidget):
def __init__(self):
QWidget.__init__(self)
self.today = time.strftime("%x").split("/")
self.__grid = QGridLayout()
self.__grid.setSpacing(4)
self.setCalendar()
self.setLayout(self.__grid)
def setCalendar(self):
selectedDate = calendar.monthrange(int(self.today[2]), int(self.today[0]))
counter, day = 0, 1
for i in range(6):
for j in range(7):
if counter < selectedDate[0] or day > selectedDate[1]:
counter += 1
continue
self.number = DayButton(str(day))
self.number.released.connect(self.checkChecked)
self.__grid.addWidget(self.number, i, j)
day += 1
def checkChecked(self):
for item in range(self.__grid.count()):
print self.__grid.itemAt(item)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Calendar()
main.show()
sys.exit(app.exec_())
As you can see, the same object always returns, which is not even the type of widget I'm adding.