Results 1 to 8 of 8

Thread: Trying to use delegates...

  1. #1
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    1

    Default Trying to use delegates...

    Hi, I'm a newbie to Qt, and I'm trying to find out how to change the way list items are drawn (in general). I figured the simplest way to do this would be to use delegates. I'm using QtCreator, so some of the code is generated by it:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtGui>
    4. #include <QPainter>
    5. #include <QString>
    6.  
    7. class ItemDelegate : public QItemDelegate
    8. {
    9. Q_OBJECT
    10. public:
    11. inline ItemDelegate(QObject *mainWindow) : QItemDelegate(mainWindow) {}
    12.  
    13. inline void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    14.  
    15. {
    16. painter->drawText(QPoint(option.Left + 20, option.Top), "Bloop!");
    17. }
    18. };
    19.  
    20. MainWindow::MainWindow(QWidget *parent)
    21. : QMainWindow(parent), ui(new Ui::MainWindow)
    22. {
    23. ui->setupUi(this);
    24. ui->listWidget->setItemDelegate(new ItemDelegate(this));
    25. ui->listWidget->addItem("Item 1");
    26. ui->listWidget->addItem("Item 2");
    27. ui->listWidget->addItem("Item 3");
    28. }
    29.  
    30. MainWindow::~MainWindow()
    31. {
    32. delete ui;
    33. }
    To copy to clipboard, switch view to plain text mode 

    Right now, I'm just trying to add a margin to the left of the text, just as a test. The problem is, the program doesn't seem to run, instead crashing with an exit code of one. I've tried using different class types, etc, but nothing quite seems to work. Any help would be greatly appreciated. Thanks.

  2. #2
    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: Trying to use delegates...

    Does it work if you remove this line?
    Qt Code:
    1. ui->listWidget->setItemDelegate(new ItemDelegate(this));
    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.


  3. #3
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    1

    Default Re: Trying to use delegates...

    Yes, I forgot to mention that the program will run if I remove that line.

  4. #4
    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: Trying to use delegates...

    Try this:
    Qt Code:
    1. class ItemDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. ItemDelegate(QObject *parent = 0) : QItemDelegate(parent) {}
    6.  
    7. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    8. {
    9. painter->drawText(QPoint(option.rect.left() + 20, option.rect.top()), "Bloop!");
    10. }
    11. };
    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.


  5. #5
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    1

    Default Re: Trying to use delegates...

    Same result, unfortunately.

  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: Trying to use delegates...

    Can we see the backtrace from the debugger?
    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
    Aug 2009
    Posts
    4
    Thanks
    1

    Default Re: Trying to use delegates...

    I don't know where to find the debugger traceback in this IDE, but the compiler did spit out this message (toward the bottom) that might be helpful:

    Qt Code:
    1. Running build steps for project listtest2...
    2. Configuration unchanged, skipping QMake step.
    3. Starting: C:/Qt/2009.03/mingw/bin/mingw32-make.exe -w
    4. mingw32-make: Entering directory `C:/Documents and Settings/Administrator/My Documents/listtest2'
    5. C:/Qt/2009.03/mingw/bin/mingw32-make -f Makefile.Debug
    6. mingw32-make[1]: Entering directory `C:/Documents and Settings/Administrator/My Documents/listtest2'
    7. c:\Qt\2009.03\qt\bin\uic.exe mainwindow.ui -o ui_mainwindow.h
    8. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\2009.03\qt\include\QtCore" -I"..\..\..\..\Qt\2009.03\qt\include\QtGui" -I"..\..\..\..\Qt\2009.03\qt\include" -I"..\..\..\..\Qt\2009.03\qt\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\2009.03\qt\mkspecs\win32-g++" -o debug\main.o main.cpp
    9. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\2009.03\qt\include\QtCore" -I"..\..\..\..\Qt\2009.03\qt\include\QtGui" -I"..\..\..\..\Qt\2009.03\qt\include" -I"..\..\..\..\Qt\2009.03\qt\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\2009.03\qt\mkspecs\win32-g++" -o debug\mainwindow.o mainwindow.cpp
    10. C:/Qt/2009.03/qt/bin\moc.exe -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\2009.03\qt\include\QtCore" -I"..\..\..\..\Qt\2009.03\qt\include\QtGui" -I"..\..\..\..\Qt\2009.03\qt\include" -I"..\..\..\..\Qt\2009.03\qt\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\2009.03\qt\mkspecs\win32-g++" -D__GNUC__ -DWIN32 mainwindow.h -o debug\moc_mainwindow.cpp
    11. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\2009.03\qt\include\QtCore" -I"..\..\..\..\Qt\2009.03\qt\include\QtGui" -I"..\..\..\..\Qt\2009.03\qt\include" -I"..\..\..\..\Qt\2009.03\qt\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\2009.03\qt\mkspecs\win32-g++" -o debug\moc_mainwindow.o debug\moc_mainwindow.cpp
    12. g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug\listtest2.exe debug/main.o debug/mainwindow.o debug/moc_mainwindow.o -L"c:\Qt\2009.03\qt\lib" -lmingw32 -lqtmaind -lQtGuid4 -lQtCored4
    13. debug/mainwindow.o: In function `ZThn8_N10MainWindowD1Ev':
    14. C:/Documents and Settings/Administrator/My Documents/listtest2/mainwindow.cpp:(.text$_ZN12ItemDelegateC1EP7QObject[ItemDelegate::ItemDelegate(QObject*)]+0x1f): undefined reference to `vtable for ItemDelegate'
    15. collect2: ld returned 1 exit status
    16. mingw32-make[1]: Leaving directory `C:/Documents and Settings/Administrator/My Documents/listtest2'
    17. mingw32-make: Leaving directory `C:/Documents and Settings/Administrator/My Documents/listtest2'
    18. mingw32-make[1]: *** [debug\listtest2.exe] Error 1
    19. mingw32-make: *** [debug] Error 2
    20. Exited with code 2.
    21. Error while building project listtest2
    22. When executing build step 'Make'
    To copy to clipboard, switch view to plain text mode 

    Thanks for all your help thus far.

  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: Trying to use delegates...

    This is a compilation error! Your program does not crash with error 1. Your compiler exits with error 1.

    The error is:
    debug/mainwindow.o: In function `ZThn8_N10MainWindowD1Ev':
    C:/Documents and Settings/Administrator/My Documents/listtest2/mainwindow.cpp.text$_ZN12ItemDelegateC1EP7QObject[ItemDelegate::ItemDelegate(QObject*)]+0x1f): undefined reference to `vtable for ItemDelegate'
    Remove the Q_OBJECT macro or rerun qmake before compiling the project.
    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. The following user says thank you to wysota for this useful post:

    foahchon (28th August 2009)

Similar Threads

  1. Images + QTableview + Delegates
    By NoRulez in forum Qt Programming
    Replies: 4
    Last Post: 9th February 2009, 20:16
  2. Delegates and Signal/Slot
    By joshuajcarson in forum Qt Programming
    Replies: 2
    Last Post: 31st October 2008, 06:13
  3. Help plz! I’m stuck with these delegates
    By codeaddict in forum Qt Programming
    Replies: 7
    Last Post: 19th August 2008, 21:33
  4. Different delegates to different Columns of QTableView.
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 6th August 2008, 09:17
  5. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 19:47

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.