This is a pretty simple and probably dumb question, but I have forgotten how to use QList QVariant::toList () const
QVariant s = this->page()->mainFrame()->evaluateJavaScript (QString ("Open(%1,%2)").arg (point.x()).arg (point.y()));
List<QVariant> x;
x = s.toList ();
Of course this is wrong, what is the correct way out? :redface:
What you do is almost correct:
QList<QVariant> x = s.toList();
(Note the use of QList instead of List.)
What you're doing is right. May be you can check if the variant contains a list before converting it. E.g:
QVariant variant = list;
if(variant.canConvert(QVariant::List))
{
QList<QVariant> list_1 = variant.toList();
}
Related
if I would call the strcpy function like this:
char *s = NULL;
strcpy(&s, "Test");
in the main function, would this be the best implementation of it:
or there is a better way to implement the function?
Thank you!
First of all: if you take sizeof(src) you will alloc memory for a pointer, not for a string. In 32 bit environments, this will leave you with 4 bytes of memory, and not 10 as you need.
Therefore, you need to supply how much memory do you need, or infere it from your second argument: something like this:
void str_cpy(char** dest, const char* src){
if (*dest == NULL) *dest = malloc(strlen(src)+1);
else *dest = realloc(*dest, strlen(src)+1);
memcpy(*dest, src, strlen(src) + 1);
}
i have main
QStack<TypeFoo*> MainStack
now i have from different objects methods output
QVector<TypeFoo*> OutPutVec
i like to add the elements of OutPutVec to MainStack without looping , just add them to the bottom if the stack , what is the best way to do this ? do i need to convert my OutPutVec
to QStack?
is this good and fast more then loop ?
QVector<T> & QVector::operator+= ( const QVector<T> & other )
QStack inherits QVector and QVector has the following definition:
QVector & operator<< ( const QVector & other )
Therefore you should be able to simply do
MainStack << OutPutVec;
Usually, I use operator << for append containers.
You can choose one of the variant.
This is realisation of operator +=
template <typename T>
QVector<T> &QVector<T>::operator+=(const QVector &l)
{
int newSize = d->size + l.d->size;
realloc(d->size, newSize);
T *w = p->array + newSize;
T *i = l.p->array + l.d->size;
T *b = l.p->array;
while (i != b) {
if (QTypeInfo<T>::isComplex)
new (--w) T(*--i);
else
*--w = *--i;
}
d->size = newSize;
return *this;
}
And realisation of operator <<
inline QVector<T> &operator<<(const QVector<T> &l)
{ *this += l; return *this; }
PS. Creating a loop to copy the data does not make sense, if you can use Qt "buns". Qt appends data faster, then your loop
PSS. If you read the assistant, you might find that the QStack is inherited from the QVector.
I am newbie in QT(4.7.4) and I am search for function, that checks an QString for alpha-characters and returns "true" if in this QString contains only characters.
Should I write this simple function myself? :( I hope it exists such function as isText() in VBA, but in Google and documentation I have not found it.
Thanks for answers and sorry for my english :)
You can simply validate the string with a QRegExp class matching an alphanumeric string. I suggest to use it with QValidator to be more clear.
You could use something like this (If your goal is to accept only strings, which contains a single character):
bool containsOnly(QString str, QChar c)
{
for(int i=0; i<str.length(); i++)
if(str.at(i)!=c)
return false;
return true;
}
and in use:
bool b = containsOnly("String", 'a');
How can I convert a QVector<char*> to a QStringList?
There is no direct way.
QVector<char*> vector;
QStringList list;
foreach (const char * str, vector) {
list << str;
}
I think you can try do it in this way (it works for me if I have a QVector<QString>):
QVector<char*> vector_char;
QStringList myStringList = vector_char.toList();
This code has to work as QStringList has inherited members of QList.
Here is somenthing in the documentation: QVector documentation.
And that is all.
I hope it can help you.
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);