I want to make a specific date like 4-5-2016 (string or int) and convert it into a valid object that QDate can interact with.
Is there a way to convert or create a QDate from a string or an int in PySide?
Yes you can use QDate.fromString() or QDate.fromJulianDay()
Example:
d = QDate.fromString("4-5-2015", "d-M-yyyy")
See the QDate documentation
Related
I have to read numbers from line edit in Qt Creator, and then divide them by 100 and display them in a label.
The only method I know is:
QString OOP_marks = ui->lineEdit_OOP_marks_input->text();
ui->label_OOP_marks->setText(QString(OOP_marks));
But the above method cannot read numbers; it reads string. I have tried a lot but can't figure out the code for this part of the program.
You can use QString::toDouble for converting string to number. If you are planning to accept integers use QString::toInt instead. To convert number to string again QString::number is way to go.
const auto& OOP_marks = ui->lineEdit_OOP_marks_input->text();
bool isNumber; // optional
const double number = OOP_marks.toDouble(&isNumber);
const auto& result = isNumber ? QString::number(number / 100) : QString("Please enter valid number.");
ui->label_OOP_marks->setText(result);
But I suggest you to use QSpinBox or QDoubleSpinBox, instead of QLineEdit.
How about QString toInt function, and then static QString::number function?
QString OOP_marks = ui->lineEdit_OOP_marks_input->text();
int OOP_marks_val = OOP_marks.toInt();
ui->label_OOP_marks->setText(QString::number(OOP_marks_val/100));
I'm new at qt, would like to write a new program with it that will be first. I want to change the image format and size after a image was loaded. However I can not find the ARGB1555 format in the supported formats. How can I convert its format to ARGB1555 ? I want this format, because will use it on hmi project that based on bare metal mcu, so will need the less memory.
It is possible to access the raw pixeldata of a QImage
const uchar * QImage::bits() const
Cast this to int ( 4bytes = pixel ) and use this function for conversion.
unsigned short ARGB8888toARGB1555(unsigned int c)
{
return (unsigned short)(((c>>16)&0x8000 | (c>>9)&0x7C00 | (c>>6)&0x03E0 | (c>>3)&0x1F));
}
Reference: https://cboard.cprogramming.com/c-programming/118698-color-conversion.html
I have 2 QDateEdit which are Date_dob and Date_doj.
I am storing the value using a Qstring shown below.
QString str_dob(ui->DATE_dob->text());
QString str_doj(ui->DATE_doj->text());
Now i want to populate the same into ui->Date_dob and ui->Date_doj (after some editing event takes place). I have used ,
ui->DATE_dob->setText(s.at(2));
ui->DATE_doj->setText(s.at(5)); //where s is a string having data
but the data doesn't populate.
Any kind of suggestion will be extremely appreciated.
Thanks in advance
For convert QString to QDate you can use QDate::fromString(). Then you can set date in QDateEdit with QDate::setDate(const QDate &date).
Hope it help.
You use wrong way the conversion.
QDate to QString
QString str_dob = ui->DATE_dob->toString("dd MM yyyy");
in the date format you should specify it else your conversation is default format. Known Format you can use
QString to QDate
if( ui->DATE_dob->setDate(QDate::fromString(str_dob,"dd MM yyyy").year()\
,QDate::fromString(str_dob,"dd MM yyyy").month()\
,QDate::fromString(str_dob,"dd MM yyyy").day()){
// Your Conversation Succes
}
when QString to QDate you have to know date format in string else your conversation fail or wrong value you get.
Example: if Qstring is : 19/12/2017 than your format is "dd/MM/yyyy"
I am really new to Qt and I have a little question for you. I am trying to work on ComboBox and when I add items to a combobox an integer like;
combobox->addItem(class.value); // class.value is an integer
It just adds a symbol to the combobox (*, / or ? )
How can I solve this little problem ?
Try combobox->addItem(QString::number(class.value));
Use QVariant . Advantage of using QVariant over QString::number() is you can convert data of any type to any other type.
int to string
QVariant(32).toString(); //assuming calss.value to be int
in your case it will be
combobox->addItem(QVariant(class.value).toString());
float to a string
QVariant(3.2).toString();
string to a float:
QVariant("5.2").toFloat();
it is that easy.
I'm having trouble with a seemingly very simple problem: I want to get a QDateTime from a QString containing a timestamp. I got the timestamp from PostgreSQL, but it doesn't matter. Here is the code that does not work:
QString timestamp = "2010-10-09 19:21:46+02:00";
QString format = "YYYY-MM-DD HH:MM:SSTZD";
QDateTime dt = QDateTime::fromString(timestamp, format);
qDebug() << dt.toString(); // outputs empty string
There must be something very obvious I'm missing. Thanks!
There were two mistakes I was making. There is no TZD in the format specifications, so I removed the time zone information since I do not need it in my app by doing:
timeStamp.chop(6);
And then used the following format to get a QDateTime. Note the lowercase format characters:
QDateTime createdAt = QDateTime::fromString(timeStamp, "yyyy-MM-dd HH:mm:ss");
Thanks to everyone above for helping out.