Print multiple QTextDocuments with QPrinter - c++

I need to generate a document to print for a number of objects which the user creates dynamically, and I want to print these documents. I wrote following code (generateDocument() takes a reference to the document to add html code):
QPrinter printer;
QPrintDialog popup(&printer);
if (popup.exec() == QDialog::Accepted)
{
for (int i = 0; i < _quiz->getSerieCount(); i++)
{
QTextDocument doc;
generateDocument(doc, _quiz->getSerie(i));
doc.print(&printer);
}
}
When printing to pdf the behaviour is different in linux and windows: On linux this just prints the last generated document, and on windows it prompts to select a new pdf for every generateDocument() call.
Am i supposed to do this differently?

You can add a page break for each serie and then print the document.
Try with the following e.g.
QTextDocument doc;
QTextCursor cursor(&doc);
for (int i = 0; i < _quiz->getSerieCount(); i++)
{
if(i!=0) \\ dont add page break for the first document
{
QTextBlockFormat blockFormat;
blockFormat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysAfter);
cursor.insertBlock(blockFormat);
}
// < append _quiz->getSerie(i) contents in the document >
}
doc.print(&printer);
Haven't tested the code, but should work on Windows without any problems I suppose, because I was using it similarly without any issues. Can't comment anything for its behavior on Linux machines. You can modify it better to suit your need.
Hope this Helps.

Related

boost::property_tree, My program crashes when I write something to the configuration file. C++

sorry my bad english launguage
Hi guys, i looking a help because i have problem with library in c++ boost.org
I have configuration file: Example
Checkbox Number:[0]=0
Checkbox Number:[1]=0
Checkbox Number:[2]=2
Checkbox Number:[3]=2
And when I write something for this file: Exapmle
Checkbox Number:[0]=0
Checkbox Number:[1]=0
Checkbox Number:[2]=2
Checkbox Number:[3]=2
NewChecbox Number:[4]=2
And that causes my program to crash.
My code to read cfg file:
SDK::F->CreateDir("CFG");//Create new directory
boost::property_tree::ptree py;
QCheckBox* checkbox[]//Qt checkboxes
= { SDK::gui->checkBox,
SDK::gui->checkBox_2,
SDK::gui->checkBox_3,
SDK::gui->checkBox_4
};
for (int i = 0; i < ARRAYSIZE(checkbox); i++)
{
static char checckbox[1000];
sprintf(checckbox, "Checkbox Number:[%d]",i);
py.put(checckbox, checkbox[i]->checkState());
}
boost::property_tree::write_ini("CFG/ConfigLauncher.cfg", py);
What do I need to change in order for the program not to crash

How to format cells in a QTableWidget

my problem is not so simple for me, because I'm a niewbie in qt, so asking to the experts: the problem, I populate dynamically a QTableWidget from a button on a toolbar, selecting data from a *.csv file.
All works fine but the formatting of the last column text rests on center-vertical position.
I had tryed to change styles or set directly on the code result: the app crash or nothing appens to the last column text.
(And if is possible how to change headers appearance).
Thank You in advance (& sorry for English mistakes).
void ToolBar::setDrawTable(QStringList sl)
{
m_table->clear();
QStringList slHeader = sl.at(0).split(';');
sl.removeAt(0);
m_table->setRowCount(sl.count());
m_table->setColumnCount(slHeader.count());
m_table->setHorizontalHeaderLabels(slHeader);
for (int r= 0; r<sl.count(); r++){
QStringList slRow = sl.at(r).split(';');
for (int c = 0; c < slRow.count(); c++)
{
QTableWidgetItem *item = new QTableWidgetItem(slRow.at(c));
m_table->setItem(r, c, item);
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
}
slRow.clear();
}
m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

How to use QProgressDialog along with QDomDocument save function

To make a long story short I have a program that uses the QDomDocument class to create an xml file and then uses the save() function to save it to a text stream object. So basically its
QDomDocument somedoc;
//create the xml file, elements, etc.
QFile io(fileName);
QTextStream out(&io);
doc.save(out,4);
io.close();
I want to be able to show the progress of the save using the QProgressDialog class, but I'm having a hard time figuring it out. Is there a way I can incrementally check to see if the file is through processing and just update the progress? Any suggestions? Thanks.
Firstly, I thought that we can find answer in Qt source code, but it was not so simple, so I found easier solution, just use toString() method and write it as usual file. For example:
QStringList all = doc.toString(4).split('\n');//4 is intent
int numFiles = all.size();
QProgressDialog *progress = new QProgressDialog("Copying files...", "Abort Copy", 0, numFiles, this);
progress->setWindowModality(Qt::WindowModal);
QFile file("path");
file.open(QIODevice::WriteOnly);
progress->show();
QTextStream stream(&file);
for (int i = 0; i < numFiles; i++) {
progress->setValue(i);
if (progress->wasCanceled())
break;
stream << all.at(i) << '\n';
QThread::sleep(1);//remove these lines in release, it is just example to show the process
QCoreApplication::processEvents();
}
progress->setValue(numFiles);
file.close();
If you want to look at the source code of QDomDocument::save(), you can find it in
qt-everywhere-opensource-src-5.4.1.zip\qt-everywhere-opensource-src-5.4.1\qtbase\src\xml\dom
or just on the GitHub.

QFileDialog history

i'm trying to set the history on the QFileDialog, but it doesn't seem to appear anywhere.
QFileDialog dialog(parent, caption, path, filter);
dialog.setHistory(history);
dialog.exec();
But i don't see the history in the dialog anywhere. Where should it be? Should it be anywhere? What am I doing wrong here?
edit:
I made this little hack to make it work even with filenames
for(int index = 0; index < files.size(); index++)
{
QFileinfo info(files[index]);
files[index] = info.path();
}
If you open the path selection combo box, you should see them under Recent Places.
Example: The following code
QStringList history;
history << "C:\\temp" << "C:\\Development" << "C:\\Development\\temp";
QFileDialog dialog;
dialog.setHistory( history );
dialog.exec();
leads to this result on my computer (Windows XP 32 bit):

How to get enumeration type value from MS Word 2007?

I need to get current page in my document, with set range. I found that it is possible to do by:
Range.Information(wdActiveEndPageNumber) //example in C#
but i have problem with that. In documentation, information is visible as property. So when i use
QString number = myRange->property("Information(wdActiveEndPageNumber)").toString()
i'm getting nothing. I also tried dynamicCall, but either doesn't work. Simple properties as Text or Start works perfectly fine, but i have no idea what to do with these enumerations.
Whole code:
QAxObject *word, *doc;
word = new QAxObject("Word.Application", this);
word->setProperty("DisplayAlerts", false);
word->setProperty("Visible", true);
doc = word->querySubObject("Documents");
doc->dynamicCall("Open(const QString&)", "path to file");
QAxObject *act = word->querySubObject("ActiveDocument");
QAxObject *next = act->querySubObject("Content");
next->dynamicCall("Select()");
next->dynamicCall("Copy()");
QClipboard *clip = QApplication::clipboard();
myTextEdit->setText(clip->text());
QString number = next->property("Information(3)").toString();
QMessageBox::information(this, tr("cos"), tr("%1").arg(number)); //here i need to know how many pages i've got
Okay, after much research, I found that there is no possibility yet to take value from Information enum. Maybe in future version of Qt, but nowadays I had to create library in Visual Basic and invoke functions from C++ code.
Just found a really cool answer to this question here:
http://www.qtforum.org/article/31970/how-do-i-get-use-the-ienumerable-interface-in-qt.html
Here the answer again.
With returnList containing your enum..
QAxObject *enum1 = returnList->querySubObject("_NewEnum");
IEnumVARIANT* enumInterface; //to get this, include <windows.h>
enum1->queryInterface(IID_IEnumVARIANT, (void**)&enumInterface);
enumInterface->Reset(); //start at the beginning of the list.
for (int i=0;i<returnList->dynamicCall("Count").toInt();i++)
{
VARIANT *theItem;
enumInterface->Next(1,theItem,NULL);
QAxObject *item = new QAxObject((IUnknown *)theItem->punkVal);
qDebug() << item->dynamicCall("Caption");
}