How to debug QDomElement in QtXml? - c++

I've got a QDomElement, and I would like to debug it, i.e. see it as plain text in debug console. In order to output it with qDebug(), it needs to be in QString format, however I don't see any conversion method from a QDomElement nor a QDomNode.
Any idea? Thanks!

There is no built-in operator for streaming DOM elements to QDebug. You could write one easily enough, something like:
QDebug operator<<(QDebug dbg, const QDomNode& node)
{
QString s;
QTextStream str(&s, QIODevice::WriteOnly);
node.save(str, 2);
dbg << qPrintable(s);
return dbg;
}

Use QTextStream:
QTextStream lTS(stdout);
lTS << lMyDomElement;

if you #include <QDebug> QDebug would act as TextStream itself.
i.e. qDebug()<< lMyDomElement; would be enough)

Well I also come across similar situations, in that case my best bet is to make use of the QDomDocument which this QDomElement is part of. So I would say you cannot get a direct way to access the QDomElement but you can achieve that using the QDomDocument.
For this you need to ensure that your QDomDocument gets updated with the recent QDomElement and then use QDomDocument::toString() which would return you the whole document as a QString.
Here is the Qt reference.
Hope this helps.

Related

Set encoding for process output issue

I want to set encoding for a Windows console process for Russian ouput. In C# the Process has a StandardOutputEncoding attribute, but in Qt no such functionality exists.
Here is the problem:
Any suggestion how to accomplish it?
Update:
I have tried QTextStream setCodec function:
void Test1::getData(QByteArray data)
{
QTextStream encodeStream(data);
encodeStream.setCodec("windows-1251");
dataTextBrowser->append(encodeStream.readAll());
emit dataFinished();
}
Result:
Thanks to Michael O. I have fixed the issue. Also, I included the code here, so others can find the solution.
Code:
void Test1::getData(QByteArray data)
{
QTextStream encodeStream(data);
encodeStream.setCodec("IBM 866");
dataTextBrowser->append(encodeStream.readAll());
emit dataFinished();
}

QSettings simple task not working

I'm trying to use QSettings on my code but it's not working correctly. My code is:
In Mainwindow.h
QSettings settings
(I have declared before all setOrganizationName setOrganizationDomain
setApplicationName)
In Mainwindow.cpp
settings.setValue("smtp/email", "test");
qDebug() << settings.value("smtp/email").toString();
But the qDebug is returning me ""
I believe it's a simple use of QSettings but it's not working.
For efficiency, the changes may not be saved to permanent storage immediately. (You can always call sync() to commit your changes.)

Is it necessary to make QDomElement created by the same QDomDocument to write XML?

I use QDomDocument to write XML document.
But in my dom tree, some nodes are created using docA, some using docB.
QDomElement parentNode = docA.CreateElement("name");//created by docA
QDomElement childNode = docB.CreateElement("value");//created by docB
parentNode.appendChild(childNode);//in onr tree
And:
QTextStream out(&file);
docA.save(out, Indent);//docA created the root QDomElement
//write the file using docA
So is it possible to write the whole tree to XML like this?
You should avoid this because things will start going wrong when if docA goes out of scope if docB is still being used. I believe what you're proposing will technically work until that occurs, but the library seems designed to discourage it.
However, there is a function QDomDocument::importNode() which is probably what you want. You can do something like this:
docAParent.appendChild( docA.importNode( docBNode, true ) );
The boolean argument controls whether or not a deep copy is made.
See the documentation: http://qt-project.org/doc/qt-4.8/qdomdocument.html#importNode

Why does this work and how? Non-static Qt object

I'm a little confused and would like to clear this up.
//QDir()::rmdir is from Qt Creator auto complete.
//It does not work.
//Says no such static function.I looked it up, turns out to be true.
//Fair enough...though I'm not sure why auto-complete suggested it.
bool success = QDir()::rmdir("Y:/dir1/dir2/dir3"); //Does not work.
//Now I could make a QDir object as such.
//I didn;t test this but I'm sure it would work fine.
//However it seems clumsy.
QDir d("Y:/"); //This seems like a waste.
d.rmdir("Y:/dir1/dir2/dir3");
//Lastly, the source of my confusion. QDir().rmdir
//This works, but WHY?
//There is no empty constructor for QDir in Qt Documentation.
//http://doc.qt.nokia.com/4.7/qdir.html
//Yet this empty constructor version works. Why?
bool success = QDir().rmdir("Y:/dir1/dir2/dir3");
My main concern is why does the last examaple [QDir().rmdir] work?
I've noticed this on a number of Qt classes. Is this an anonymous object and if so
what does this mean with regards to object clean up? Is this form safe to use?
One of the QDir constructors is:
QDir ( const QString & path = QString() )
Your QDir().xxx code is calling this constructor, which then uses default to using a QString() as one argument.
This is safe and normal to do.
QDir creates a temporary object. Pretty the same if you callsth. like:
QString s("123");
int answer = 40 + s.left(2).right(1).toInt();
Second line produces 2 temporary objects.

QImage file path

I don't see this in the documentation anywhere, so it probably doesn't exist, but just in case:
I have a function that accepts as a parameter a vector of QImages. Each QImage was loaded from disk at some point and has not been edited--it is just read from. Ideally what I'd like to do is loop over all the QImages and output a list of their file paths to an XML file.
Unfortunately, I'm not seeing in the documentation any way to get at the original file path that the image was loaded from. So my question is, is it possible, given only a QImage, to figure out what file path the QImage was originally loaded from or not?
Something along the lines of:
QString QImage::getOriginalFilepath();
I know this is probably a futile question, but its always worth a shot to ask, I suppose.
(I'm using Qt 4.7, by the way.)
I looked through the code, and that information doesn't appear to be saved anywhere. QImage::load (and the constructor) uses QImageReader(fileName,format).read() and then copies the result to itself. The QImageReader is set to delete the device representation (the opened file object) as soon as it's finished reading.
So, in summary, it appears that this is not possible.
Also for me it seems that QImage doesn't provide an interface to get its path. I also think that this is not a missing feature, because there's no need to tie a QImage to a specific path. In the majority of all use cases the QImage has no corresponding physical file.
So the best way in your situation would be to subclass QImage and add this feature:
class MyImage : public QImage {
QString path_;
public:
MyImage(const QString& path);
QString path(); // getter
};
I omit the implementation details.
I usually create a struct for hold the QImage and the image path.
struct Image
{
QImage imageData;
QString filename;
};