Results 1 to 19 of 19

Thread: Setting an icon to an Item in a QlistWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Setting an icon to an Item in a QlistWidget

    Hello friends,

    How can i set an icon to an Item in my Qlistwidget my code is like this:

    Qt Code:
    1. QDockWidget *dock = new QDockWidget(tr("Verwaltung"), this);
    2. QFont font("Verdana", 10, QFont::Bold);
    3. dock->setFont(font);
    4. //set the color of the dockwidget
    5. dock->setAutoFillBackground(true);
    6. QPalette qpal;
    7. qpal.setColor(QPalette::Active,QPalette::Window,QColor(210,239,109));
    8. dock->setBackgroundRole(QPalette::Window);
    9. dock->setPalette(qpal);
    10. //end set color
    11. dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    12. customerList = new QListWidget(dock);
    13. customerList->addItems(QStringList("Bezirksverwaltung"));
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting an icon to an Item in a QlistWidget

    If you have a QStringList you want to add to the view:
    Qt Code:
    1. QIcon icon=...
    2. foreach(QString str, myStringList) {
    3. QListWidgetItem *item = new QListWidgetItem(icon, str, customerList);
    4. customerList->addItem(item);
    5. }
    To copy to clipboard, switch view to plain text mode 

    EDIT:
    Actually, you don't have to use addItem when you pass the list widget as parent in the list widget item's constructor.

    regards
    Last edited by marcel; 23rd August 2007 at 20:44.

  3. #3
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Setting an icon to an Item in a QlistWidget

    Hello thanks but some problems occures:

    Qt Code:
    1. customerList = new QListWidget(dock);
    2. QIcon icon=new QIcon(":/images/labelic1.ico");
    3. foreach(QString str, myStringList) {
    4. QListWidgetItem *item = new QListWidgetItem(icon, str, customerList);
    5. customerList->addItem(item);
    To copy to clipboard, switch view to plain text mode 

    what is str and customerList?

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting an icon to an Item in a QlistWidget

    what is str and customerList?
    Funny you ask .
    customerList is your QListWidget.
    str is a QString and it represents the text you want your item to display(besides the icon).

    EDIT: and myStringList is a QStringList containing the strings of the items you want to add( f.e. the names of the customers)

    Regards

  5. #5
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Setting an icon to an Item in a QlistWidget

    ;o)) i meant myStrList.....

    so I have some code:

    Qt Code:
    1. QDockWidget *dock = new QDockWidget(tr("Verwaltung"), this);
    2. QFont font("Verdana", 10, QFont::Bold);
    3. dock->setFont(font);
    4. //set the color of the dockwidget
    5. dock->setAutoFillBackground(true);
    6. QPalette qpal;
    7. qpal.setColor(QPalette::Active,QPalette::Window,QColor(210,239,109));
    8. dock->setBackgroundRole(QPalette::Window);
    9. dock->setPalette(qpal);
    10. //end set color
    11. dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    12. customerList = new QListWidget(dock);
    13. QStringList myStringList("hallo","servus");
    14. QIcon *icon=new QIcon(":/images/labelic1.ico");
    15. foreach(QString str, myStringList) {
    16. QListWidgetItem *item = new QListWidgetItem(icon, str, customerList);
    17. customerList->addItem(item);
    18. }
    19. dock->setWidget(customerList);
    20. addDockWidget(Qt::LeftDockWidgetArea, dock);
    21. viewMenu->addAction(dock->toggleViewAction());
    To copy to clipboard, switch view to plain text mode 

    But iI get some Errors like this:

    Qt Code:
    1. mainwindow.cpp: In member function `void MainWindow::createDockWindows()':
    2. mainwindow.cpp:160: error: no matching function for call to `QStringList::QStringList(const char[6], const char[7])'
    3. c:/Qt/4.3.1/include/QtCore/../../src/corelib/tools/qstringlist.h:67: note: candidates are: QStringList::QStringList(const QList<QString>&)
    4. c:/Qt/4.3.1/include/QtCore/../../src/corelib/tools/qstringlist.h:66: note: QStringList::QStringList(const QStringList&)
    5. c:/Qt/4.3.1/include/QtCore/../../src/corelib/tools/qstringlist.h:65: note: QStringList::QStringList(const QString&)
    6. c:/Qt/4.3.1/include/QtCore/../../src/corelib/tools/qstringlist.h:64: note: QStringList::QStringList()
    7. mainwindow.cpp:163: error: no matching function for call to `QListWidgetItem::QListWidgetItem(QIcon*&, QString&, QListWidget*&)'
    8. c:/Qt/4.3.1/include/QtGui/../../src/gui/itemviews/qlistwidget.h:67: note: candidates are: QListWidgetItem::QListWidgetItem(const QListWidgetItem&)
    9. c:/Qt/4.3.1/include/QtGui/../../src/gui/itemviews/qlistwidget.h:66: note: QListWidgetItem::QListWidgetItem(const QIcon&, const QString&, QListWidget*, int)
    10. c:/Qt/4.3.1/include/QtGui/../../src/gui/itemviews/qlistwidget.h:64: note: QListWidgetItem::QListWidgetItem(const QString&, QListWidget*, int)
    11. c:/Qt/4.3.1/include/QtGui/../../src/gui/itemviews/qlistwidget.h:63: note: QListWidgetItem::QListWidgetItem(QListWidget*, int)
    12. mingw32-make[1]: *** [release\mainwindow.o] Error 1
    13. mingw32-make: *** [release] Error 2
    14. mingw32-make[1]: Leaving directory `J:/qtprojects/Modest-Office'
    15. ---------------------- Build finished with 4 error(s) and 3 warning(s) ----------------------
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting an icon to an Item in a QlistWidget

    Qt Code:
    1. QStringList myStringList;
    2. myStringList << "item1" << "item2" << "item3"; //and so on...
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Setting an icon to an Item in a QlistWidget

    Ok

    Qt Code:
    1. QStringList myStringList;
    2. myStringList <<"Bezirksverwaltung" << "FAkturierung";
    3. QIcon *icon=new QIcon(":/images/labelic1.ico");
    4. foreach(QString str, myStringList) {
    5. QListWidgetItem *item = new QListWidgetItem(icon, str, customerList);
    6. customerList->addItem(item);
    To copy to clipboard, switch view to plain text mode 

    I have always one error :

    Qt Code:
    1. main.cpp:23:2: warning: no newline at end of file
    2. In file included from mainwindow.cpp:1:
    3. mainwindow.h:57:23: warning: no newline at end of file
    4. mainwindow.cpp: In member function `void MainWindow::createDockWindows()':
    5. mainwindow.cpp:164: error: no matching function for call to `QListWidgetItem::QListWidgetItem(QIcon*&, QString&, QListWidget*&)'
    6. c:/Qt/4.3.1/include/QtGui/../../src/gui/itemviews/qlistwidget.h:67: note: candidates are: QListWidgetItem::QListWidgetItem(const QListWidgetItem&)
    7. c:/Qt/4.3.1/include/QtGui/../../src/gui/itemviews/qlistwidget.h:66: note: QListWidgetItem::QListWidgetItem(const QIcon&, const QString&, QListWidget*, int)
    8. c:/Qt/4.3.1/include/QtGui/../../src/gui/itemviews/qlistwidget.h:64: note: QListWidgetItem::QListWidgetItem(const QString&, QListWidget*, int)
    9. c:/Qt/4.3.1/include/QtGui/../../src/gui/itemviews/qlistwidget.h:63: note: QListWidgetItem::QListWidgetItem(QListWidget*, int)
    10. mingw32-make[1]: Leaving directory `J:/qtprojects/Modest-Office'
    11. mingw32-make[1]: *** [release\mainwindow.o] Error 1
    12. mingw32-make: *** [release] Error 2
    13. ---------------------- Build finished with 3 error(s) and 3 warning(s) ----------------------
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting an icon to an Item in a QlistWidget

    It is:
    Qt Code:
    1. QListWidgetItem *item = new QListWidgetItem(*icon, str, customerList);
    To copy to clipboard, switch view to plain text mode 
    because you create the QIcon on the heap. It is a pointer. The constructor takes as parameter a const reference to a QIcon, not a pointer.

    Regards

  9. #9
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Setting an icon to an Item in a QlistWidget

    And how can i create my QIcon in a right way??

    like this Qicon *icon; icon=("......");

    or like this QIcon *icon("....."); ????

  10. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting an icon to an Item in a QlistWidget

    Like this:
    Qt Code:
    1. QIcon icon(":/images/labelic1.ico");
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Setting an icon to an Item in a QlistWidget

    Ok i hope i Dónt mess up your nervs with my question but i am new to qt.
    your code compiles atm without errors but i can´t see my icon have you an idea??

Similar Threads

  1. QListWidget and selecting an item
    By invictus in forum Newbie
    Replies: 4
    Last Post: 19th June 2007, 11:59
  2. Replies: 1
    Last Post: 19th April 2007, 22:23
  3. QListWidget in ListMode - still has icon space
    By high_flyer in forum Newbie
    Replies: 10
    Last Post: 30th March 2007, 19:43
  4. extract item from QListWidget
    By impeteperry in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2006, 19:41
  5. keypress while editing an item in QListWidget
    By Beluvius in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 09:56

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.