Qt Port update.

Lubomir I. Ivanov neolit123 at gmail.com
Sun Apr 14 07:26:34 PDT 2013


On 14 April 2013 06:47, Dirk Hohndel <dirk at hohndel.org> wrote:
> Thiago Macieira <thiago at macieira.org> writes:
>
>> On sábado, 13 de abril de 2013 14.25.24, Dirk Hohndel wrote:
>>> void MainTab::clearStats()
>>> {
>>>         QList<QLabel*> labels;
>>>         labels << ui->maxdepth_2 << ui->mindepth << ui->avgdepth
>>>                 << ui->maxsac << ui->minsac << ui->avgsac
>>>                 << ui->dives << ui->maxtemp << ui->mintemp << ui->avgtemp
>>>                 << ui->totaltime << ui->avgtime << ui->longestdive <<
>>> ui->shortestdive;
>>>
>>>         Q_FOREACH(QLabel *l, labels){
>>>                 l->setText(QString());
>>>         }
>>> }
>>>
>>> What exactly does that series of '<<' do in this context? I find this
>>> particularly hard to read...
>>
>> In class QList:
>>
>>     inline QList<T> &operator<< (const T &t)
>>     { append(t); return *this; }
>>
>> That operator is very useful in some contexts. Here, Tomaz was using to add
>> all the labels to one QList so he could use foreach on the list.
>
> I repeat - I find this incredibly hard to read...
>

thinking about alternatives, since QList accepts type <T> i don't
think C varags (with the macro va_arg) and a wrapper macro are a safe
bet, so the C++ stream operator is much safer here, especially in the
case of non-primative types like QLabel. so something like Glib's way
qlist_init(the_list, QLabel, value1, value2, value3, NULL) isn't
really an option, AFAIK. the best looking, safer and portable option
for C programmers will be individual calls to "append":

list.append(ui->maxdepth_2);
list.append(...);

as a bold alternative we could enable:
CONFIG += c++11

at this point, i don't think this is an issue on windows and same goes
for linux. not sure about what the situation is with osx, because they
make some weird decisions over there. also if we have plans to target
mobile eventually, this has to be investigated further.

it will allow usage of std::initializer_list:
QList<QLabel *> labels = {ui->maxdepth_2, ui->mindepth, ......};
instead of "list << value" and "list.append(value)"

looks pretty similar to c struct initialisers on the user side. behind
the scenes however C++ initializer list aren't exactly pretty as can
be seen here: http://en.cppreference.com/w/cpp/utility/initializer_list

a feature which C++ has been lacking for quite some time, yet
available in other high level languages.

lubomir
--


More information about the subsurface mailing list