PDA

View Full Version : foreach or alike



prophet0
29th December 2011, 21:10
iconmodel->setQuery("SELECT `PictureForButtonMode` FROM `dictionary` WHERE `PictureForButtonMode` LIKE '%.%'");

while(iconmodel->query().next())
{
QString buttonFV = iconmodel->query().value(0).toString(); // = String = image file name

foreach(...) // is this possible ?
{
// The code below works when not in this foreach statement
QString bttnPath = QApplication::applicationDirPath();
bttnPath = "/home/dev/ttm/directory/icon/" + buttonFV;

stylesheet += "QListView { show-decoration-selected: 0; border:transparent; color: white; background: transparent ; }";
stylesheet += "QListView::text { padding-right: 300px; }";
stylesheet += "QListView::item {image: url(" +bttnPath + "); width: 135px; height: 207px; }";

iListView->setStyleSheet(stylesheet);
}


this code works without the foreach but im getting the first image for each item and its using the same image for all of them

what im trying to do is for each string assign the string filename to the item.... still cant get it working properly ...

amleto
29th December 2011, 21:17
foreach works with containers what is your container that you want to iterate over?

prophet0
30th December 2011, 02:51
using QSQLQueryModel as my container

Lykurg
30th December 2011, 07:52
you can't use QSQLQueryModel in a foreach loop. It also isn't necessary since the while loop is there already.

Further, what do you try to achieve? You code looks "strange":
iListView->setStyleSheet(stylesheet);will be overwritten each loop, that only the last one will take effect.

prophet0
30th December 2011, 16:32
im trying to change the item image for each query value returned

wysota
1st January 2012, 20:10
How about subclassing the model and reimplementing data() to return the proper icon for Qt::DecorationRole of each respective index?

prophet0
2nd January 2012, 05:12
Sorry but would you be able to show an example of subclassing a model that seems like the perfect way to go

wysota
2nd January 2012, 10:13
Don't you know how to implement a subclass in C++?


class MyModel : public QSqlQueryModel {
public:
MyModel(...) : QSqlQueryModel(...) {}

QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const {
// put your stuff here
}
};

prophet0
3rd January 2012, 16:56
so i've subclassed the model.

how would i insert an image onto one of the items in data ?

wysota
3rd January 2012, 22:33
There are only three Qt keywords: "emit", "slots" and "signals". The rest is pure C++.


how would i insert an image onto one of the items in data ?
You don't add images. You return images as Qt::DecorationRole data from your model.

prophet0
4th January 2012, 16:05
QVariant testdata::data(const QModelIndex &index, int role) const
{
if ( index.column() == 0 )
{
QString imgFile = index.model()->data( index, Qt::DisplayRole );
if ( Qt::DisplayRole == role )
{
return QString();
}
if ( !QFile::exists( imgFile ))
{
imgFile = "/home/dev/ttm/content/ttmad.jpg";
}
QPixmap pixmap( imgFile );
if ( role == Qt::DecorationRole )
{
return pixmap;
}
if(role == Qt::SizeHintRole)
{
return pixmap.size();
}
}
return index.model()->data( index, role );
}


first i get

//testdata.cpp:58: error: conversion from ‘QVariant’ to non-scalar type ‘QString’ requested

wysota
4th January 2012, 17:02
This has no chance of working anyway. In lines #5 and #24 you are effectively calling yourself. As for the error, yes, you are trying to implicitly convert from QVariant to QString. There is no such implicit cast, you have to do the conversion yourself using QVariant API.

prophet0
4th January 2012, 17:24
i would have to change lines #5 & #24 to index.model().data

but how would i convert a QVarient to QString ?

wysota
4th January 2012, 18:42
i would have to change lines #5 & #24 to index.model().data
No, not really.


but how would i convert a QVarient to QString ?
Please read the docs on QVariant.

prophet0
4th January 2012, 19:02
Would it not be easier to use view + custom model and custom delegate ?

wysota
4th January 2012, 20:04
No, it wouldn't. The OP's problem requires writing about 10 lines of code. I doubt you can do better with a custom model and a custom delegate. Actually it would be possible with just the custom delegate (and a standard model), but it would probably still be more than 10 lines of code.

prophet0
4th January 2012, 20:20
sorry still learning Qt just bought the Foundations to Qt Development and watching videos from youtube and countless hours on google and this forum..

If you have any good referances to learning Qt please let me know..

so with that said i'm not looking for anyone to code for me but the paths and direction would serve most useful just trying to learn as much as i can.

wysota
4th January 2012, 22:13
You should start by making sure you know C++ on a decent level before taking on Qt. To me it seems C++ is your problem, not Qt.

prophet0
5th January 2012, 17:03
Thanks for the advice. Thanks for all your help