QDate toString(fromString) -> setText(Label) - c++

I am new to Qt UI designer. We got a task to read a txt file, get a string out of it, and print it to a label via QDate.
So what I need, is to read a string, format it to QDate, and then format it to QString.
I tried doing it without an instance of QDate, but it did not work. The working code with an instance looks like:
QDate QDateInstance;
for (int i = 0; i < (int)TravelAgency1.FlightsList.size()-1; i++){
if (SearchedBooking == TravelAgency1.FlightsList[i]->getId()){
ui->ID_Output->setText(QString::number(TravelAgency1.FlightsList[i]->getId()));
QDateInstance = QDateInstance.fromString(QString::fromStdString(TravelAgency1.FlightsList[i]->getFromDate()),"yyyyMMdd");
ui->StartD_Output->setText(QDateInstance.toString());
QDateInstance = QDateInstance.fromString(QString::fromStdString(TravelAgency1.FlightsList[i]->getToDate()), "yyyyMMdd");
ui->EndD_Output->setText(QDateInstance.toString());
But I do not really like the declaration of an instance QDateInstance. I am guessing, there is a more elegant way of doing it.
Initially I was trying:
ui->StartD_Output->setText(QDate::toString(QDate::fromStdString(TravelAgency1.FlightsList[i]->getToDate()), "yyyyMMdd");
Any ideas?

Related

I want to replace an element in a QString - QT - C++

I have following problem:
I saved in a QString an XMLStart.
QString f = "<class=\"go\">\n"
"<number>2</number>\n"
"<column>3</column>\n"
"<row>4</row>";
I want to change for example the value of row with 7 in this String by for example using replace or something like that.
f.replace();
How can i achive that ? Is that possible?
Regards
I'm not familiar with XML in Qt and I'm sure that there is a more elegant way that hopefully someone will post after me, but this is an easy way to do your replacement.
Of course you'll have to provide the string values in a better way...
QString xml = "<class=\"go\">\n"
"<number>2</number>\n"
"<column>3</column>\n"
"<row>4</row>";
QString tag = "row";
QString val = "7";
QByteArray exp("<%1>%2</%1>");
QString rex = QString(exp).arg(tag, ".*");
QString rep = QString(exp).arg(tag, val);
xml.replace(QRegExp(rex), rep);

Double to QString and save in QJsonDocument

I need to save values ​​below pre-set levels, using QJsonDocument.
I have the following example of code:
(...)
gameLevels= {3.67, 7.43, 9.76};
while(gameLevels[i] <= x)
{
for(...)
{
//do something and calculate auxPoints.
}
QString sGL = QString::number(gameLevels[i]);
QString below = "below";
QString points = "pts";
instantPowerPoints.insert(below + sGL+ points , auxPoints);
i++;
(...)
}
emit saveData(QJsonDocument(instantPowerPoints));'
It should save something like:
"below3.67pts":2
"below7.43pts":6
"below9.76pts":10
But instead is saving:
"below3":Object
"67pts":2
"below7":Object
"43pts":6
"below9":Object
"76pts":10
I get the problem is how I save the array of doubles gameLevels. But I really need to have the number with the dot save as a string. Is there another way to save the string like this without automatically creating the object?
I am using C++ in QTCreator.
This should to the work
instantPowerPoints.insert(QString("%1 %2 %3").arg(below).arg(sGL).arg(points), auxPoints);

qt c++ gui 'on_lineEdit'

I'm new to qt and to c++ and have run into something that I can't solve by searching the internet. First, here is the code in question:
void MainWindow::on_lineEdit_3_textChanged(const QString &arg1) //check for hex or octal
{
QString text = ui -> lineEdit_3 -> text();
QString y = text.mid(0, 1);
int n = text.size();
{
if (y == "0")
{
if (n > 1)
{
text = "Dec. only";
ui -> lineEdit_3 -> setText("");
}
}
}
ui -> lineEdit -> setText(text);
}
This works fine, but when I come back to the editor after running, I have a message at the bottom of the screen:
Unused parameter 'arg1' [-Wunused parameter]
I am sure it is referring to the &arg1 in the first line, however I have no idea what arg1 would be used for or how to use it. Can anyone direct me to information about its use, or tutorial on its use?
From the docs: "This property holds the line edit's text." arg1 must be the current text. For more info, read here.
You're getting that exact message because in your function definition you have const QString &arg1, however you've not used it within your function itself as instead you're retrieving the text with ui->lineEdit_3->text(). The arg1 parameter is the text contained in the ui->lineEdit_3 after it has been changed, so you don't need to retrieve it with the call to ->text().
Side tip, you can squash the warning message with Q_UNUSED(someParameterName), but it flags a warning message for a reason.
This line is not necessary, since you already have the text in arg1.
QString text = ui -> lineEdit_3 -> text();
Then you can use the following to get the first character from the textfield.
QChar y = arg1.at(0);

Print multiple QTextDocuments with QPrinter

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.

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");
}