So i have a QFile and QTextStream member as part of my class... trying to init. them together in my constructor:
Class.h:
QFile _file;
QTextStream _textstrm;
Class.cpp:
_file = QFile (/*file name*/);
_file.open(/*set stuff*/);
_textstrm = QTextTream ( &_file );
And the comp error i get, C2248, says the objects to have access to the operators in their own class..
The problem is that you are creating a new object and you are adding an attribute that has no access, you must use the functions provided by the object.
_file.setFileName(/*file name*/);
_file.open(/*set stuff*/);
_textstrm.setDevice( &_file );
Related
I have implemented my own xml writer to generate xml as QString.
i have create a class "MyXmlWriter" with private member variable as QXmlStreamWriter and try to initialize it in the public method writeToString()
in declaration header file:
class MyXmlWriter {
public:
MyXmlWriter();
~MyXmlWriter();
QString writeToString();
private:
QXmlStreamWriter writer;
void writePart();
}
in cpp file:
void MyXmlWriter::writePart() {
// i want to use the QXmlStreamWriter instance hier
}
QString MyXmlWriter::writeToString(){
QString result;
writer(&result); // at this became the error: no match for call to '(QXmlStreamWriter) (QString*)'
xmlWriter.setAutoFormatting(true);
// private method called
writePart();
return result;
}
this error appear on build:
error: no match for call to (QXmlStreamWriter) (QString) writer(&result); *
If the QXmlStreamWriter write is declared in the local method writeToString() then i cant access this writer in private method writePart()
i want to use the member variable "writer" in other methods that's why a local declaration isn't an option for me.
You have to assign a new object to your variable writer, like this:
QString MyXmlWriter::writeToString() {
QString result;
writer = QXmlStreamWriter(&result); // will not compile
return result;
}
This code is dangerous. result is destroyed at the end of writeToString(), so writer then contains invalid data and you cannot use it anymore.
Additionally, QXmlStreamWriter cannot be copied, so this would probably not compile at all. It's better to just pass the QXmlStreamWriter to your methods, like this:
class MyXmlWriter {
public:
MyXmlWriter();
~MyXmlWriter();
QString writeToString();
private:
void writePart(QXmlStreamWriter &writer);
}
and
QString MyXmlWriter::writeToString() {
QString result;
QXmlStreamWriter writer(&result);
writePart(writer);
writePart(writer);
return result;
}
I wanted to extend a class with an additional property and then assign a base class object to this new class. Here is my sample code:
class QExtFileInfoList: public QFileInfoList
{
public:
bool newProperty;
}
QDir var1 = "/abc/def";
QFileInfoList var2 = var1.entryInfoList(); // works
QExtFileInfoList var3 = var1.entryInfoList(); // does not work
the code returns:
conversion from 'QFileInfoList {aka QList<FileInfo>}'
to non-scalar type 'QElfFileInfoList' requested
How can I fix this issue?
you have defined a new class. But who is creating them?
QDir will not create classes for you that you have created.
There are 3 solutions in mind:
write a converter function QFileInfoList to QElfFileInfoList
write a constructor for QElfFileInfoList taking just a QFileInfoList reference ( this would convert automatically if you would ask for conversion of a single instance)
derive a new class from QDir that is creating your own class for you.
I would choose them in these order.
I check the QT-Help, I found that class QFile has the following constructors:
QFile()
QFile(const QString &name)
QFile(QObject *parent)
QFile(const QString &name, QObject *parent)
There is no QFile(char *) for class QFile
But QFile("/home/mythicsr/test"); is ok, why?
Consider the statement:
QFile("/home/mythicsr/test");
It leads to the creation of a temporary object of type QString under the hood and thus the constructor QFile(const QString &name) is invoked.
As mentioned by #meetaig in the comments it's possible because:
QString supports initialization using char*
Or better, as noted in the comments by #Slava - const char * in this case.
That's all.
I'm pretty new to Qt and I have a problem with finding out how to save/load data from your application.
I'm creating a Calendar app and have to save different classes like: Deathlines, appointments, birthdays, etc.
And I've found this tutorial http://qt-project.org/doc/qt-4.8/tutorials-addressbook-part6.html but it only describes how to save one type of class.
So I was wondering if you could help me, because I have no idea how to save/load multiple classes that way, I don't need some detailed description of it (however it would be appreciated of course) but only a gentle push into the right direction.
Because nowhere in this tutorial is explained how to save multiple classes :(
EDIT: This program is for PC (project for school)
You can define your custom class and implement stream operators for it :
class CustomType
{
public:
CustomType()
{
paramter1=0;
paramter2=0;
paramter3="";
}
~CustomType(){}
int paramter1;
double parameter2;
QString parameter3;
};
inline QDataStream& operator<<( QDataStream &out, const CustomType& t )
{
out<<t.paramter1;
out<<t.paramter2;
out<<t.paramter3;
return out;
}
inline QDataStream& operator>>( QDataStream &in, CustomType& t)
{
in>>t.paramter1;
in>>t.paramter2;
in>>t.paramter3;
return in;
}
You should register the stream operators of your class somewhere in your code when starting your application before streaming the class. This could be done in the constructor of your main window :
qRegisterMetaTypeStreamOperators<CustomType>("CustomType");
Now you can save or load objects of your class to or from file.
Saving some objects of your custom classes to a file :
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_8);
out << object1;
out << object2;
loading objects of your custom classes from a file :
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_8);
in >> object1;
in >> object2;
Note that the order of reading and writing to file should be the same.
I am going crazy here and I promise that I have looked at the documentation.
I declare a QTextStream as a member variable in my header file, but in my constructor, I want to set it to go to stdout in my constructor.....but I can't get the syntax right to let me do it!
All I want to do is this:
QTextStream m_text_out;
m_text_out = QTextStream(stdout)........
Any help would be great!
Use an initializer list.
class MyClass {
public:
MyClass();
private:
QTextStream m_text_out;
}
MyClass::MyClass() : m_text_out(stdout) {
}