PDA

View Full Version : Trying to use delegates...



foahchon
28th August 2009, 00:36
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:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QPainter>
#include <QString>

class ItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
inline ItemDelegate(QObject *mainWindow) : QItemDelegate(mainWindow) {}

inline void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const

{
painter->drawText(QPoint(option.Left + 20, option.Top), "Bloop!");
}
};

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->listWidget->setItemDelegate(new ItemDelegate(this));
ui->listWidget->addItem("Item 1");
ui->listWidget->addItem("Item 2");
ui->listWidget->addItem("Item 3");
}

MainWindow::~MainWindow()
{
delete ui;
}

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.

wysota
28th August 2009, 01:27
Does it work if you remove this line?

ui->listWidget->setItemDelegate(new ItemDelegate(this));

foahchon
28th August 2009, 02:26
Yes, I forgot to mention that the program will run if I remove that line.

wysota
28th August 2009, 02:31
Try this:

class ItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
ItemDelegate(QObject *parent = 0) : QItemDelegate(parent) {}

void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
painter->drawText(QPoint(option.rect.left() + 20, option.rect.top()), "Bloop!");
}
};

foahchon
28th August 2009, 02:36
Same result, unfortunately.

wysota
28th August 2009, 02:58
Can we see the backtrace from the debugger?

foahchon
28th August 2009, 04:29
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:


Running build steps for project listtest2...
Configuration unchanged, skipping QMake step.
Starting: C:/Qt/2009.03/mingw/bin/mingw32-make.exe -w
mingw32-make: Entering directory `C:/Documents and Settings/Administrator/My Documents/listtest2'
C:/Qt/2009.03/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Documents and Settings/Administrator/My Documents/listtest2'
c:\Qt\2009.03\qt\bin\uic.exe mainwindow.ui -o ui_mainwindow.h
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
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
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
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
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
debug/mainwindow.o: In function `ZThn8_N10MainWindowD1Ev':
C:/Documents and Settings/Administrator/My Documents/listtest2/mainwindow.cpp:(.text$_ZN12ItemDelegateC1EP7QObjec t[ItemDelegate::ItemDelegate(QObject*)]+0x1f): undefined reference to `vtable for ItemDelegate'
collect2: ld returned 1 exit status
mingw32-make[1]: Leaving directory `C:/Documents and Settings/Administrator/My Documents/listtest2'
mingw32-make: Leaving directory `C:/Documents and Settings/Administrator/My Documents/listtest2'
mingw32-make[1]: *** [debug\listtest2.exe] Error 1
mingw32-make: *** [debug] Error 2
Exited with code 2.
Error while building project listtest2
When executing build step 'Make'

Thanks for all your help thus far.

wysota
28th August 2009, 08:17
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$_ZN12ItemDelegateC1EP7QObjec t[ItemDelegate::ItemDelegate(QObject*)]+0x1f): undefined reference to `vtable for ItemDelegate'

Remove the Q_OBJECT macro or rerun qmake before compiling the project.