I can make a file and make my Qt project read and write to it .
But How can I make it create it's own text file for the first time then it writes and reads from and to it later for example i want it to write and read to and from this directory C:\Users\Administrator\Documents .
I will answer supposing that you are asking (don't know if I understood well, since your question is hardly understandable) how to create a file one time, then later, write and read from this file already created.
First, you have to give your file a name.
QString fileName = "C:\Users\Administrator\Documents\file.txt";
QFile file(fileName);
If your file is not already created : calling the function QFile::open(OpenMode mode) will create this file :
bool openOk = file.open(QIODevice::ReadWrite);
if (openOk)
{
// process
}
You can now write and read from your file.
Later, if you want to use your file again, just call this function again : if the file is already created, then you can read/write in this file.
Related
I am trying to export a GUI (Student Grade Manager App) I created in eclipse as a JAR file and run it on my desktop. Nothing happens when I double click it (though it does successfully export) at the moment. Here's an screenshot:
GUI Screenshot - Click here
I'm assuming the main issue here is that in my GUI, I am using the File Input stream in the CourseSectionApp.java file (Where the main method is located). It reads a .txt file called "StudentMarks.txt" and that's where it gets all its student data from.:
BufferedReader aFile = new BufferedReader ( new FileReader("Marks.txt"));
model = CourseSection.loadFrom(aFile);
Is there anyway to get this to work? Can I have the .txt file just export with the JAR file so it loads together? OR is there a way to put a sub-window, like modern applications have where the user can go to File->New-> and say, "load from file", and then be allowed to navigate to the .txt file on his computer?
My main goal is to be able to email this to friends and have them use it, but I googled around and only found a few people having similiar issues with not-so-clear answers. And if the JAR file cannot do this, what can? Will making it a .exe change anything?
Screenshot of Marks.txt file
I tried to make this question as legible as possible and if there is any other information I can provide you guys, please let know.
If you don't need to write to the file, including the file as resource in the jar would be your best option. This way you can always be sure the file is available via jar entry name. E.g.
try (InputStream is = getClass().getResourceAsStream("/data/Marks.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr)){
model = CourseSection.loadFrom(br);
}
If you also need to write to the file, you can use FileChooser to open a dialog for choosing a file:
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Marks File");
fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt"));
File file = fileChooser.showOpenDialog(null); // you could also pass a owner window here
if (file != null) {
// TODO: handle completed file selection by user
}
The issue described in the comments indicates that there is a problem with the classpath though. You need to make sure all required classes are available when the application is run...
I'm making an application part of which is reading from an XML which stores some preferences. However, whenever I build the project, all the sources get copied but the preferences file does not! I have added the following to the .pro file -
RESOURCES += rsc.qrc
And my rsc.qrc contains
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>data/preferences.xml</file>
<file>data/gamedata.xml</file>
</qresource>
</RCC>
Now whenever I try to open preferences.xml
QFile preferences(":/data/preferences.xml");
if(!preferences.exists()){
preferences.open(QFile::WriteOnly);
preferences.write("abc");
qDebug() << "Written";
}
else {
qDebug() << "File exists";
}
Absolutely nothing gets printed and even the rest of the application stops working.
You don't use the resource part correctly in your example.
It will most likely not work because you try to write to a resource that is embedded into your executable after you have build your application. Reading is fine, but writing can't work by definition.
If you want a editable setting files, you have to distribute them along with your executable, or use a different method for reading/writing your settings like QSettings.
However using QSettings also means, that you will need to configure all your default settings in your loading function in case the values do not exist if you use the default configuration. Meaning you use registry on windows.
You have the option to force the use of a INI file format in the constructor of QSettings, this can make sense if you want to provide a default settings INI file instead of your xml files.
In case you want to store more complex data a xml file might be needed anyway, so if you want to stick with that you will need a way to copy your setting files to your build path. This can be done within your pro file with QMAKE_POST_LINK.
Example:
COPY_CMD = "$$PWD\\data\\preferences.xml $$OUT_PWD\\release\\data\\preferences.xml"
COPY_CMD = $${QMAKE_COPY} $$replace( COPY_CMD, "/", "\\" ) $$escape_expand( \\n\\t )
QMAKE_POST_LINK += $$COPY_CMD
I wrote a notepad in qt gui, but when I associate a file with it and click on it, the .exe is run and file is not opened, so I have to open it using
QFileDialog::getSaveFileName(
this,"Save As","",
"Text Document (*.txt)\n All Files (*.*)")
When we click on text.txt it directly opens the file in notepad. How can I make same for my app?
remark: QDir::currentpath() returns the path of file.txtq (which is associated with it) on which we clicked but I was not able to return its name.
When double-clicking the file (associated with your exe), its path is passed to your program via command line arguments. You can access them in the following way:
if (QApplication::arguments().size() > 1) {
const QString filename = QApplication::arguments().at(1);
// "filename" now contains path and name of the file to open.
}
Also, I have no idea why you are using QFileDialog::getSaveFileName(). In order to call the open file dialog, you'll need the QDialog::getOpenFileName() method.
So I have a general question. When you convert your .ui file into a .py file with pyuic, on the top part of the .py file it says:
WARNING! All changes made in this file will be lost!
Is is serious ? How will changes made in the file lost ?
I'm sorry I realise this is not a smart question but I just want to make sure what this is about and that it will not comprise anything afterwards!
Since this is a generated file, it is a result, not a source. Thus:
When you distribute your project, you shouldn't be distributing that file. So, whatever you do to the file, won't reach your customers anyway.
Whenever your project is built and the source .ui file is changed, the output will be regenerated - and whatever changes you made to the output file, thus lost.
What this means is: You’ve used a .uic file to generate a .py file, therefore if you modify the later manually ( i.e : create a class , or adding a comment etc...) and subsequently re-generate a file via puic those will be lost.
That’s why you should create a main.py file and import the generated .py file into it leaving the original untouched.
If at a later stage you need to add a button and a label for example, you do so via qdesigner which gets you your new .uic file you create a .py
with the puic and voila your gui is modified your old .py erased by the new one but everything in your main.py stays the same.
Hope this makes sense..
Provided you have generated .py file from the .ui file and will not be making any changes in the .ui file, changes will persist.
I have the following issue: I create a QFileSystemWatcher and it runs and works nicely on Linux, but no way on Windows 7. Can you spot anything in the code that might make it not to work?
Thx.
Here is the code to initialize it:
mConfigChangeWatcher = new QFileSystemWatcher();
mConfigChangeWatcher->addPath(config_file_name);
QObject::connect(mConfigChangeWatcher,
SIGNAL(fileChanged(QString)),
this,
SLOT(configFileChanged(QString)));
and this is supposed to be the slot getting the work done:
void MyClass::configFileChanged(const QString &file)
{
qDebug() << "Changed: " << file ;
}
When you check if the file is added to the watcher using QFileSystemWatcher::files() method after the first modification in the file do you get the correct list?
I was with the issue that some applications, when modifing a file, delete the old file from the system and write it again.
Note that QFileSystemWatcher stops monitoring files once they have been renamed or removed from disk, and directories once they have been removed from disk.
I was using QFileSystemWatcher to watch an image file edited by Photoshop. Somehow the file gets removed from the list of files being watched.
I had the same problem and solved it very fast.
Within the slot that manages the fileChanged signal I noted the path disappears from files(). I simply make a check and re-add it if necessary
if (! watcher_.files().contains(path))
{
watcher_.addPath(path);
}
I hope this helps
Fabio