Jingoism
28th July 2007, 09:12
Ok first off I am a total noob at C++ and QT as I just started a few days ago, but I have been trudging through some online tutorials and just recently hit a snag. I am quite sure it's a simple issue, but here goes.
Got a list widget, line edit widget and a button that when pressed outputs the text to the list widget and then outputs to a file. Now I got it working great as long as I hard coded the filename into my program, but when I wanted to possibly create a new file I could not figure out where to put it.
You can see below where I wanted to put it, but I knew it would not work since it was encapsulated by the if statement. I think my brain is fried from looking at all this for too long hehe.
void DialogImpl::doSomething() {
QString linedt = lineEdit->text(); // Set linedt to the text in the Line Edit box
if (!file.isOpen()) && ( linedt != "" ) { // If file is not open then open it
QFile file("/home/jingoism/" + linedt); // This is wrong, and the part I dont understand
QTextStream out(&file); // Also wrong, should go with QFile
file.open(QIODevice::WriteOnly | QIODevice::Text); // Open the file as Writeonly
listWidget->addItem("Opened File!"); // Verify in listWidget file is opened
listWidget->addItem(linedt); // Add the line of text to the listWidget
out << linedt << endl; // Add the line to the file
lineEdit->clear(); // Clear the LineEdit box
} else {
if ( linedt != "" ) { // If text in the Line Edit box is not null then proceed
out << linedt << endl;
listWidget->addItem(linedt);
lineEdit->clear();
}
}
}
Got a list widget, line edit widget and a button that when pressed outputs the text to the list widget and then outputs to a file. Now I got it working great as long as I hard coded the filename into my program, but when I wanted to possibly create a new file I could not figure out where to put it.
You can see below where I wanted to put it, but I knew it would not work since it was encapsulated by the if statement. I think my brain is fried from looking at all this for too long hehe.
void DialogImpl::doSomething() {
QString linedt = lineEdit->text(); // Set linedt to the text in the Line Edit box
if (!file.isOpen()) && ( linedt != "" ) { // If file is not open then open it
QFile file("/home/jingoism/" + linedt); // This is wrong, and the part I dont understand
QTextStream out(&file); // Also wrong, should go with QFile
file.open(QIODevice::WriteOnly | QIODevice::Text); // Open the file as Writeonly
listWidget->addItem("Opened File!"); // Verify in listWidget file is opened
listWidget->addItem(linedt); // Add the line of text to the listWidget
out << linedt << endl; // Add the line to the file
lineEdit->clear(); // Clear the LineEdit box
} else {
if ( linedt != "" ) { // If text in the Line Edit box is not null then proceed
out << linedt << endl;
listWidget->addItem(linedt);
lineEdit->clear();
}
}
}