Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QCalendarWidget tooltip



  • Hello!!

    Does anyone know how to assign a tooltip to each of the cells in a qcalendarwidget?

    Thank you very much!


  • Lifetime Qt Champion

    Hi,

    From the looks of it, you will have to install an event filter to catch the QEvent::ToolTip event and react on that.



  • @SGaist Can you help me with that? I read that I need to reimplement the QCalendarWidget class. I have this class:

    #include "calendarmanager.h"
    
    CalendarManager::CalendarManager(QWidget *parent)
        : QCalendarWidget (parent)
    {
    //    m_outlinePen.setColor(QColor(213,116,23));
        m_outlinePen.setColor(Qt::transparent);
        m_transparentBrush.setColor(QColor(213,116,23,50));
    
        getDates();
    }
    
    CalendarManager::~CalendarManager()
    {
    
    }
    
    void CalendarManager::setColor(const QColor &color) {
        m_outlinePen.setColor(color);
    }
    
    QColor CalendarManager::getColor() const {
        return ( m_outlinePen.color() );
    }
    
    void CalendarManager::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const {
        QCalendarWidget::paintCell(painter, rect, date);
    
        if (m_dates.contains(date)) {
            painter->setPen(m_outlinePen);
            painter->setBrush(QBrush(QColor(213,116,23,50)));
            painter->drawRect(rect.adjusted(0, 0, -1, -1));
        }
    }
    
    void CalendarManager::getDates() {
    //    QFile file("C:\\Users\\Ivan\\Desktop\\prueba.txt");
    //    if (!file.open(QIODevice::ReadOnly)) {
    //        // Error code
    //    }
    
    //    QList<QByteArray> wordList;
    //    QDate date;
    //    QString name;
    //    calendarEvent e;
    
    //    while (!file.atEnd()) {
    //        QByteArray line = file.readLine();
    //        wordList = line.split(',');
    
    //        date = QDate::fromString(wordList.first(), "dd/MM/yyyy");
    //        name = wordList.last();
    
    //        e.date = date;
    //        e.name = name;
    
    //        m_events.append(e);
    //        m_dates.append(date);
    //    }
    //    file.close();
    }
    
    void CalendarManager::setDateState(QDate dtDate, bool bState) {
        /* Esta es la funcionalidad mínima necesaria para pintar un día en el calendario */
        calendarEvent e;
        e.date = dtDate;
    
        if (bState) {
            m_events.append(e);
            m_dates.append(dtDate);
        } else {
            for (int iDate = 0; iDate < m_events.size(); iDate++) {
                if (m_events.at(iDate).date == e.date)
                    m_events.removeAt(iDate);
                if (m_dates.at(iDate) == e.date)
                    m_dates.removeAt(iDate);
            }
        }
    }
    
    

    How I have to proceed with the event filter? I think I read that I need it inside the paintCell function but I don't know how to do it.

    Thank you very much!


  • Lifetime Qt Champion

    You have an example of event filter here.

    What kind of tool tip do you want to show ?



  • @SGaist I only want to show a simple text associated to the selected day


  • Lifetime Qt Champion

    In your event filter get the Qt's event and then us a QToolTip to check the dialog you want to build.


Log in to reply