how to set/read QSqlRelationalTableModel column properties and constraints? - qt

Is there any way to check properties and constraints for each column in a QSqlRelationalTableModel? For example, I'd like to ask my QSqlRelationalTableModel object whether a certain column can contain nulls, or what datatype is valid for that column.

You'll need to get a QSqlField value for each column of the model, which is given by
QSqlRecord record = model->database().record(model->tableName());
QSqlField field = record.field(columnIndex);
then you'll be able to check if a field can be null with QSqlField::requiredStatus() (if the driver supports querying that property) and to get its data type with QSqlField::type().

From alexisdm's answer above, I wrote this simple code snippet to output the properties of each field in a table. Posting it here to save typing for anyone else who is interested.
I also discovered a gotcha: if you use table_model::record() or table_model::record(int) you get unexpected (to me) results for some properties, e.g., isAutoValue seems to always return false, even for fields designated as autoincrement fields in the database. However, you do get a real value for typeID() (though I haven't been able to determine what typeID() is), whereas typeID() always returned -1 for me using model->database().record(model->tableName()).
QSqlRecord record = table_model->database().record(table_model->tableName());
// the following get isAutoValue() wrong; but have a real typeID()
//QSqlRecord record = table_model->record();
//QSqlRecord record = table_model->record(table_model->rowCount() - 1);
qDebug() << "********** table" << table_model->tableName() << "*********";
for (int i = 0; i < table_model->columnCount(); ++i) {
QSqlField field = record.field(i);
qDebug() << "---------- field" << i << field.name() << "--------";
qDebug() << "default value" << field.defaultValue();
qDebug() << "is auto value" << field.isAutoValue();
qDebug() << "is generated" << field.isGenerated();
qDebug() << "is null" << field.isNull();
qDebug() << "is read only" << field.isReadOnly();
qDebug() << "is valid" << field.isValid();
qDebug() << "length" << field.length();
qDebug() << "precision" << field.precision();
qDebug() << "required status" << field.requiredStatus();
qDebug() << "type" << field.type();
qDebug() << "type id" << field.typeID();
qDebug() << "value" << field.value();
}

Related

Converting ISO::DateTime to number format

I want to change the date time format to numbers only so that it can be incremented.
Current Implementation is saved as QString 2019-03-13T09:01:22+01:0
Expected result: 201903120858031
qt is a really powerful framework, you are just coding a solution in a wrong way.
Imagine the pain in the back just validating dates, leap years etc etc
every date and dateTime has the methods to do calendar math operations.
see this example as ref:
//
QDateTime dateOrigin = QDateTime::currentDateTime();
QDateTime ReturnDate = QDateTime::currentDateTime();
QString isoDate{dateOrigin.toString(Qt::ISODate)};
qDebug() << "before: " << isoDate;
ReturnDate = dateOrigin.addDays(1);
ReturnDate = dateOrigin.addMonths(1);
ReturnDate = dateOrigin.addYears(1);
qDebug() << "after: " << ReturnDate.toString(Qt::ISODate);
//
update:
lets suppose that 201903120858031 is a date time formated as yyyyMMddHHmmss, then you can convert that to a long
//201903120858031
//yyyyMMddHHmmss
qDebug() << "after: " << ReturnDate.toString("yyyyMMddHHmmss");
qDebug() << "after as long number: " << ReturnDate.toString("yyyyMMddHHmmss").toLong();
producing the output:
before: "2019-02-28T12:43:33"
after: "20200228124333"
after as number: 20200228124333

QDebug's operator<< chained output,including include expression,output unexpected result

When I execute following code:
bool ok = false;
QByteArray ba("fffff3");
qDebug() << ok << ba.toInt(&ok, 16) << ok;
The output is true 16777203 true,I vaguely know why.Someone can explain what is going on.What is ok's change process?

How to get camera intrinsics and extrinsics in openni2?

I have a primesense carmine 1.08 and carmine 1.09. I need the intrinsic parameters for the RGB and the IR camera and the extrinsics between the two. I use pcl with openni2 support. So I need to know the sensor parameters used by openni2/pcl.
Is there a way in openni2 to find the intrinsics and the extrinsics using openni2/pcl? Libfreenect2 has option to get IR and color camera intrinsics, but are these parameters same as that in openni? Are all these parameters extracated from sensor during runtime?
I tried to get it via pcl, but i get nan for the focal length and the principal points
int main (int argc, char** argv)
{
std::string device_id ("");
pcl::io::OpenNI2Grabber::Mode depth_mode =
pcl::io::OpenNI2Grabber::OpenNI_Default_Mode;
pcl::io::OpenNI2Grabber::Mode image_mode =
pcl::io::OpenNI2Grabber::OpenNI_Default_Mode;
pcl::io::OpenNI2Grabber grabber (device_id, depth_mode, image_mode);
grabber.start();
double fx,fy,px,py;
grabber.getDepthCameraIntrinsics(fx,fy,px,py);
cout << "fx=" << fx << endl;
cout << "fy=" << fy << endl;
cout << "px=" << px << endl;
cout << "py=" << px << endl;
return (0);
}
A similar question has been asked here https://stackoverflow.com/questions/41110791/openni-intrinsic-and-extrinsic-calibration. However it hasnt recieved any answers.

How to set umlaut for header in QTableWidget?

I have this command to set label for all headers of a table:
ui.mytable->setHorizontalHeaderLabels(QStringList() << tr("VERTRAG") << tr("DATUM/UHRZEIT") << tr("PRÜFER"));
But this Ü does not appear. I tried to use
ui.mytable->setHorizontalHeaderLabels(QStringList() << tr("VERTRAG") << tr("DATUM/UHRZEIT") << tr("PRÜFER").replace(QString::fromLatin1("Ü"), "Ü"));
and
ui.mytable->setHorizontalHeaderLabels(QStringList() << tr("VERTRAG") << tr("DATUM/UHRZEIT") << tr("PRÜFER").replace(QString::fromUtf8("Ü"), "Ü"));
but it does not work also. How can I solve this?
ok, thank you guys. I have a simple answer. It works with me.
ui.mytable->setHorizontalHeaderLabels(QStringList() << tr("VERTRAG") << tr("DATUM/UHRZEIT") << QLatin1String("PRÜFER"));

QList memory deallocation

I'm trying to free memory after using QList, but it doesn't seem to work properly.
Here's my code:
QList<double> * myList;
myList = new QList<double>;
double myNumber;
cout << "CP1" << endl;
getchar(); // checkpoint 1
for (int i=0; i<1000000; i++)
{
myNumber = i;
myList->append(myNumber);
cout << myList->size() << endl;
}
cout << "CP2!" << endl;
getchar(); // checkpoint 2
for (int i=999999; i>0; i--)
{
myList->removeLast();
cout << myList->size() << endl;
}
cout << "CP3!" << endl;
getchar(); // checkpoint 3
delete myList;
cout << "CP4!" << endl;
getchar(); // checkpoint 4
Memory usage:
CP1: 460k
CP2:19996k
CP3:19996k
CP4:16088k
So it looks like despite removing of elements and deleting myList still large part of memory is being used. I believe there is a way to handle it but I can't find it.
Thanks in advance for any help.
Pawel
Memory manager is not required to release the memory your program has allocated. There are no problems in your deallocation.
QList is an array based list. The array expands automatically, but does not shrink automatically. Removing elements from the list does not affect the size of the array.
To trim the array down to the actual size, create a new QList and add the contents to it. Then delete the original list.
Unfortunately looks like there is no convenience method to do this, like the List.TrimExcess() in .NET.

Resources