How to I make QMessageBox delete the content of a vector - c++

I'm very new to code so I apologise if this is a simple question but I am struggling.
I have a GUI program that saves user input into a vector, displays it then can be saved as a txt file. Once it has been saved, I want a QMessageBox to appear asking if the user wishes to delete the now saved vector data. The vector is named v_History.
QMessageBox msgBox;
msgBox.setText("History saved to file.");
msgBox.setInformativeText("Would you like to delete the current history?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
msgBox.setDefaultButton(QMessageBox::Discard);
msgBox.exec();
Any help or advice is greatly appreciated.
Thank you

When using setStandardButtons(), exec() returns a value indicating which standard button was clicked, eg:
QMessageBox msgBox;
msgBox.setText("History saved to file.");
msgBox.setInformativeText("Would you like to delete the current history?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
msgBox.setDefaultButton(QMessageBox::Discard);
int ret = msgBox.exec();
if (ret == QMessageBox::Discard)
{
// delete the history as needed ...
}

Related

File Not Being Created in QT C++ using QFileDialog

there I'm trying to make a simple notepad application. I've got a new file menu item. When the user clicks on that, it checks whether the text field is empty or not. If that is not empty then it prompts to save the work. If the user opts for yes then it asks for the location using QFileDialog. However, this code doesn't create the file at the provided destination. Can anyone figure out this code?
if(this->checkTextField()==false){ //checkks whether the textField is empty or not
QMessageBox::StandardButton reply = QMessageBox::question(this,this->appName,this->document_modified);
if(reply == QMessageBox::Yes){
QString filename = QFileDialog::getSaveFileName(this,"Save the File",QDir::homePath(),this->textFilter);
QFile file(filename);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.flush();
file.close();
}
}
Simply creating a QFile will neither create nor open a file on the filesystem. You have to open it to write something to it/read from it.

Print a PDF file with Qt

I know that there are similar questions about this issues but not a single one seems to address my problem.
I have two options In my widget: one that allows the user to save the display as PDF file and one that allows the user to print the display directly.
My problem is that there seems to be quite some minor differences if I print the saved PDF file with Windows vs when I print the interface with Qt. A possible solution that I begin to follow is that when I print the interface first to save the interface in PDF as a temporary file and after to try to print with Qt that file.
Unfortunately it seems that I can not use directly the PDF file without convert it in a pixmap or something similar.
This is how I manage to export in PDF format:
QTextDocument doc;
if (!ComputeDoc(doc))
return false;
QPrinter printer;
printer.setOutputFileName(filePath);
printer.setPaperSize(format);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOrientation (orientation);
printer.setFullPage(true);
if(ok && !filePath.isEmpty())
{
QPainter painter(&printer);
painter.setBackgroundMode(Qt::TransparentMode);
painter.setRenderHint(
(QPainter::RenderHint)(int)(
QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::HighQualityAntialiasing |
QPainter::SmoothPixmapTransform),
true);
painter.setPen(Qt::white);
doc.drawContents(&painter);
}
And this how I originally try to print the file:
QTextDocument doc;
if (!ComputeDoc(doc))
return false;
QPrinter printer;
printer.setPaperSize(format);
printer.setOrientation (orientation);
printer.setFullPage(true);
QPrintDialog printDialog(&printer, this);
if(ok && printDialog.exec() == QDialog::Accepted)
{
QPainter painter(&printer);
painter.setBackgroundMode(Qt::TransparentMode);
painter.setRenderHint(
(QPainter::RenderHint)(int)(
QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::HighQualityAntialiasing |
QPainter::SmoothPixmapTransform),
true);
painter.setPen(Qt::white);
doc.drawContents(&painter);
}
As you can see from the print the inner frame is not centered and in the left part (the image is flipped to right) of the paper there is a line that in PDF is not. Sorry for this example but I can not post the entire page. I hope this will give a small clarification.
Does anyone know a solution to print a PDF file from the disk with qt? Or maybe how to make Windows to print to file from qt?

Qt: Linking my model data with a QListView

I'm writing my first Qt project (so I'm new to the environment) and I've got this project build using the MVC design pattern.
It's a pretty basic note manager/editor. I've got a class uiman ("Ui Manager") which takes care of my UI, and my function to open a notes database (which is just a list of text files to open)
void uiman::openDBDialog(){
QFileDialog dialog;
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setDirectory("/Users/myuserdir/Desktop");
dialog.setFilter(QDir::Files);
dialog.setWindowTitle("Open File");
dialog.setNameFilter("Textie Notes DB(*.db)");
dialog.exec();
QString pathDB = dialog.selectedFiles().first();
model = new notesModel();
model->setNotesDB(new QString(pathDB.toUtf8().constData()));
refreshAll();
}
so far, so good. I take the path of my database and give it to my model, which should now manage the rest.
Now, the refreshAll() function should take the list I opened up and show them in my QListView, but I can't parse the file and append items on the go using clear() and append() unlike the QListWidget. So, how do I approach building a vector (I suppose) of names from my file and feeding them to my QListView?
Sorry if I'm not being clear, but the official documentation hasn't been clear enough.
Edit: This is my model, nodesmodel, and here's the code.
notesModel::notesModel(QObject *parent) :
QFileSystemModel(parent)
{
QStringList noteList;
}
void notesModel::setNotesDB(QString *dbpath){
// open the notes database
databasepath = dbpath;
}
QFile* notesModel::getDB(){
if(this->dbFile == NULL)
this->dbFile = new QFile(databasepath- >toUtf8().constData());
return this->dbFile;
}
As you already noticed, you do not add/remove data from the viewer widget itself, this is done in the model.
You can set the model with the setModel() method.
If you want to show a simple QString list, then you can use:
QStringList strings;
strings << "String 1" << "String 2" << "String 3";
model = new QStringListModel(strings, parent);
view->setModel(model);
// model is managed by parent, and will be deleted when parent is deleted.
// If you create multiple models you might consider other memory management strategy
To read a text file and store its lines in a QStringList, see this answer.

Automatically saving a file with QFileDialog

I have to automate a test using QTest, Qt, C++:
I write text in a tab (part of tabwidget) and then try to close it, afterwards a QFileDialog appears ( because I made changes to the plaintext in the tab), I try to "catch" the QFileDialog like this:
QWidgetList topWidgets = QApplication::topLevelWidgets();
foreach (QWidget *w, topWidgets) {
if (QFileDialog *fd = qobject_cast<QFileDialog *>(w)) {
fd->setFileMode(QFileDialog::ExistingFiles);
fd->selectFile("/tmp/test.txt");
}
}
After getting the QFileDialog object I want my changes from the tab to be saved in the file "test.txt" which I created before in the tmp directory. When I execute this nothing happens, the QFileDialog pops up, but test.txt is not selected and not saved, how can I achieve this?
The selectFile method does not work if the filedialog is visible and if the focus is set to the line edit widget. From the qfiledialog.cpp (QT 5.2):
if (!isVisible() || !d->lineEdit()->hasFocus())
d->lineEdit()->setText(file);
For our automated tests, we just hide the filedialog for a moment, call selectFile() and show it again
Try this:
QWidgetList topWidgets = QApplication::topLevelWidgets();
foreach (QWidget *w, topWidgets) {
if (QFileDialog *fd = qobject_cast<QFileDialog *>(w)) {
fd->hide();
fd->selectFile("/tmp/test.txt");
fd->show();
fd->exec();
}
}

how to clear textbox text in C++/XAML

im new to C++/XAML, using VS2012, working on my first Windows 8 app.
I have created a textbox1 that take a number, another textbox2 that display the results, another button that once it is clicked, it does the calculation. everything works, my question is when user want to do the calculation again, he will need to click on textbox1, press the backspace to erased the last entered number, how can i make it when textbox1 is clicked and tapped, it will auto clear the previously entered text? or how do i use/make a "CLEAR" button to handle the text clearing for textbox1 and textbox2? Thank You!
With button:
<Button Content="Clear" Name="button1" Click="button1_Click" />
Code behind:
void YourClass::button1_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ args)
{
textBox1->Text = "";
textBox2->Text = "";
}
If you want to use some logic when you tap or something else you should take a look at Tapped event. I'd do it another way: Whenever it got focus(GotFocus event) then select all the text in text box(textBox1->SelectAll(); in GotEvent handler).
You can use
SetWindowText is a function to set the contents of a Edit Control
m_myEditCtrl.SetWindowText(_T("")); // if using MFC
SetWindowText(hWndMyEditCtrl,_T("")); // When using plain Win32 API
myEditCtrl.Text = ""; // When using C++CLI
You can use it from the "On Click" handler also if needed!
I think that could be useful, if you just add in the end of the program that apply to your textboxes a new button with:
textBox2->Text = String::Empty;
textBox1->Text = String::Empty;
OR
…you could add these code lines before start the procedure of the program, just in the beginning of the program. Each time it would start over with blank.
Cheers.