Results 1 to 19 of 19

Thread: foreach or alike

  1. #1
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default foreach or alike

    Qt Code:
    1. iconmodel->setQuery("SELECT `PictureForButtonMode` FROM `dictionary` WHERE `PictureForButtonMode` LIKE '%.%'");
    2.  
    3. while(iconmodel->query().next())
    4. {
    5. QString buttonFV = iconmodel->query().value(0).toString(); // = String = image file name
    6.  
    7. foreach(...) // is this possible ?
    8. {
    9. // The code below works when not in this foreach statement
    10. QString bttnPath = QApplication::applicationDirPath();
    11. bttnPath = "/home/dev/ttm/directory/icon/" + buttonFV;
    12.  
    13. stylesheet += "QListView { show-decoration-selected: 0; border:transparent; color: white; background: transparent ; }";
    14. stylesheet += "QListView::text { padding-right: 300px; }";
    15. stylesheet += "QListView::item {image: url(" +bttnPath + "); width: 135px; height: 207px; }";
    16.  
    17. iListView->setStyleSheet(stylesheet);
    18. }
    To copy to clipboard, switch view to plain text mode 

    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 ...

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: foreach or alike

    foreach works with containers what is your container that you want to iterate over?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: foreach or alike

    using QSQLQueryModel as my container

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: foreach or alike

    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":
    Qt Code:
    1. iListView->setStyleSheet(stylesheet);
    To copy to clipboard, switch view to plain text mode 
    will be overwritten each loop, that only the last one will take effect.

  5. #5
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: foreach or alike

    im trying to change the item image for each query value returned

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: foreach or alike

    How about subclassing the model and reimplementing data() to return the proper icon for Qt::DecorationRole of each respective index?
    Last edited by wysota; 1st January 2012 at 20:16.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: foreach or alike

    Sorry but would you be able to show an example of subclassing a model that seems like the perfect way to go

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: foreach or alike

    Don't you know how to implement a subclass in C++?

    Qt Code:
    1. class MyModel : public QSqlQueryModel {
    2. public:
    3. MyModel(...) : QSqlQueryModel(...) {}
    4.  
    5. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const {
    6. // put your stuff here
    7. }
    8. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: foreach or alike

    so i've subclassed the model.

    how would i insert an image onto one of the items in data ?
    Last edited by prophet0; 3rd January 2012 at 16:56.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: foreach or alike

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: foreach or alike

    Qt Code:
    1. QVariant testdata::data(const QModelIndex &index, int role) const
    2. {
    3. if ( index.column() == 0 )
    4. {
    5. QString imgFile = index.model()->data( index, Qt::DisplayRole );
    6. if ( Qt::DisplayRole == role )
    7. {
    8. return QString();
    9. }
    10. if ( !QFile::exists( imgFile ))
    11. {
    12. imgFile = "/home/dev/ttm/content/ttmad.jpg";
    13. }
    14. QPixmap pixmap( imgFile );
    15. if ( role == Qt::DecorationRole )
    16. {
    17. return pixmap;
    18. }
    19. if(role == Qt::SizeHintRole)
    20. {
    21. return pixmap.size();
    22. }
    23. }
    24. return index.model()->data( index, role );
    25. }
    To copy to clipboard, switch view to plain text mode 

    first i get

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

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: foreach or alike

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: foreach or alike

    i would have to change lines #5 & #24 to index.model().data

    but how would i convert a QVarient to QString ?
    Last edited by prophet0; 4th January 2012 at 17:35.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: foreach or alike

    Quote Originally Posted by prophet0 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: foreach or alike

    Would it not be easier to use view + custom model and custom delegate ?

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: foreach or alike

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #17
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: foreach or alike

    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.

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: foreach or alike

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #19
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: foreach or alike

    Thanks for the advice. Thanks for all your help

Similar Threads

  1. foreach error while compiling
    By smanoj in forum Newbie
    Replies: 4
    Last Post: 24th November 2011, 05:32
  2. QSplitter Skype-alike
    By davideanastasia in forum Newbie
    Replies: 3
    Last Post: 2nd June 2011, 14:26
  3. foreach QTextEdit in Layout
    By slc in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2010, 00:45
  4. lifetime of foreach
    By BalaQT in forum Newbie
    Replies: 4
    Last Post: 4th March 2010, 15:55
  5. Foreach performance
    By jano_alex_es in forum General Programming
    Replies: 2
    Last Post: 17th November 2009, 13:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.