PDA

View Full Version : Set specific date in DateEdit



gfucka
17th January 2012, 00:39
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"

QDate date= QDate::fromString(dataricovero,"yyyy-MM-dd");
ui->dateEditDataricovero->setDate(date);

but dont work.

If I do

QDate date = QDate::currentDate();
ui->dateEditDataricovero->setDate(date);
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 ;(

ChrisW67
17th January 2012, 01:48
but dont work. What about it does not work? This:


#include <QtGui>
#include <QDebug>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QString dataricovero("2012-01-17");
QDate date= QDate::fromString(dataricovero,"yyyy-MM-dd");
// OR QDate date= QDate::fromString(dataricovero,Qt::ISODate);
qDebug() << date;
QDateEdit e;
e.setDate(date);
e.show();
return app.exec();
}

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?

gfucka
17th January 2012, 02:02
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:


QVBoxLayout layout;
layout.addWidget(&labelAtt);

ui->tabCartella->widget(6)->setLayout(&layout);
labelAtt.show();

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

Thank you again for the answer!

ChrisW67
17th January 2012, 02:43
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.

gfucka
17th January 2012, 11:33
I dont have undestrand,
can you explain me with some example?
Sorry but I'm a new in the world of Qt!
Thank you!

Spitfire
18th January 2012, 15:23
Create a widget with a layout to which you'll be adding new lables then stick that widget into scroll area.
Something like this:



MainWindow::MainWindow()
{
QVBoxLayout* lay = new QVBoxLayout(); //cache this pointer so you can easily add new labels later

QWidget* container = new QWidget();
container->setLayout( lay );

QScrollArea* sa = new QScrollArea();
sa->setWidget( container );

this->setCentralWidget( sa );

lay->addWidget( new QLabel( "new label" );
}
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
sa->setWidgetResizable( true );
otherwise scroll area will not resize container widget as more contents is added.

gfucka
26th January 2012, 11:06
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:


QVBoxLayout* lay = new QVBoxLayout(); //cache this pointer so you can easily add new labels later



QWidget* container = ui->tabCartella->widget(6);
container->setLayout( lay );


QScrollArea* sa = new QScrollArea();
sa->setWidget(container);
sa->setWidgetResizable( true );

ui->tabCartella->setCurrentWidget(container);

lay->addWidget( new QLabel( "new label" ));


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.