How to print more than one QStrings on QtextEdit - qt

Lets say we have a variable called X and we do some operations on it. now for printing it on the QtextEdit I want to print it like this cout on console:
cout << "The value of X is " << X << endl;
But the setText function only prints out a QString not both "the value of ... " and X.

You can use a QTextStream to write data into a QString similar to cout:
int X = 42;
QString str;
QTextStream out(&str);
out << "The value of X is " << X << endl;
qDebug() << str;
Output:
"The value of X is 42
"

I would solve this in the following way:
QString text = QString("This is my value: %1").arg(x); // x can be either number or string
textEdit->setText(text);

If your "x" is an integer, for example, you can convert that number into a string and concatenate that with the introducing string like that:
QString myText = "This is my value: " + QString::number(x);
If x=5 this will give you this string:
This is my value: 5
You can now assign myText to your QTextEdit with settext.

Related

command : Delete[] x

I have the following simple code. I allocate dynamically memory for 3 doubles, I assign to each double a number and after I deallocate the memory but as one can see if runs the code the only difference before and after the deletion (delete[] x) and the only difference is for the first double of the vector. I can't understand why the content of the first element of the vector changed and the content of x remained the same with the same address of memory.
#include <iostream>
#include <cmath>
int main(int argc, char * argv[])
{
double * x;
x = new double [3];
x[0] = 1; x[1]=3; x[2]=5;
std::cout << x[0] << " " << x[1] << " " << x[2] << "\n";
std::cout << x << "\n";
delete[] x;
std::cout << x[0] << " " << x[1] << " " << x[2] << "\n";
std::cout << x << "\n";
return 0;
}
To my understanding, this is undefined behaviour; x is read after it is deleted.

c++11 share_ptr as value a map key

I just insert pValue to map next i get it from map ,and its value should be 20 ,but not, why?
typedef shared_ptr<BaseObject> PtrValue;
class CodeExecuteContext{
public:
map<string,PtrValue> varIdentPool;
};
class BaseInteger :public BaseObject{
public:
int value;
BaseInteger(int val):value(val){
enumObjType = INT;
};
};
...
PtrValue pValue = rightNode->executeCode(context);
context.varIdentPool.insert(make_pair(leftNode.idName,pValue));
BaseInteger * baseInteger = (BaseInteger *) pValue.get();
cout << "==AssignmentASTFork== insert [ " << leftNode.idName << " , " << baseInteger->value << " ]" <<endl;
map<string,PtrValue>::iterator it;
for (it = context.varIdentPool.begin() ; it!=context.varIdentPool.end();it++) {
BaseInteger * baseInteger = (BaseInteger *) it->second.get();
cout << "[key : " << it->first << ", value : " << baseInteger->value << endl;
}
result:
==AssignmentASTFork== insert [ arg , 20 ]
[key : arg, value : 32742]
The map::insert function returns "pair", where the boolean from the pair indicates whether the element has been inserted or not. If the boolean is true, it has been inserted. If the key already existed, the boolean will be false and the key-value pair has not been inserted.
Try "auto insertResult = context.varIdentPool.insert(make_pair(leftNode.idName,pValue));"
If insertResult.second == false, the key already existed.
The iterator (insertResult.first) will always point to the item from the map.
With that iterator you can alter the value from the map by "insertResult.first->second = newValue"

trouble with output of recursive function

i'm having trouble getting the correct output of the function. The function output should show the expression. For example, if the input is "1234", then the output should be 1 + 2 + 3 + 4 = 10.
i can get the function to output the first part of the expression, but i'm not sure how to get it to output the sum as well.
heres what i have so far:
void sumDigits(int num, int &sum){
sum += num % 10;
if(num < 10)
cout << num;
else {
sumDigits(num/10, sum);
cout << " + " << num % 10;
}
}
why don't you do it in caller?
you could pass the remaining digits number to process to a function and output sum when it's 0, but I doing it in the caller is better..
<< can be chained. Try
cout << " + " << num % 10 << " = " << sum << endl;

Qt Delete Items from QMultiHash while Iterating

I want to delete items out of my QMultiHash. Looking at the docs, I believe I am doing it correctly but it always crashes after the first delete. What am I doing wrong?
Here is my code:
for (QMultiHash<int, Frame*>::iterator i = m_FrameBuffer.begin(); i != m_FrameBuffer.end(); ++i) {
if ( (frameNumber - i.key()) >= ( 5 ) ) { // Delete frames 5 frames old or more
qDebug() << "DELETE ==> Key:" << i.key() << "Value:" << i.value() << " Difference: " << (frameNumber - i.key());
int removed = m_FrameBuffer.remove(i.key());
qDebug() << "Removed this many: " << removed;
}
}
Here is the output:
FRAME COUNT: 1
FRAME COUNT: 2
FRAME COUNT: 3
FRAME COUNT: 4
FRAME COUNT: 5
DELETE ==> Key: 2 Value: Frame(0x138a400) Difference: 5
Removed this many: 1
The program has unexpectedly finished.
Your iterator becomes invalid after you remove items from a container while iterating it. Try this:
QList<int> keys = m_FrameBuffer.keys();
foreach (int key, keys)
{
int diff = frameNumber - key;
if (diff >= 5)
{
qDebug() << "DELETE ==> Key:" << key
<< "Value:" << m_FrameBuffer.value(key)
<< "Difference: " << diff;
int removed = m_FrameBuffer.remove(key);
qDebug() << "Removed this many: " << removed;
}
}
Also you can use QMutableHashIterator for it:
QMutableHashIterator<int, Frame*> it(m_FrameBuffer);
while (it.hasNext())
{
it.next();
int key = it.key();
int diff = frameNumber - it.key();
if (diff >= 5)
{
qDebug() << "Items to be removed:"
<< m_FrameBuffer.values(it.key()).size();
it.remove();
}
}

QString::toDouble() giving me double with wrong precision

I have a QString myNumber containing "09338.712001". When I do:
myNumber.toDouble();, it returns 9338.71, but I want the double to be the original value, which is 09338.712001. Does anyone know how to get the double returned by toDouble to have the same precision as the QString? Thanks.
Your problem probably is in how you output these values.
QString s("9338.712001");
bool ok = false;
double a = 9338.712001;
double b = s.toDouble(&ok);
double c = 1/3.0;
qDebug() << "a: " << a;
qdebug() << "b: " << b;
qDebug() << "a: " << QString("%1").arg(a, 0, 'g', 13)
qDebug() << "b: " << QString("%1").arg(b, 0, 'e', 13);
qDebug() << "c: " << QString("%1").arg(c, 0, 'g', 30);
result:
a: 9338.71
b: 9338.71
a: "9338.712001"
b: "9.3387120010000e+03"
c: "0.333333333333333314829616256247"
But anyways, maybe now it's a good moment to read this: What Every Computer Scientist Should Know About Floating-Point Arithmetic

Resources