How can I get cell (x, y) position from QCalendarWidget?
Question:
I am trying to get QCalendarWidget’s Cell position.
from the upper image, it’s cell position must be tuple: (2,4)
I would also appreciate if I can get the cell date and find it’s position on another library.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QCalendarWidget
from PyQt5.QtCore import QDate
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
cal = QCalendarWidget(self)
cal.setGridVisible(True)
cal.clicked[QDate].connect(self.showDate)
self.lbl = QLabel(self)
date = cal.selectedDate()
self.lbl.setText(date.toString())
vbox = QVBoxLayout()
vbox.addWidget(cal)
vbox.addWidget(self.lbl)
self.setLayout(vbox)
self.setWindowTitle('QCalendarWidget')
self.setGeometry(300, 300, 400, 300)
self.show()
def showDate(self, date):
self.lbl.setText(date.toString())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
Answers:
UPDATE
Ok, I’m sorry, my code didn’t meet your demand.
Here you are.
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QCalendarWidget, QTableView
from PyQt6.QtCore import QDate, Qt, QModelIndex
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.cal = QCalendarWidget(self)
self.cal.setGridVisible(True)
self.cal.clicked[QDate].connect(self.showDate)
self.lbl = QLabel(self)
date = self.cal.selectedDate()
self.lbl.setText(date.toString())
vbox = QVBoxLayout()
vbox.addWidget(self.cal)
vbox.addWidget(self.lbl)
self.setLayout(vbox)
self.setWindowTitle('QCalendarWidget')
self.setGeometry(300, 300, 400, 300)
self.show()
def showDate(self, date):
self.lbl.setText(date.toString())
child = self.findChild(QTableView)
c = child.currentIndex()
data = child.model().data(c)
print(c.column() , c.row())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec())
Explanation
QCalendarWidget
is made from some widgets containing QTableView
.
So we get the QTableView
by findChild method.
If you click the tile of QTableView, the index is fixed.
The index has row, column.
this solution is a little dirty, but better.
I delete my previous answer.
I am trying to get QCalendarWidget’s Cell position.
from the upper image, it’s cell position must be tuple: (2,4)
I would also appreciate if I can get the cell date and find it’s position on another library.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QCalendarWidget
from PyQt5.QtCore import QDate
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
cal = QCalendarWidget(self)
cal.setGridVisible(True)
cal.clicked[QDate].connect(self.showDate)
self.lbl = QLabel(self)
date = cal.selectedDate()
self.lbl.setText(date.toString())
vbox = QVBoxLayout()
vbox.addWidget(cal)
vbox.addWidget(self.lbl)
self.setLayout(vbox)
self.setWindowTitle('QCalendarWidget')
self.setGeometry(300, 300, 400, 300)
self.show()
def showDate(self, date):
self.lbl.setText(date.toString())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
UPDATE
Ok, I’m sorry, my code didn’t meet your demand.
Here you are.
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QCalendarWidget, QTableView
from PyQt6.QtCore import QDate, Qt, QModelIndex
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.cal = QCalendarWidget(self)
self.cal.setGridVisible(True)
self.cal.clicked[QDate].connect(self.showDate)
self.lbl = QLabel(self)
date = self.cal.selectedDate()
self.lbl.setText(date.toString())
vbox = QVBoxLayout()
vbox.addWidget(self.cal)
vbox.addWidget(self.lbl)
self.setLayout(vbox)
self.setWindowTitle('QCalendarWidget')
self.setGeometry(300, 300, 400, 300)
self.show()
def showDate(self, date):
self.lbl.setText(date.toString())
child = self.findChild(QTableView)
c = child.currentIndex()
data = child.model().data(c)
print(c.column() , c.row())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec())
Explanation
QCalendarWidget
is made from some widgets containing QTableView
.
So we get the QTableView
by findChild method.
If you click the tile of QTableView, the index is fixed.
The index has row, column.
this solution is a little dirty, but better.
I delete my previous answer.