I can't get this to work. Anyone knows how to make it work?
void MainWindow::on_pushButton_clicked()
{
int sum1 = ui->lineEdit->text().toInt();
int sum2 = ui->lineEdit_2->text().toInt();
ui->label_4->setText(sum1 + sum2);
}
Error:
C:\Qt\Tools\QtCreator\bin\Mellemrubrik\mainwindow.cpp:26: error: C2664: 'QLabel::setText' : cannot convert parameter 1 from 'int' to 'const QString &'
Reason: cannot convert from 'int' to 'const QString'
No constructor could take the source type, or constructor overload resolution was ambiguous
In general, you can convert multiple numeric types to QStrings like so:
int val1, val2;
QString result = QString("val1=%1 val2=%2 sum=%3").arg(val1).arg(val2).arg(val1+val2);
But for numbers, this is also possible:
int val1, val2;
QString result = QString::number(val1+val2);
You can see Qt's documentation for more info!
You can try this
int v1,v2;
v1=ui->lineEdit->text().toInt():
v2=ui->lineEdit_2->text().toInt()
QString result = QString::number(v1+v2);
ui->label->setText(result);
Would setNum as shown below do?
int v1,v2;
v1=ui->lineEdit->text().toInt():
v2=ui->lineEdit_2->text().toInt()
ui->label->setNum(v1+v2);
Related
I have this code:
QString carda = "000123";
QString queryStringAnet("SELECT * FROM [records] WHERE ([user]='" + carda.toInt() + "' AND [apl]='"+apl+"' AND [tasktype]='"+taskType+"' AND [taskkind]='"+taskKind+"' AND [timestamp]='"+timestamp+"')");
and for the conversion from QString to Int when I use carda.toInt() Im having this error:
error: invalid operands to binary expression ('const char *' and
'const char [14]')
and warnings:
warning: adding 'int' to a string does not append to the string
use array indexing to silence this warning
I dont understand why QString.toInt() wont be working... any idea?
I dont understand why QString.toInt() wont be working... any idea?
the problem is that in qt you just can't concatenate together strings and numbers...
and actually you dont even need to convert the string carda to integer because that is a QString
instead just do:
QString queryStringAnet("SELECT * FROM [records] WHERE ([user]='" + carda + "' AND [apl]='"+apl+"' AND [tasktype]='"+taskType+"' AND [taskkind]='"+taskKind+"' AND [timestamp]='"+timestamp+"')");
Documentation: http://doc.qt.io/qt-5/qflags.html#operator-Int
The question. I want to know what flags are set withouth testing one by one so I want the int number. Can anyone provide an example of how to use that operator in one of the many qt methods that rerturn a QFlags?
By referring to QFlags.h source code (https://android.googlesource.com/platform/prebuilts/android-emulator-build/qt/+/master/common/include/QtCore/qflags.h)
This is the definition in QFlags for "Int" operator.
Q_DECL_CONSTEXPR inline operator Int() const Q_DECL_NOTHROW { return i; }
And the "i" in return statement is declared as
Int i;
And the "Int" is declared as
typedef int Int
Notice the below two constructors of QFlags. The first constructor takes Enum as parameter and the second constructor takes QFlag as parameter.
Q_DECL_CONSTEXPR inline QFlags(Enum f) Q_DECL_NOTHROW : i(Int(f)) {}
Q_DECL_CONSTEXPR inline QFlags(QFlag f) Q_DECL_NOTHROW : i(f) {}
After noticing the above constructors, if Enum is passed to the constructor, the Enum can be a signed one or unsigned one. QFlags internally type casts it to int using Int.
Consider below example now.
//Qt::CursorShape is an Enum
Qt::CursorShape shape = Qt::ArrowCursor;
//Create QFlags object by passing "ENUM" as parameter
QFlags<Qt::CursorShape> qF(shape);
//Create QFlags object by just passing FLAG as a parameter
QFlags<Qt::CursorShape> q(Qt::ArrowCursor);
Now the situation where "Int" operator is called: In the below piece of code the first statement invokes Int operator and not in the second statement.
//Now try getting the values.
int test = qF; //In this case the "Int" operator is called.
int test1 = q;
I have a struct like this one :
struct Nom {
QString Nom;
....
QList<quint64> indNum;
}
In my .h file. I declare :
QVector *n;
In my .cpp file. I declare :
n = new QVector<Nom>;
I read a file to fill in n.
When I write this :
n->back().indNum.append(i->size()-1);
it works.
When I write that :
n->at(j).indNum.append(i->size()-1);
I have a compilation error:
no matching member funtion for call to 'append'
candidate function not viable: 'this' argument has type 'const
QList', but method is not marked
const void append(const T &t);
I don't understand why it works in the first case and the second.
Could anyone explain and help me solve this ?
Thanks in advance.
QVector::at returns a const reference to the Nom value, so you cannot modify the item returned by n->at(j). To get a non-const reference you can use (*n)[j].
n->back() works because for QVector::back there is a const and a non-const overload.
Using Qt, I want this code to work:
QList<QPair<QString, QString>> list;
foreach (QPair<QString, QString> pair, list)
{
}
instead, I get the error:
'pair' : undeclared identifier
Using a typedef I can make it work, but this is not what I want (unless this is the only thing that works):
typedef QPair<QString, QString> MyPair;
QList<MyPair> list;
foreach (MyPair pair, list)
{
}
Can anyone explain why the first foreach doesn't compile?
it's not the foreach error. It's declaration error. You declared list like this:
QList<QPair<QString, QString>> list;
while it should this way:
QList<QPair<QString, QString> > list;
Just declare QPair outside of loop:
QPair<QString,QString> pair;
foreach(pair,list){
}
It is not possible to use template classes inside qt foreach statement which contains more than one template parameter, because comma separator conflicts with comma separator inside macros.
#define add( a, b ) (a + b)
template < typename T1, typename T2 >
struct DATA
{
static const T1 val1 = 1;
static const T2 val2 = 2;
};
// Usage
const int c = add( 1, 2 ); // OK
const int d = add( DATA< int, int >::val1 , DATA< int, int >::val2 ); // FAIL
because macros add will interpret "DATA< int" as first argument, and " int >::val1" as second, and so on.
Some explanation with above answer... if your compiler accept
QList<QPair<QString, QString>> list;
giving no error on such declaration, reasons for topic caster error is different and indeed has to do with a fact that declaration must be done outside of foreach() loop. That's explained in QT documentation.
regarding >> and > >... that's old story and latest GCC (so linux/mac) consider it to be a syntax mistake, because it's not conforming standard. >> in GCC manner is treated as operator with all follow-up errors..
I am trying to utilize a nested QList:
QMap<int, QMap<QString, QList<int> > > teamGames;
for (int team1 = 1; team1 <= TOTAL_TEAMS; ++team1) {
QMap<QString,QList<int>> games;
teamGames[team1]=games;
QList<int> home;
QList<int> away;
games["home"] = home;
games["away"] = away;
}
teamGames.value(1).value("home").push_back(1);
When I compile I get:
1>.\main.cpp(154) : error C2662: 'QList::push_back' : cannot convert 'this' pointer from 'const QList' to 'QList &'
I'm sure its something simple that I'm overlooking, or maybe there is a simpler solution that's eluding me. Any help greatly appreciated.
As you can see here QMap::value(const Key & key) const; returns a const T, which means you can not modify what you get. Even if you could you would modify a copy of the value you put into the map. What you need is T& QMap::operator[](const Key& key) which returns the value associated with the key as a modifiable reference. So call
((teamGames[1])["home"]).push_back(1);