Results 1 to 19 of 19

Thread: Setting an icon to an Item in a QlistWidget

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

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

    Probably you didn't compiled the resource file or it is not correct. Can you post it?

    Regards

  13. #13
    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. 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.  
    14. QStringList myStringList;
    15. myStringList <<"Bezirksverwaltung" << "Fakturierung";
    16. QIcon icon(":/images/labelic1.ico");
    17. foreach(QString str, myStringList) {
    18. QListWidgetItem *item = new QListWidgetItem(icon, str, customerList);
    19. customerList->addItem(item);
    20. }
    21.  
    22. dock->setWidget(customerList);
    23. addDockWidget(Qt::LeftDockWidgetArea, dock);
    24. viewMenu->addAction(dock->toggleViewAction());
    25. QDockWidget *dock2 = new QDockWidget(tr("Produktion"), this);
    26. dock2->setFont(font);
    To copy to clipboard, switch view to plain text mode 


    An here are the Output from the Compiler:
    Qt Code:
    1. Update project (qmake Modest-Office.pro)...
    2. Clean Project (make clean)...
    3. mingw32-make -f Makefile.Release clean
    4. mingw32-make[1]: Entering directory `J:/qtprojects/Modest-Office'
    5. del release\moc_mainwindow.cpp
    6. del release\qrc_dockwidgets.cpp
    7. del release\main.o release\mainwindow.o release\moc_mainwindow.o release\qrc_dockwidgets.o
    8. mingw32-make[1]: Leaving directory `J:/qtprojects/Modest-Office'
    9. mingw32-make -f Makefile.Debug clean
    10. mingw32-make[1]: Entering directory `J:/qtprojects/Modest-Office'
    11. del debug\moc_mainwindow.cpp
    12. del debug\qrc_dockwidgets.cpp
    13. J:\qtprojects\Modest-Office\debug\moc_mainwindow.cpp konnte nicht gefunden werden
    14. del debug\main.o debug\mainwindow.o debug\moc_mainwindow.o debug\qrc_dockwidgets.o
    15. J:\qtprojects\Modest-Office\debug\qrc_dockwidgets.cpp konnte nicht gefunden werden
    16. mingw32-make[1]: Leaving directory `J:/qtprojects/Modest-Office'
    17. J:\qtprojects\Modest-Office\debug\main.o konnte nicht gefunden werden
    18. Build (make)...
    19. mingw32-make -f Makefile.Release
    20. mingw32-make[1]: Entering directory `J:/qtprojects/Modest-Office'
    21. g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"c:\Qt\4.3.1\include\QtCore" -I"c:\Qt\4.3.1\include\QtCore" -I"c:\Qt\4.3.1\include\QtGui" -I"c:\Qt\4.3.1\include\QtGui" -I"c:\Qt\4.3.1\include" -I"." -I"c:\Qt\4.3.1\include\ActiveQt" -I"release" -I"." -I"c:\Qt\4.3.1\mkspecs\default" -o release\main.o main.cpp
    22. g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"c:\Qt\4.3.1\include\QtCore" -I"c:\Qt\4.3.1\include\QtCore" -I"c:\Qt\4.3.1\include\QtGui" -I"c:\Qt\4.3.1\include\QtGui" -I"c:\Qt\4.3.1\include" -I"." -I"c:\Qt\4.3.1\include\ActiveQt" -I"release" -I"." -I"c:\Qt\4.3.1\mkspecs\default" -o release\mainwindow.o mainwindow.cpp
    23. In file included from main.cpp:5:
    24. mainwindow.h:57:23: warning: no newline at end of file
    25. main.cpp:23:2: warning: no newline at end of file
    26. C:\Qt\4.3.1\bin\moc.exe -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"c:\Qt\4.3.1\include\QtCore" -I"c:\Qt\4.3.1\include\QtCore" -I"c:\Qt\4.3.1\include\QtGui" -I"c:\Qt\4.3.1\include\QtGui" -I"c:\Qt\4.3.1\include" -I"." -I"c:\Qt\4.3.1\include\ActiveQt" -I"release" -I"." -I"c:\Qt\4.3.1\mkspecs\default" -D__GNUC__ -DWIN32 mainwindow.h -o release\moc_mainwindow.cpp
    27. g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"c:\Qt\4.3.1\include\QtCore" -I"c:\Qt\4.3.1\include\QtCore" -I"c:\Qt\4.3.1\include\QtGui" -I"c:\Qt\4.3.1\include\QtGui" -I"c:\Qt\4.3.1\include" -I"." -I"c:\Qt\4.3.1\include\ActiveQt" -I"release" -I"." -I"c:\Qt\4.3.1\mkspecs\default" -o release\moc_mainwindow.o release\moc_mainwindow.cpp
    28. In file included from mainwindow.cpp:1:
    29. mainwindow.h:57:23: warning: no newline at end of file
    30. c:\Qt\4.3.1\bin\rcc.exe -name dockwidgets dockwidgets.qrc -o release\qrc_dockwidgets.cpp
    31. In file included from release\moc_mainwindow.cpp:10:
    32. release\/../mainwindow.h:57:23: warning: no newline at end of file
    33. g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"c:\Qt\4.3.1\include\QtCore" -I"c:\Qt\4.3.1\include\QtCore" -I"c:\Qt\4.3.1\include\QtGui" -I"c:\Qt\4.3.1\include\QtGui" -I"c:\Qt\4.3.1\include" -I"." -I"c:\Qt\4.3.1\include\ActiveQt" -I"release" -I"." -I"c:\Qt\4.3.1\mkspecs\default" -o release\qrc_dockwidgets.o release\qrc_dockwidgets.cpp
    34. g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -mthreads -Wl -Wl,-subsystem,windows -o "release\Modest-Office.exe" release\main.o release\mainwindow.o release\moc_mainwindow.o release\qrc_dockwidgets.o -L"c:\Qt\4.3.1\lib" -lmingw32 -lqtmain -lQtGui4 -lQtCore4
    35. mingw32-make[1]: Leaving directory `J:/qtprojects/Modest-Office'
    36. ---------------------- Build finished with 4 warning(s) ----------------------
    To copy to clipboard, switch view to plain text mode 


    And I add a Screenshot from my App!
    Last edited by LordQt; 23rd August 2007 at 22:29.

  14. #14
    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 this is my Resource:

    Qt Code:
    1. <!DOCTYPE RCC><RCC version="1.0">
    2. <qresource>
    3. <file>images/new.png</file>
    4. <file>images/print.png</file>
    5. <file>images/save.png</file>
    6. <file>images/undo.png</file>
    7. <file>images/Modestsplash.png</file>
    8. <file>images/labelic1.ico</file>
    9. </qresource>
    10. </RCC>
    To copy to clipboard, switch view to plain text mode 

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

    Add RESOURCES = yourresource.qrc to the pro file( or run qmake -project again).

    Make sure you replace the qrc name with the appropriate one.

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

    hmmmm the resources += is on top and the name is correct but nothing seems to change i don´t see my icon ????And i compile it again

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

    Is then the relative path correct?
    Judging from the path the images directory should be in the same dir with the qrc file.

    Regards

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

    My icon is in the images folder the pro file one level higher or what du you mean?

  19. #19
    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 i solve it!!

    my icon is corrupt i take a png format and it runs. But by this way the code is written by automatic appending the icon to the item. But right now i have one icon for each item in my dockwidget. How can i put seperate icon to each item??

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.