[SOLVED]Qmessagebox is causing windows explorer to open the application directory
-
I'm using a Qmessagebox to alert the user that a filename needs to be specified if they haven't already done so. The problem is that when you click ok on the message box, a windows explorer window opens to the application's current directory. When compiled and ran in OSX 10.7.5, this doesn't happen. I'm not sure if the problem is coming from the message box or some other part of the code. Any help or suggestions would be greatly appreciated. Thanks.
@QMessageBox msgBox;
msgBox.setText("Please specify output file using the Save As button");//open output file
QFile mFile(Filename);if(!mFile.open(QFile::WriteOnly | QFile::Text)) { msgBox.exec(); //if file output was not specified alert user with message box //qDebug()<<"Could not open file"; return; }
@
-
Hi,
You might better use the static way the QMessageBox works:
@
StandardButton warning(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
// so implement like this:
QMessageBox::warning(this, "You idiot", "Select a proper file!!");
@
And maybe also use the QFile.exists() function before you try to open the file.
Greetz -
I tried your suggestion, but I get a compilation error:
invalid use of 'this' in non-member function
-
oke, you can leave out the this pointer. It will not have a parent. Not a problem. The dialog is set to modal as default!
And I conclude that your code is placed outside a QObject class then? -
Hi,
Having the complete function could be useful
-
Correct, this code is coming from a function, I've written. How do I go about leaving out the pointer? Thanks in advance for the help, its been many years since I've done any object oriented programming and this is my first time using Qt.
-
NULL or 0, that should do the trick! Like SGalst says, seeing all the function we could help more!
Gr -
Complete Function:
@void FileWrite (QString Filename,QString R1,QString R2,QString R3,QString R4,QString R5,int pos,QString event,QString fs,QString elsize) //function used to write files
{
//QMessageBox msgBox; //create a message box object to alert the user that an output filename must be specified
// msgBox.setText("Please specify output file using the Save As button"); //set text of the message box//create a series of Qfile objects that read in the resource files QFile mFile_1(R1); if(!mFile_1.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"Could not open file for read"; return; } QTextStream in1(&mFile_1); QString rt1 = in1.readAll(); QFile mFile_2(R2); if(!mFile_2.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"Could not open file for read"; return; } QTextStream in2(&mFile_2); QString rt2 = in2.readAll(); QFile mFile_3(R3); if(!mFile_3.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"Could not open file for read"; return; } QTextStream in3(&mFile_3); QString rt3 = in3.readAll(); QFile mFile_4(R4); if(!mFile_4.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"Could not open file for read"; return; } QTextStream in4(&mFile_4); QString rt4 = in4.readAll(); QFile mFile_5(R5); if(!mFile_5.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"Could not open file for read"; return; } QTextStream in5(&mFile_5); QString rt5 = in5.readAll(); //open output file QFile mFile(Filename); if(!mFile.open(QFile::WriteOnly | QFile::Text)) { QMessageBox::warning(,"You idiot", "Select a proper file!!"); // msgBox.exec(); //if file output was not specified alert user with message box //qDebug()<<"Could not open file"; return; } QTextStream out(&mFile); //create a text stream output to the output file
// qDebug()<<fs;
out<<rt1<<pos<<rt2<<event<<rt3<<fs<<rt4<<elsize<<rt5; //concatenate user input with resource files to outputmFile.flush(); mFile.close();
}@
-
Using Null got it to work, however, my original problem persists, after clicking ok in the message box, windows explorer pops open the program directory
-
Yup, It might be your QFile use.
Do try this:
@
QFile File_f(FileName_str);
if (File_f.exists() == true)
{
// store your data
}
else
{
// Give warning, no such file
QMEssageBox::warning(0, "Idiot", "That is not a good spot, try another!!");
}
@ -
Ah, might found the problem:
@
File_f.open(QIODevice::WriteOnly | QIODevice::Text);
@ -
It's still not working. Thank you all for the help, I'm going to go a bit deeper with the debugger and read up on some more of the documentation to see if I can figure out whats going on.
-
On a side note, why allow your user to give an improper file name ? You can use QFileDialog::getSaveFileName() to unsure that you have a something that is correct in the first place
-
[quote author="SGaist" date="1386192093"]On a side note, why allow your user to give an improper file name ? You can use QFileDialog::getSaveFileName() to unsure that you have a something that is correct in the first place[/quote]
It's not that they're giving an improper file name, it's only if there hasn't been one specified at all.
I figured out what was causing the problem, it had nothing to do with the message box. After the filewrite function executed I used QDesktopsrvices to open a url (which is the file being written. In the case where a file wasn't written it was defaulting to opening windows explorer.
I solved the problem by adding conditional control to the desktopservices call, and making my filewrite function return a bool indicating whether or not a file was actually written.Once again thank you all for the help and guidance, it is greatly appreciated
-
You're welcome !
Then I would recommend to end the function early e.g. first check to make so you don't do all the processing in vain.
Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)