[SOLVED] --Digital Clock not displaying properly in QDockWidget
-
I took the dockWidgets example app and combined it with the digitalclock app so I could display hours:minutes:seconds in the dockwidget.
All is well, but the clock only shows minutes:seconds in the dockwidget.
here's my dockwidget code in the mainwindow
//================================================QDockWidget *clockDock = new QDockWidget(this);
clockDock->setFixedWidth(520);
clockDock->setFixedHeight(100);
//clockDock->set
clockDock->setAllowedAreas(Qt::LeftDockWidgetArea);
DigitalClock *digitalClock = new DigitalClock(clockDock);
clockDock->setWidget(digitalClock);
addDockWidget(Qt::LeftDockWidgetArea, clockDock);
//================================================and here's the digital clock class:
//=================================================
#include <QtWidgets>#include "digitalclock.h"
//! [0]
DigitalClock::DigitalClock(QWidget *parent)
: QLCDNumber(parent)
{
setSegmentStyle(Filled);QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
timer->start(1000);showTime();
//setWindowTitle(tr("Digital Clock"));
//resize(150, 60);
}
//! [0]//! [1]
void DigitalClock::showTime()
//! [1] //! [2]
{
QTime time = QTime::currentTime();
// QString text = time.toString("hh:mm");
QString text = time.toString("hh:mm:ss");
// if ((time.second() % 2) == 0)
// text[2] = ' ';
display(text); // DOES NOT DISPLAY hh:mm:ss, but only mm:ss !!
}
//! [2]
//=================================================What am I doing wrong?
-
It turns out that one may disable all features of a QDockWidget by doing
clockDock->setFeatures(QDockWidget::NoDockWidgetFeatures); or set only desired features by doing clockDock->setFeatures(QDockWidget::DockWidgetMovable |
QDockWidget::DockWidgetFloatable); //DockWidgetClosableand the reason the digital clock was not displaying hh:mm:ss when the example app displayed hh:mm without a problem is because the QLCDNumber class only displays 5 "digits" by default. In this case each "digit" correlates to the number of characters returned by
QString text = time.toString("hh:mm:ss");
There are 8 characters in text variable, so I increased the DigitCount attribute from 5 to 8 by doing
setDigitCount(8);
and the whole time format gets displayed properly.