PDA

View Full Version : Setting an icon to an Item in a QlistWidget



LordQt
23rd August 2007, 20:30
Hello friends,

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


QDockWidget *dock = new QDockWidget(tr("Verwaltung"), this);
QFont font("Verdana", 10, QFont::Bold);
dock->setFont(font);
//set the color of the dockwidget
dock->setAutoFillBackground(true);
QPalette qpal;
qpal.setColor(QPalette::Active,QPalette::Window,QC olor(210,239,109));
dock->setBackgroundRole(QPalette::Window);
dock->setPalette(qpal);
//end set color
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
customerList = new QListWidget(dock);
customerList->addItems(QStringList("Bezirksverwaltung"));

marcel
23rd August 2007, 20:34
If you have a QStringList you want to add to the view:


QIcon icon=...
foreach(QString str, myStringList) {
QListWidgetItem *item = new QListWidgetItem(icon, str, customerList);
customerList->addItem(item);
}


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

LordQt
23rd August 2007, 21:10
Hello thanks but some problems occures:


customerList = new QListWidget(dock);
QIcon icon=new QIcon(":/images/labelic1.ico");
foreach(QString str, myStringList) {
QListWidgetItem *item = new QListWidgetItem(icon, str, customerList);
customerList->addItem(item);

what is str and customerList?

marcel
23rd August 2007, 21:14
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

LordQt
23rd August 2007, 21:27
;o)) i meant myStrList.....

so I have some code:



QDockWidget *dock = new QDockWidget(tr("Verwaltung"), this);
QFont font("Verdana", 10, QFont::Bold);
dock->setFont(font);
//set the color of the dockwidget
dock->setAutoFillBackground(true);
QPalette qpal;
qpal.setColor(QPalette::Active,QPalette::Window,QC olor(210,239,109));
dock->setBackgroundRole(QPalette::Window);
dock->setPalette(qpal);
//end set color
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
customerList = new QListWidget(dock);
QStringList myStringList("hallo","servus");
QIcon *icon=new QIcon(":/images/labelic1.ico");
foreach(QString str, myStringList) {
QListWidgetItem *item = new QListWidgetItem(icon, str, customerList);
customerList->addItem(item);
}
dock->setWidget(customerList);
addDockWidget(Qt::LeftDockWidgetArea, dock);
viewMenu->addAction(dock->toggleViewAction());


But iI get some Errors like this:



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

marcel
23rd August 2007, 21:29
QStringList myStringList;
myStringList << "item1" << "item2" << "item3"; //and so on...

LordQt
23rd August 2007, 21:33
Ok



QStringList myStringList;
myStringList <<"Bezirksverwaltung" << "FAkturierung";
QIcon *icon=new QIcon(":/images/labelic1.ico");
foreach(QString str, myStringList) {
QListWidgetItem *item = new QListWidgetItem(icon, str, customerList);
customerList->addItem(item);


I have always one error :



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

marcel
23rd August 2007, 21:36
It is:


QListWidgetItem *item = new QListWidgetItem(*icon, str, customerList);

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

LordQt
23rd August 2007, 21:43
And how can i create my QIcon in a right way??

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

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

marcel
23rd August 2007, 21:44
Like this:


QIcon icon(":/images/labelic1.ico");

LordQt
23rd August 2007, 21:48
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??

marcel
23rd August 2007, 21:51
Probably you didn't compiled the resource file or it is not correct. Can you post it?

Regards

LordQt
23rd August 2007, 21:56
Ok



QDockWidget *dock = new QDockWidget(tr("Verwaltung"), this);
QFont font("Verdana", 10, QFont::Bold);
dock->setFont(font);
//set the color of the dockwidget
dock->setAutoFillBackground(true);
QPalette qpal;
qpal.setColor(QPalette::Active,QPalette::Window,QC olor(210,239,109));
dock->setBackgroundRole(QPalette::Window);
dock->setPalette(qpal);
//end set color
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
customerList = new QListWidget(dock);

QStringList myStringList;
myStringList <<"Bezirksverwaltung" << "Fakturierung";
QIcon icon(":/images/labelic1.ico");
foreach(QString str, myStringList) {
QListWidgetItem *item = new QListWidgetItem(icon, str, customerList);
customerList->addItem(item);
}

dock->setWidget(customerList);
addDockWidget(Qt::LeftDockWidgetArea, dock);
viewMenu->addAction(dock->toggleViewAction());
QDockWidget *dock2 = new QDockWidget(tr("Produktion"), this);
dock2->setFont(font);


An here are the Output from the Compiler:


Update project (qmake Modest-Office.pro)...
Clean Project (make clean)...
mingw32-make -f Makefile.Release clean
mingw32-make[1]: Entering directory `J:/qtprojects/Modest-Office'
del release\moc_mainwindow.cpp
del release\qrc_dockwidgets.cpp
del release\main.o release\mainwindow.o release\moc_mainwindow.o release\qrc_dockwidgets.o
mingw32-make[1]: Leaving directory `J:/qtprojects/Modest-Office'
mingw32-make -f Makefile.Debug clean
mingw32-make[1]: Entering directory `J:/qtprojects/Modest-Office'
del debug\moc_mainwindow.cpp
del debug\qrc_dockwidgets.cpp
J:\qtprojects\Modest-Office\debug\moc_mainwindow.cpp konnte nicht gefunden werden
del debug\main.o debug\mainwindow.o debug\moc_mainwindow.o debug\qrc_dockwidgets.o
J:\qtprojects\Modest-Office\debug\qrc_dockwidgets.cpp konnte nicht gefunden werden
mingw32-make[1]: Leaving directory `J:/qtprojects/Modest-Office'
J:\qtprojects\Modest-Office\debug\main.o konnte nicht gefunden werden
Build (make)...
mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory `J:/qtprojects/Modest-Office'
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
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
In file included from main.cpp:5:
mainwindow.h:57:23: warning: no newline at end of file
main.cpp:23:2: warning: no newline at end of file
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
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
In file included from mainwindow.cpp:1:
mainwindow.h:57:23: warning: no newline at end of file
c:\Qt\4.3.1\bin\rcc.exe -name dockwidgets dockwidgets.qrc -o release\qrc_dockwidgets.cpp
In file included from release\moc_mainwindow.cpp:10:
release\/../mainwindow.h:57:23: warning: no newline at end of file
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
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
mingw32-make[1]: Leaving directory `J:/qtprojects/Modest-Office'
---------------------- Build finished with 4 warning(s) ----------------------


And I add a Screenshot from my App!

LordQt
23rd August 2007, 21:57
and this is my Resource:



<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>images/new.png</file>
<file>images/print.png</file>
<file>images/save.png</file>
<file>images/undo.png</file>
<file>images/Modestsplash.png</file>
<file>images/labelic1.ico</file>
</qresource>
</RCC>

marcel
23rd August 2007, 22:04
Add RESOURCES = yourresource.qrc to the pro file( or run qmake -project again).

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

LordQt
23rd August 2007, 22:12
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

marcel
23rd August 2007, 22:13
Is then the relative path correct?
Judging from the path the images directory should be in the same dir with the qrc file.

Regards

LordQt
23rd August 2007, 22:16
My icon is in the images folder the pro file one level higher or what du you mean?

LordQt
23rd August 2007, 22:24
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??