How to use QTextStream with PythonQT? - qt

I am writing a plugin for a Qt desktop app using PythonQT.
I wonder how to use << operator in python.
QTextStream stream(&file);
stream << doc.toString();
Any hints?
How may I ask Python to list all methods for a given class like QTextStream?
Or is there another way in Python to write a QDomDocument to a QFile?

Found a solution already...
doc = QDomDocument()
root = doc.createElement("Animation")
doc.appendChild(root)
stream = QTextStream(xmlfile)
doc.save(stream, 0)

Related

How to work around string splitting while loading a list from a file in QT

I'm trying to create a simple "To Do list" app in QT Creator while coding the part that loads and saves the list from a file I get stuck on a problem.
If you enter a string like "Do my homework" the program threads the string as it should, but when you load the program again the save file got split in words. So it gets all the entries but each word separated ("Do", "my", "homework").
What is the solution? I tried working with 'char arrays' and 'getline' but they give me nothing but errors.
Here is my code for the save and load parts:
void MainWindow::LoadList(){
std::ifstream load_file("./data.bin");
char loader[255];
while (load_file >> loader){
QString Writer = QString::fromStdString(loader);
ui->lstTaskList->addItem(Writer);
}
}
void MainWindow::SaveList(){
std::ofstream save_file("./data.bin");
for (auto i = 0; i < ui->lstTaskList->count(); i++){
QString Saver = ui->lstTaskList->item(i)->text();
std::string saver = Saver.toStdString();
save_file << saver << std::endl;
}
}
Can anyone help me with this, please?
My thanks in advance...
The anwser was using QFile and QByteArray for me, I knew about QFile but it tries using basic "std" c++ till I learned more about QT.

pleora sdk convert PvBuffer or PvRawData to QByteArray

I'm using the pleora sdk to capture images from an external camera and I am able to successfully write the data to tiff image files on disk. My next step is to change the data storage to SQLite instead of disk files.
I have PvBuffer *lBuffer pointer working fine. Now I need to convert that data to a format I can use to write to SQLite. I'm using Qt on linux so the QByteArray would be very convenient.
This is kind of a specific question for the pleora sdk and Qt. I'm hoping someone has experience with this.
PvRawData *rawData = lBuffer->GetRawData();
QByteArray ba;
//Need to copy the data from rawData to ba.
Thank you in advance.
I found an answer and wanted to post in case anybody else has something similar. I uses the reintepret_cast method.
data = lBuffer->GetDataPointer()
imgSize = lBuffer->GetPayloadSize();
const char *d = reinterpret_cast<char *>(data);
QByteArray ba(d, imgSize);
QSqlQuery q = QSqlQuery( db );
q.prepare("INSERT INTO imgData (image) values (:imageData)");
q.bindValue(":imageData", ba);
if ( !q.exec() )
qDebug() << "Error inserting image into table: " << q.lastError() << endl;
else
qDebug() << "Query executed properly" << endl;

What's the best way to copy two binary files in QT

I want to copy one binary file to another binary file.
The only constraint I have is that copying must occur through the QFile (because I've overloaded some internal methods and I need them to run).
I wrote a naive way to solve but is a slow writer:
QFile * write_to = new QFile("myfile.bin");
if(write_to->open(QFile::WriteOnly))
{
QFile read_from("my_outher_bin.bin");
if(read_from.open(QIODevice::ReadOnly))
{
QDataStream write_data(write_to);
QDataStream read_data(&read_from);
while(write_to->size() < read_from.size())
write_data << read_data;
}
}
What is the most effective way to do this?
An easy and safe way to do it, use the STD libraries, std::ifstream is a good option:
std::ifstream src("file.txt", std::ios::binary);
std::ofstream dst("to_file.txt", std::ios::binary);
dst << src.rdbuf();
If you are using C++17, you can use the <filesystem> extension and use the fs::copy_file function.
fs::copy_file("file.txt", "to_file.txt");

Qt ActiveX "Unknown error"

I'm working on integrating T-Cube motor controller (http://www.thorlabs.de/newgrouppage9.cfm?objectgroup_id=2419) into the software based on Qt-4.8.1 package. Due to there is no manual or any sort of tutorial how to retrieve ActiveX object and how to call methods I did the following.
1) Looked through Windows registry looking for words similar to motor controller name. Found a candidate with CLSID "{3CE35BF3-1E13-4D2C-8C0B-DEF6314420B3}".
2) Tried initializing it in the following way (code provided is shortened, all result checks are removed in order to improve readability):
HRESULT h_result = CoInitializeEx(NULL, COINIT_MULTITHREADED);
pd->moto = new QAxObject();
initialized = moto->setControl( "{3CE35BF3-1E13-4D2C-8C0B-DEF6314420B3}" );
QString stri = browser->generateDocumentation();
obj->dynamicCall("SetHWSerialNum(int)", params);
QVariantList params;
params << 0;
params << 0.0;
int result = pd->moto->dynamicCall("GetPosition(int, double&)", params).toInt();
value = params[1].toFloat();
QVariantList params;
params << 0;
params << dist;
params << dist;
params << true;
int result = pd->moto->dynamicCall("MoveRelativeEx(int, double, double, bool)", params).toInt();
3) generateDocumentation() method gives perfect description of ~150 methods.
4) All dynamicCall() invocations cause "Error calling ...: Unknown error", where "..." is a first argument of dynamicCall() from the list generateDocumentation()'s given me.
5) If I insert into dynamicCall() any method which isn't presented in the documentation generated the output is different. So I suppose that methods in documentation generated really exist.
6) If I use #import directive and try calling directly avoiding QAxObject usage I see "mg17motor.tlh" file but none of interfaces described there contain any methods. So I can't use it directly as well. Is it normal?
I would be very much obliged for any advice.
You can find the ActiveX object using the OLE viewer. Then search for something like
APT.. or MG.. under all objects. Then find the parameter ProgID=MGMOTOR.MGMotorCtrl.1.
Now in Qt don't use QAxObject but QAxWidget. Then you get something like:
QAxWidget* aptMotor;
QVariant chanID = QVariant(0);
aptMotor = new QAxWidget();
aptMotor->setControl("MGMOTOR.MGMotorCtrl.1");
//Nice html documentation on available functions
QFile file("out.html");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out << aptMotor->generateDocumentation();
file.close();
aptMotor->setProperty("HWSerialNum",QVariant(83853493));
aptMotor->dynamicCall("StartCtrl");
aptMotor->dynamicCall("EnableHWChannel(QVariant)",chanID);
QThread::sleep(1); // Give it time to enable the channel
double pos(0);
aptMotor->dynamicCall("SetAbsMovePos(QVariant,QVariant)",chanID,QVariant(pos));
aptMotor->dynamicCall("MoveAbsolute(QVariant,QVariant,QVariant)",chanID,0);
aptMotor->dynamicCall("StopCtrl");

How to get magic number of a binary file

There is a magic number associated with each binary file , does anyone know how to retrieve this information from the file?
file <file_name>
magic numbers are usually stored in (linux):
/usr/share/file/magic
also check this link, someone was trying to use libmagic to get the information in C program, might be useful if you're writing something yourself.
Use libmagic from the file package to try and sniff out the type of file if that's your goal.
There are no general "magic" numbers in binary files on unix, though different formats might define their own. The above library knows about many of those and also use various other heuristics to try and figure out the format/type of file.
The unix file command uses magic number. see the file man page for more.(and where to find the magic file )
Read this: http://linux.die.net/man/5/magic
It's complex, and depends on the specific file type you're looking for.
There is a file command which in turn uses a magic library, the magic library reads from a file found in /etc called magic (this is installation dependant and may vary), which details what are the first few bytes of the file and tells the file what kind of a file it is, be it, jpg, binary, text, shell script. There is an old version of libmagic found on sourceforge. Incidentally, there is a related answer to this here.
Hope this helps,
Best regards,
Tom.
Expounding on #nos's answer:
Example below uses the default magic database to query the file passed on the command line. (Essentially an implementation of the file command. See man libmagic for more details/functions.
#include <iostream>
#include <magic.h>
#include <cassert>
int main(int argc, char **argv) {
if (argc == 1) {
std::cerr << "Usage " << argv[0] << " [filename]" << std::endl;
return -1;
}
const char * fname = argv[1];
magic_t cookie = magic_open(0);
assert (cookie !=nullptr);
int rc = magic_load(cookie, nullptr);
assert(rc == 0);
auto f= magic_file(cookie, fname);
if (f ==nullptr) {
std::cerr << magic_error(cookie) << std::endl;
} else {
std::cout << fname << ' ' << f << std::endl;
}
}

Resources