Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: display time as as string ?

  1. #1
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default display time as as string ?

    How can I display trme as a string ?
    Could someone give me some code to be able to do that ?
    I am using a TimeEdit window. So suppose someone edits it. I would like the editted time to show up in a mesage box.

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: display time as as string ?

    QString str = "time";


    sorry couldn't resist.

    RTFM
    Qt has really good documentation. From within Qt Creator, Click on Help-Index and type QDateTime - that should help you on your way.

  3. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: display time as as string ?

    see QTime::toString()

  4. #4
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: display time as as string ?

    Hi

    Hers the code for you

    Qt Code:
    1. QTimeEdit timeEdit;
    2.  
    3. QString timeInString = timeEdit.time().toString("h:m:s ap");
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: display time as as string ?

    Well it does it . It displays 12:00 in a message box.
    But it does not display the update time when the user edits the EditTime box.
    I tried
    Qt Code:
    1. QString timeInString = QString timeInString = timeEdit.timeChanged().time().toString("h:m:s ap");
    To copy to clipboard, switch view to plain text mode 
    But it gave a parameter error on timeChanged .
    I am flumoxed here.

  6. #6
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: display time as as string ?

    You can't use timeChanged() the way you do.
    It's a [signal] and you must connect it to a [slot].

  7. #7
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: display time as as string ?

    ok. What i need is some way to retrieve the text from QTimeEdit and display it in a messagebox.

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: display time as as string ?

    A pointer of QTimeEdit in the messagebox

  9. #9
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: display time as as string ?

    Here is the code:
    Qt Code:
    1. void calendartime:: display_message()
    2. {
    3. QMessageBox msgbox;
    4. QTimeEdit *timeEdit;
    5. QString timeInString = timeEdit.time().toString("h:m:s ap");
    6. msgbox.setText(timeInString) ;
    7. msgbox.exec();
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    I get the following error:
    calendartime.cpp:44: error: request for member 'time' in 'timeEdit', which is of non-class type 'QTimeEdit*'

  10. #10
    Join Date
    Jun 2007
    Location
    Massachusetts, USA
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: display time as as string ?

    Try
    Qt Code:
    1. QString timeInString = timeEdit->time().toString("h:m:s ap");
    To copy to clipboard, switch view to plain text mode 
    (Note "timeEdit->" instead of "timeEdit.".)

  11. #11
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: display time as as string ?

    I get calendartime.cpp:44: error: base operand of '->' has non-pointer type 'QTimeEdit'
    Then I tried this code
    Qt Code:
    1. void calendartime:: display_message()
    2. {
    3.  
    4.  
    5. QMessageBox msgbox;
    6. QTimeEdit *timeEdit;
    7. QString text = timeEdit->time().toString("h:m:s ap");
    8.  
    9. msgbox.setText(text);
    10. msgbox.exec();
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 
    and it crashed withc process id 4192. I suspect this has to something with the pointer to QTimeEdit.
    I am starting to think it can't be done..
    Last edited by Petr_Kropotkin; 27th January 2010 at 22:41.

  12. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: display time as as string ?

    Your allocating timeEdit on the stack and not initialising the object, so your dereferencing a random memory location. It's going to crash. Create the object with 'new' first.

    eg. timeEdit = new QTimeEdit(this);

    or don't use a pointer and use a value instead (omit the '*' in the declaration)

  13. #13
    Join Date
    Jun 2007
    Location
    Massachusetts, USA
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: display time as as string ?

    On closer look, 'timeEdit' is a pointer that is never initialized. You need to create the object or retrieve the time from somewhere. Do you have a QTimeEdit object somewhere else in your code?

  14. #14
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: display time as as string ?

    It runs but then when you change the time it crashes and gives me : Visual C++ Runtime Error. It terminates in an unusal way.
    I removed the message box to eee if things would run .
    Last edited by Petr_Kropotkin; 28th January 2010 at 00:09.

  15. #15
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: display time as as string ?

    I just did that
    In the ui_ calendartime.h I added this line
    Qt Code:
    1. timeEdit->text();
    To copy to clipboard, switch view to plain text mode 

    In the calendartime.h I changed the function to
    Qt Code:
    1. display_message(QTimeEdit &time)
    To copy to clipboard, switch view to plain text mode 

    and in calendartime.cpp I changed the connnect function to

    Qt Code:
    1. connect(acceptButton, SIGNAL(clicked(bool)),this , SLOT(display_message(QTimeEdit &)))
    To copy to clipboard, switch view to plain text mode 

    and here is display_message
    Qt Code:
    1. void calendartime:: display_message(QTimeEdit &time)
    2. {
    3.  
    4. QMessageBox msgbox;
    5. QString text = time.time().toString("h:m:s ap");
    6.  
    7. msgbox.setText(text);
    8. msgbox.exec();
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    It ran but crashed.

  16. #16
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: display time as as string ?

    Make a connection like this:
    Qt Code:
    1. connect(acceptButton, SIGNAL(clicked()), this, SLOT(display_message()));
    To copy to clipboard, switch view to plain text mode 

    Then:
    Qt Code:
    1. void calendartime:: display_message()
    2. {
    3. QMessageBox msgbox;
    4. QString text = timeEdit.time().toString("h:m:s ap");
    5.  
    6. msgbox.setText(text);
    7. msgbox.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 
    In this case, timeEdit is the name of the QTimeEdit you used in your form.

    And don't mess around in ui_ files. Changes you make there will be lost after compilation.

  17. #17
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: display time as as string ?

    The original connect function was display_message().
    Without declaring QTimeEdit, it crashed.
    Here is the old version
    Qt Code:
    1. void calendartime:: display_message()
    2. {
    3. QMessageBox msgbox;
    4. QTimeEdit timeEdit;
    5. QString timeInString = timeEdit.time().toString("h:m:s ap");
    6. msgbox.setText(timeInString);
    7. msgbox.exec();
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    It runs but does not display the changes from QTimeEdit
    Last edited by Petr_Kropotkin; 28th January 2010 at 16:46.

  18. #18
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: display time as as string ?

    So what *does* it do?

  19. #19
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: display time as as string ?

    it jiust displays 12:00 pm

  20. #20
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: display time as as string ?

    Which is correct, as you are creating a QTimeEdit and not passing in a QTime to set the time to, so the default is 12:00pm. If you wish to display a different value, pass in a QTime object which contains the appropriate time.

Similar Threads

  1. real time data display
    By hammer256 in forum Qt Programming
    Replies: 13
    Last Post: 25th March 2013, 16:47
  2. How to display individual character of a string?
    By cooper in forum Qt Programming
    Replies: 2
    Last Post: 15th July 2009, 05:19
  3. Display a QWidget on two X-Server at the same time
    By tarod in forum Qt Programming
    Replies: 0
    Last Post: 1st July 2008, 12:55
  4. QTableView does not display time string correctly
    By ad5xj in forum Qt Programming
    Replies: 1
    Last Post: 5th August 2007, 20:35
  5. Display the camera frame in real time
    By alex_lue in forum Qt Programming
    Replies: 8
    Last Post: 27th July 2007, 10:31

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
  •  
Qt is a trademark of The Qt Company.