Results 1 to 9 of 9

Thread: There is a problem in the code

  1. #1
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default There is a problem in the code

    Hello
    The following code is correct:
    Qt Code:
    1. #include <QtWidgets>
    2. class DigitalClock : public QLCDNumber
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. DigitalClock(QWidget *parent = 0);
    8.  
    9. private slots:
    10. void showTime();
    11. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QtWidgets>
    2. #include "u.h"
    3. DigitalClock::DigitalClock(QWidget *parent)
    4. : QLCDNumber(parent)
    5. {
    6. setSegmentStyle(Filled);
    7.  
    8. QTimer *timer = new QTimer(this);
    9. connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
    10. timer->start(1000);
    11.  
    12. showTime();
    13.  
    14. setWindowTitle(tr("Digital Clock"));
    15. resize(150, 60);
    16. }
    17. void DigitalClock::showTime()
    18. {
    19. [COLOR="#FF0000"] QTime time = QTime:: currentTime(); [/COLOR]
    20. QString text = time.toString("hh:mm");
    21. if ((time.second() % 2) == 0)
    22. text[2] = ' ';
    23. display(text);
    24. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "u.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. DigitalClock clock;
    9. clock.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. QTime time ; time. currentTime();
    To copy to clipboard, switch view to plain text mode 
    thanks
    Last edited by rezas1000; 18th August 2014 at 11:57.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: There is a problem in the code

    The two following piece of code:

    Qt Code:
    1. QTime time = QTime::currentTime();
    To copy to clipboard, switch view to plain text mode 

    is not equivalent to:

    Qt Code:
    1. QTime time; time.currentTime();
    To copy to clipboard, switch view to plain text mode 
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    rezas1000 (18th August 2014)

  4. #3
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: There is a problem in the code

    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.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: There is a problem in the code

    No, why do you think it should be?

    Cheers,
    _

  6. #5
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: There is a problem in the code

    Quote Originally Posted by anda_skoa View Post
    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) {
    .....
    .....

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: There is a problem in the code

    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,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    rezas1000 (23rd August 2014)

  9. #7
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: There is a problem in the code

    The following code :
    Qt Code:
    1. #include <QtWidgets>
    2. #include "u.h"
    3. DigitalClock::DigitalClock(QWidget *parent)
    4. : QLCDNumber(parent)
    5. {
    6. setSegmentStyle(Filled);
    7.  
    8. QTimer *timer = new QTimer(this);
    9. connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
    10. timer->start(1000);
    11.  
    12. showTime();
    13.  
    14. setWindowTitle(tr("Digital Clock"));
    15. resize(150, 60);
    16. }
    17. void DigitalClock::showTime()
    18. {
    19. [COLOR="#FF0000"] QTime time = QTime:: currentTime(); [/COLOR]
    20. QString text = time.toString("hh:mm");
    21. if ((time.second() % 2) == 0)
    To copy to clipboard, switch view to plain text mode 

    "this",where to point?
    thanks.
    Last edited by rezas1000; 23rd August 2014 at 19:16.

  10. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: There is a problem in the code

    Quote Originally Posted by rezas1000 View Post
    The following code :
    "this",where to point?
    thanks.
    Where ever that is, shows himself - in this case, the object of the class DigitalClock.

  11. #9
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: There is a problem in the code

    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.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. Socket thread affinity problem, with code
    By Anslem in forum Newbie
    Replies: 3
    Last Post: 2nd June 2014, 18:21
  2. uic generate code problem.
    By jerry7 in forum Newbie
    Replies: 1
    Last Post: 21st March 2011, 07:59
  3. Problem with code (probably repaintinng label)
    By hakermania in forum Newbie
    Replies: 2
    Last Post: 8th January 2011, 13:58
  4. Whats the problem with my code (http post)
    By newtolinux in forum Qt Programming
    Replies: 2
    Last Post: 10th November 2010, 23:47
  5. Problem compiling Qt 4.7.0 code with d3d10.h
    By pogo11 in forum Qt Programming
    Replies: 2
    Last Post: 12th October 2010, 22:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.