问题
I want to be able to highlight a day from my SQL Database on my calendar widget just like how the current day is highlighted when you start the program, in my example it is a red highlight.
What i want to happen is when the user presses on the date that is highlighted the text that is alongside the date in the databse is displayed on the label below the calendar.
This is my page i made using QT Designer:
PYQT Page
This is the database that has all the data:
SQL Database
And this is the code that creates the QT Page:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Calendar.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
import datetime
import PyQt5
from PyQt5.QtCore import QDate, Qt
from PyQt5.QtGui import (QColor, QFont, QTextCharFormat, QTextLength,
QTextTableFormat)
from PyQt5.QtWidgets import (QApplication, QComboBox, QDateTimeEdit,
QHBoxLayout, QLabel, QMainWindow, QSpinBox, QTextBrowser, QVBoxLayout,
QWidget)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1200, 900)
MainWindow.setMinimumSize(QtCore.QSize(0, 0))
MainWindow.setMaximumSize(QtCore.QSize(1200, 900))
MainWindow.setStyleSheet("background-color: rgb(200, 200, 200);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Header = QtWidgets.QTextEdit(self.centralwidget)
self.Header.setEnabled(False)
self.Header.setGeometry(QtCore.QRect(-10, 0, 1300, 110))
self.Header.setMinimumSize(QtCore.QSize(1300, 110))
self.Header.setMaximumSize(QtCore.QSize(1300, 110))
self.Header.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"border-color: rgb(255, 255, 255);\n"
"border-width : 1.2px;\n"
"border-style:inset;")
self.Header.setObjectName("Header")
self.LECTURP = QtWidgets.QLabel(self.centralwidget)
self.LECTURP.setGeometry(QtCore.QRect(512, 2, 180, 61))
self.LECTURP.setStyleSheet("color: rgb(0, 176, 240);\n"
"font: 8pt \"MS Shell Dlg 2\";\n"
"text-decoration: underline;\n"
"background-color: rgb(255, 255, 255);\n"
"font: 28pt \"Calbri\";\n"
"text-decoration: underline;")
self.LECTURP.setObjectName("LECTURP")
self.LecturpBanner = QtWidgets.QLabel(self.centralwidget)
self.LecturpBanner.setGeometry(QtCore.QRect(320, 60, 561, 31))
self.LecturpBanner.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"color: rgb(0, 176, 240);\n"
"font: 14pt \"Calibri\";")
self.LecturpBanner.setObjectName("LecturpBanner")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(30, 110, 1141, 661))
self.calendarWidget.setStyleSheet("alternate-background-color: rgb(255, 255, 255);\n"
"font: 75 16pt \"MS Shell Dlg 2\";\n"
"background-color: rgb(200, 200, 200);\n"
"selection-background-color: rgb(255, 0, 0);")
self.calendarWidget.setObjectName("calendarWidget")
self.DateInfoOutput = QtWidgets.QLabel(self.centralwidget)
self.DateInfoOutput.setGeometry(QtCore.QRect(30, 778, 1141, 97))
self.DateInfoOutput.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"border-color: rgb(0, 0, 0);\n"
"font: 16pt \"Calibri\";\n"
"border-width : 1.2px;\n"
"border-style:inset;")
self.DateInfoOutput.setText("")
self.DateInfoOutput.setObjectName("DateInfoOutput")
self.WeekNumber = QtWidgets.QLabel(self.centralwidget)
self.WeekNumber.setGeometry(QtCore.QRect(56, 168, 113, 49))
self.WeekNumber.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"font: 16pt \"MS Shell Dlg 2\";")
self.WeekNumber.setObjectName("WeekNumber")
self.calendarWidget.raise_()
self.Header.raise_()
self.LECTURP.raise_()
self.LecturpBanner.raise_()
self.DateInfoOutput.raise_()
self.WeekNumber.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.editor = QTextBrowser()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.LECTURP.setText(_translate("MainWindow", "LECTURP"))
self.LecturpBanner.setText(_translate("MainWindow", "Lecture, Exam, Coursework, Timetable, Uploader and Reminder Program."))
self.WeekNumber.setText(_translate("MainWindow", "Week No."))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
In my code i attempted to do this using this code. This part of the code gives an error where the date = Qdate(All_Dates, 3) part gives an error.
The error that is created is this: arguments did not match any overloaded call: QDate(): too many arguments QDate(int, int, int): argument 1 has unexpected type 'str' QDate(int, int, int, QCalendar): argument 1 has unexpected type 'str' The code below is just to create the QT page:
connection = sqlite3.connect("Calendardatabase.db")
# This is the code for the database cursor
crsr = connection.cursor()
Count = 1
crsr.execute('SELECT Day FROM Calendar WHERE Number = ?', (Count,))
RawDay = crsr.fetchone()
Day = str(RawDay)
Day = Day.replace('(', '')
Day = Day.replace(')', '')
Day = Day.replace(',', '')
Day = int(Day)
crsr.execute('SELECT Month FROM Calendar WHERE Number = ?', (Count,))
RawMonth = crsr.fetchone()
Month = str(RawMonth)
Month = Month.replace('(', '')
Month = Month.replace(')', '')
Month = Month.replace(',', '')
Month = int(Month)
crsr.execute('SELECT Year FROM Calendar WHERE Number = ?', (Count,))
RawYear = crsr.fetchone()
Year = str(RawDay)
Year = Year.replace('(', '')
Year = Year.replace(')', '')
Year = Year.replace(',', '')
Year = int(Year)
Current_Day = datetime.date.today().strftime("%A,")
Current_Date = datetime.date.today().strftime("%d")
Current_Month = datetime.date.today().strftime("%B")
Combined_Date = ("It is a " + Current_Day + " It is the " + Current_Date + " of " + Current_Month)
Combined_Date = str(Combined_Date)
self.DateInfoOutput.setText(Combined_Date)
cursor = self.editor.textCursor()
self.selectedDate = QDate.currentDate()
All_Dates = (Day, Month, Year)
All_Dates = str(All_Dates)
date = QDate(All_Dates, 3)
format = cursor.charFormat()
format.setFontPointSize(self.fontSize)
boldFormat = QTextCharFormat(format)
boldFormat.setFontWeight(QFont.Bold)
highlightedFormat = QTextCharFormat(boldFormat)
highlightedFormat.setBackground(Qt.yellow)
cell = table.cellAt(0, weekDay-1)
cellCursor = cell.firstCursorPosition()
cellCursor.insertText(QDate.longDayName(weekDay), boldFormat)
回答1:
You need to connect to the clicked(date) signal.
def dateClicked(self, clickedDate):
connection = sqlite3.connect("Calendardatabase.db")
crsr = connection.cursor()
crsr.execute(
'SELECT Text FROM Calendar WHERE Day = ? AND Month = ? AND YEAR = ?',
(clickedDate.day(), clickedDate.month(), clickedDate.year()))
result = crsr.fetchone()
if result:
self.dateInfoOutput.setText(result[0])
else:
self.dateInfoOutput.setText('')
Besides that:
- Never, NEVER edit the files created with pyuic, use them as modules instead. Read more on using Designer.
- Avoid using uppercase names for variables and attributes.
- Avoid fixed position and size of widgets unless it is really necessary (which usually isn't); prefer layouts instead, they are easily accessible in the widget box of Designer or can be set on existing widget from their context menu.
- When providing example code in questions, always ensure that they are both minimal and reproducible.
来源:https://stackoverflow.com/questions/60645241/how-to-output-sql-data-onto-a-qcalendarwidget