QFtp won't work - qt

I feel like I'm probably missing something very easy here, but I'm at a loss to figure out what. I have a C++ function (with Qt 4.7) where I need to access the files on an FTP server. To do this, I have the following set up:
QString source = "ftp://username:password#ftp.myftpserver.com/directoryname/";
QFtp *ftp = new QFtp(this);
ftp->connectToHost(source);
connect(ftp, SIGNAL(listInfo(QUrlInfo)), this, SLOT(processInfoFromFile(QUrlInfo)));
connect(ftp, SIGNAL(done(bool)), this, SLOT(finishThisProcess()));
ftp->list();
When I type the source directly into a browser it comes up correctly and shows me all the files inside the directory. I also have another QFtp instance (different variable names) elsewhere in the program set up the same way; that works. However, with this one it simply interprets the directory at source as being empty and immediately jumps to finishThisProcess. Is there something I'm missing? Thanks!
EDIT: this is the other instance of the ftp client:
ftp2 = new QFtp(this);
QString user = "username";
QString pass = "password";
connect(ftp2, SIGNAL(listInfo(QUrlInfo)), this, SLOT(processInfoFromFile(QUrlInfo)));
connect(ftp2, SIGNAL(done(bool)), this, SLOT(finishThisProcess()));
ftp2->connectToHost("ftp.myftpserver.com");
ftp2->login(user, pass);
ftp2->list();
It's the same as the other except a)this one tries to access one directory level farther up, and b)I declared the username and password separately and then login manually. I tried the one giving me problems this way, but to no avail.

1) You should connect the signals and slots before the relevant statements.
2) Also, you should use the login method with the username and password.
So, your code should look like this:
QString source = "ftp://ftp.myftpserver.com/directoryname/";
QFtp *ftp = new QFtp(this);
connect(ftp, SIGNAL(listInfo(QUrlInfo)), this, SLOT(processInfoFromFile(QUrlInfo)));
connect(ftp, SIGNAL(done(bool)), this, SLOT(finishThisProcess()));
ftp->connectToHost(source);
ftp->login(username, password);
ftp->list();

Related

QSoundEffect does not reload source

In my program it is possible to select a sound for an action. the sound is changeable, which means the .wav file gets replaced by another file.
This may cause the problem. When i replace the file and set the source of the QSoundEffect the sound does not change.
At the moment i am having a source like this:
//variable in .h
QUrl sound = "file:///"+soundDirectory+"sound.wav";
QUrl newSound = "file:///"+soundDirectory+"newSound.wav"; ;
QSoundEffect soundeffect;
//called in setSound() in .cpp
soundEffect.setSource(sound);
the sound loads without problem and i can play that sound.
i can change that sound with this code
// changing the sound in changeSound()
soundEffect.setSource(newSound);
this also works fine. the new sound is loaded and i can play it.
But it is also possible to change the sound files in the directory:
//changeSoundFile()
QFile::remove(sound.toLocalFile());
QFile::copy(anyPossibleSound.toLocalFile(), sound.toLocalFile());
This also works and replaces the sound file in its directory with another.
If I call setSound() after changing the file. It seems like the file does not get reload. and the sound is not changed. This is also the problem if changed the sound in between (call setSound on startup, then changeSound, then changeSoundFile and the setSound again)
Am I overlooking something?
It's not mentioned in the official docs, but you can find the implementation on GitHub:
void QSoundEffect::setSource(const QUrl &url) {
if (d->source() == url)
return;
d->setSource(url);
emit sourceChanged();
}
The file does not get reloaded because the URL is the same. The implementation holds an internal cache with the data that has been loaded before, so when you play the file, nothing change.
The API does not provide a way of forcing the reset of the data source. You have two alternatives:
Re-creating the QSoundEffect instance each time you modify the file.
Changing the file name:
// Create a temporal file
const auto uuid = QUuid::createUuid();
const auto new_filename = uuid.toString() + ".wav";
// Copy the file
QFile::remove(sound.toLocalFile());
QFile::copy(anyPossibleSound.toLocalFile(), new_filename);
sound = QUrl(new_filename);
// Use it
soundEffect.setSource(sound);

I want my modPath variables to be from one directory

I want my modPath variables to be from one directory, is the following correct? Using C#. Thank you ^^
private readonly string modPath = #"C:\Users\SusanPeter\Mods";
if (string.IsNullOrEmpty(modPath))
{
MessageBox.Show("Sorry wrong directory");
return;
}
I am a beginning, learning to Write a program to check whether this particular Path (C:\Users\SusanPeter\Mods) is valid, and as long as not empty.
If this program is not placed in (C:\Users\SusanPeter\Mods), it will prompt for a message. I am sort of ensuring the path is correct before starting the program.

Qt5: download a file without saving to HDD

I'm trying to download a file in Qt5, but the file must not be located on the HDD after download.
To clarify> My app will use a downloaded file to update some firmware, and I don't want the downloaded update to remain on the user's hard drive because it could get stolen.
So, I'm trying to make a QFile from QNetworkReply* but without saving it to some path on a hard drive.
I'm downloading a file using QNetworkAccessManager and storing the data into QNetworkReply. I always used to make a QFile with QNetworkReply*, but now I can't do that.
I have found the QTemporaryFile class where a file gets removed right after using it, but that still leaves user with some options of finding the file later.
I tried typecasting that QNetworkReply* as a QFile, but didn't manage to get that to work, seems like QFile can't be without a path on HDD.
Does anyone have any ideas how to do this, and how?
Thanks everyone.
Again not sure your intended end use case but since your data is small enough to hold in memory you can use a QByteArray or QBuffer and write into it from your QNetworkReply. QBuffer provides a QIODevice interface for the QByteArray so it may be a bit easier for you to work with.
Make sure to open the QBuffer for read/write. See the simple example below from the Qt documentation, http://doc.qt.io/qt-5/qbuffer.html#details, below:
QBuffer buffer;
char ch;
buffer.open(QBuffer::ReadWrite);
buffer.write("Qt rocks!");
buffer.seek(0);
buffer.getChar(&ch); // ch == 'Q'
buffer.getChar(&ch); // ch == 't'
buffer.getChar(&ch); // ch == ' '
buffer.getChar(&ch); // ch == 'r'
That should allow you to read back the data and use as required without creating a file on the system.

Start a process using QProcess

I'm trying to start Microsoft word using QProcess as following:
QString program = "WINWORD.EXE";
process->start(program);
but nothing happens.
winword.exe is on path (so when i type winword.exe word is openning up).
Is it the right way to do so ?
may be code below will help you:
QProcess *process = new QProcess(this);
QString program = "explorer.exe";
QString folder = "C:\\";
process->start(program, QStringList() << folder);
I think you are trying to execute program that doesn't consists in global $PATH windows variable, that's why winword.exe doesn't executes.
Also you may need to define absolute path to program, e.g.:
QString wordPath = "C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE"
process->start(wordPath, QStringList() << "");
For me, I need to add " characteres :
m_process->start("\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\"");
From Qt documentation:
Note: Processes are started
asynchronously, which means the
started() and error() signals may be
delayed. Call waitForStarted() to make
sure the process has started (or has
failed to start) and those signals
have been emitted.
Connect the signals mentioned in doc to some GUI control or debug output and see what happens. If there is an error, you should check the error type using QProcess::error().
If the method, where you're trying to launch external process, is finished right after your code, e.g.:
void foo() {
...
QString program = "WINWORD.EXE";
process->start(program);
}
and variable
process
was declared as local variable, it will be destroyed at the end of method and no external process will be started - or correctly you won't see it because it will be destroyed right after start.
It was the reason for similar issue in my case. Hope it helps.
You can just set the working directory:
myProcess = new QProcess();
myProcess->setWorkingDirectory("C:\\Z-Programming_Source\\Java-workspace\\Encrypt1\\bin\\");
Or do it at start:
myProcess->start("dir \"My Documents\"");
At start() you can enter a command for the console... read the manual.
I prefer the first option. More readable.
QProcess *pro = new QProcess;
QString s = "\"C:\Users\xyz\Desktop\Example.exe";
pro ->start(s);

Error: is not a valid virtual path

I used something like
Dim i As String
i = Server.MapPath("~/photos/") + fileName
Only once in a project that was working and online, on the offline version, when I run it on my machine, its working, no errors, I uploaded it, it gave me an error like:
'~/photos/http://www.MyURL.com/photos/4411568359267Pic003.jpg' is not a valid virtual path.
Indicating a line in my code:
var marker = new GMarker(new GLatLng(<%=coordinates%>));
This have never happened before, and I don't know where to start troubleshooting as this script -Google Maps- doesn't even need images, i tried to comment it out, it gave me the same error but on a different script this time, the one that show formatting toolbar for the text areas
Line 8: new nicEditor({buttonList : ['fontSize','fontFamily','fontFormat','bold','italic','underline','strikethrough','forecolor','bgcolor','removeformat'], iconsPath : '../nicEdit/nicEditorIcons.gif'}).panelInstance('<%= txtDescription.ClientID %>');
..please HELP :'(
can you post your fileupload aspx page and your function so we can troubleshoot it.
"'~/photos/http://www.MyURL.com/photos/4411568359267Pic003.jpg'"
look closely at the url, it is tacking the full url on to "i". what type of control or you using, a generic or a server side control
solved, turns out the error is due to old database records when i was storing the whole path in the fileName...Thanks Joshua and Keltex

Resources