PDA

View Full Version : There is a problem in the code



rezas1000
18th August 2014, 12:50
Hello
The following code is correct:

#include <QtWidgets>
class DigitalClock : public QLCDNumber
{
Q_OBJECT

public:
DigitalClock(QWidget *parent = 0);

private slots:
void showTime();
};


#include <QtWidgets>
#include "u.h"
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);
}
void DigitalClock::showTime()
{
QTime time = QTime:: currentTime();
QString text = time.toString("hh:mm");
if ((time.second() % 2) == 0)
text[2] = ' ';
display(text);
}

#include <QApplication>

#include "u.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DigitalClock clock;
clock.show();
return app.exec();
}

and pro is;

QT += gui core widgets
SOURCES += \
main.cpp \
u.cpp

HEADERS += \
u.h

But when I change the colored part as follows:

Not run as before:

QTime time ; time. currentTime();
thanks

wysota
18th August 2014, 12:54
The two following piece of code:


QTime time = QTime::currentTime();

is not equivalent to:


QTime time; time.currentTime();
The first one assigns the return value of QTime::currentTime() to the 'time' variable, the second one doesn't assign anything anywhere resulting in the 'time' variable containing its default (invalid time) value.

rezas1000
23rd August 2014, 18:44
Hello
If the code is correct :

digitalClock::digitalClock(QWidget *parent)

: QLCDNumber(parent) {
....
....


The following code should be correct?!!!!!!!!

class a
{public:
a(int x):int(x){}
};

Please explain
thanks.

anda_skoa
23rd August 2014, 18:47
No, why do you think it should be?

Cheers,
_

rezas1000
23rd August 2014, 19:04
No, why do you think it should be?

Cheers,
_

Because int is a data type And QLCDNumber is data type.

for QLCDNumber,What happens in the following code?


digitalClock::digitalClock(QWidget *parent)

: QLCDNumber(parent) {
.....
.....

anda_skoa
23rd August 2014, 19:11
But digitalClock is related to QLCDNumber, it is derived from it (a subclass of QLCFNumber).
The code is calling the base class constructor with an argument provided to the subclass constructor.

There is no such relation between "a" and "int".

Cheers,
_

rezas1000
23rd August 2014, 19:33
The following code :

#include <QtWidgets>
#include "u.h"
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);
}
void DigitalClock::showTime()
{
QTime time = QTime:: currentTime();
QString text = time.toString("hh:mm");
if ((time.second() % 2) == 0)

"this",where to point?
thanks.

Lesiok
23rd August 2014, 22:47
The following code :
"this",where to point?
thanks.
Where ever that is, shows himself - in this case, the object of the class DigitalClock.

faldzip
25th August 2014, 15:01
It seems that you do not have basic C++ knowledge, which is required to create a more complex application than "hello world".
My advice is to start from the beginning - get the C++ book and start with the HelloWorld and then slowly get through the classes and structs to inheritance and virtual functions. Then you'll be ready to develop your Qt application, as this is the knowledge you *must* have.
For example: You can't write the book in German language if you don't know this language, right? First you have to learn German and then start writing a book.