Results 1 to 7 of 7

Thread: Set specific date in DateEdit

  1. #1
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Set specific date in DateEdit

    Hi I have this problem:
    I have a mysql table and in one field I have a date, I want put this date in a DateEdit.
    I make this code:
    dataricovero is a string like this: "2012-01-17"
    Qt Code:
    1. QDate date= QDate::fromString(dataricovero,"yyyy-MM-dd");
    2. ui->dateEditDataricovero->setDate(date);
    To copy to clipboard, switch view to plain text mode 

    but dont work.

    If I do
    Qt Code:
    1. QDate date = QDate::currentDate();
    2. ui->dateEditDataricovero->setDate(date);
    To copy to clipboard, switch view to plain text mode 
    All work fine but I dont want set the current time but the time read in sql query.

    Please help me!
    Thank you

    P.S. Sorry for my english ;(

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Set specific date in DateEdit

    but dont work.
    What about it does not work? This:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. QString dataricovero("2012-01-17");
    8. QDate date= QDate::fromString(dataricovero,"yyyy-MM-dd");
    9. // OR QDate date= QDate::fromString(dataricovero,Qt::ISODate);
    10. qDebug() << date;
    11. e.setDate(date);
    12. e.show();
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    outputs 'QDate("Tue Jan 17 2012")' (English locale obviously) and the date edit contains that date.

    What is the data type of the Mysql column this value is coming from?
    Are you sure that dataricovero is a string in the format you think?

  3. #3
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set specific date in DateEdit

    Thank you for reply.
    Now it works, the error was in the string, it was (2012-01-17 ) with the final space!
    I remove the space and it now work! Thank you.

    I have another problem:
    I want generate one or more QLabel runtime and put it in a container with scrollbar.
    What container I must use?
    And how I can add the QLabel to the container?

    I make this:
    Qt Code:
    1. QVBoxLayout layout;
    2. layout.addWidget(&labelAtt);
    3.  
    4. ui->tabCartella->widget(6)->setLayout(&layout);
    5. labelAtt.show();
    To copy to clipboard, switch view to plain text mode 

    But the label is not show and the QVBoxLayout dont have a scrollbar!

    Thank you again for the answer!

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Set specific date in DateEdit

    You are creating the QVBoxLayout and, I guess, the QLabel on the stack. At the end of that scope these objects will be destroyed, which will remove them form the UI.
    Allocate them on the heap with operator new.

  5. #5
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set specific date in DateEdit

    I dont have undestrand,
    can you explain me with some example?
    Sorry but I'm a new in the world of Qt!
    Thank you!

  6. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Set specific date in DateEdit

    Create a widget with a layout to which you'll be adding new lables then stick that widget into scroll area.
    Something like this:
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. QVBoxLayout* lay = new QVBoxLayout(); //cache this pointer so you can easily add new labels later
    4.  
    5. QWidget* container = new QWidget();
    6. container->setLayout( lay );
    7.  
    8. sa->setWidget( container );
    9.  
    10. this->setCentralWidget( sa );
    11.  
    12. lay->addWidget( new QLabel( "new label" );
    13. }
    To copy to clipboard, switch view to plain text mode 
    Now when you add too many labels to fit on the screen scroll area will show scroll bars to navigate through widgets content.

    Edit:
    Forgot to mention that you have to set
    Qt Code:
    1. sa->setWidgetResizable( true );
    To copy to clipboard, switch view to plain text mode 
    otherwise scroll area will not resize container widget as more contents is added.
    Last edited by Spitfire; 18th January 2012 at 15:39.

  7. #7
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set specific date in DateEdit

    Hi tnak you so much for your help.
    I try the code and it works, but what I want to do is add a scrollarea and put into labels in a QTAbWidget.
    In the mainWindow I have a QTabWidget with the name tabCartella whit 6 tab, I want put a scroll area and labels into the widget 6 (the last).
    I try with this:
    Qt Code:
    1. QVBoxLayout* lay = new QVBoxLayout(); //cache this pointer so you can easily add new labels later
    2.  
    3.  
    4.  
    5. QWidget* container = ui->tabCartella->widget(6);
    6. container->setLayout( lay );
    7.  
    8.  
    9. QScrollArea* sa = new QScrollArea();
    10. sa->setWidget(container);
    11. sa->setWidgetResizable( true );
    12.  
    13. ui->tabCartella->setCurrentWidget(container);
    14.  
    15. lay->addWidget( new QLabel( "new label" ));
    To copy to clipboard, switch view to plain text mode 

    But when I run this code the last tab (tho one where I wans put labels) disappear!
    Please help me
    Thank you so much for your Help.

Similar Threads

  1. Replies: 5
    Last Post: 14th February 2011, 14:06
  2. dateEdit, editingFinished, isolate return key press
    By meena in forum Qt Programming
    Replies: 5
    Last Post: 1st July 2010, 06:32
  3. get date
    By mmm286 in forum Newbie
    Replies: 2
    Last Post: 17th February 2010, 09:18
  4. date
    By AnithaRagupathy in forum Qt Programming
    Replies: 3
    Last Post: 2nd November 2007, 06:28
  5. How to default Date-Edit Widget to system date
    By JohnToddSr in forum Qt Tools
    Replies: 4
    Last Post: 17th January 2007, 19:18

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.