PDA

View Full Version : I got it wrong, can't compile



been_1990
11th January 2010, 17:52
I tried following the MVF video but wasnt able to get what I wrote down to compile:

main.cpp

#include <QtGui/QApplication>
#include "delegate.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//Widget w;
//w.show();
ListDelegate *delegate;
QListView view;
view.setItemDelegate(delegate);
view.show();
return a.exec();
}

delegate.h

#ifndef DELEGATE_H
#define DELEGATE_H
#include <QtGui>

class ListDelegate : public QAbstractItemDelegate
{
public:
ListDelegate(QObject *parent=0);

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

QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index)const;
};

QSize ListDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
return QSize(100,40);
}

void ListDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QColor background;

if(option.state & QStyle::State_Selected)
background = Qt::darkRed;
else
background = Qt::lightGray;

painter->setFont(QFont("Arial",12,QFont::Bold));
painter->drawText(option.rect, Qt::AlignCenter,
index.model()->data(index).toString());
}
#endif // DELEGATE_H

wysota
11th January 2010, 18:25
What is the error you get? By the way, this code won't work as you are using an uninitialized variable in main().

been_1990
11th January 2010, 18:43
What "uninitialized variable in main()"? The program just stops working, crashes.

wysota
11th January 2010, 19:17
So why did you say you were unable to compile the application? Compilation error and crashing are two different things.

Your "delegate" variable is uninitialized. It's just a place in memory pointing to some garbage.

I suggest you polish your C++ skills a bit.

been_1990
12th January 2010, 01:47
Sorry about it, terrible mistake. I confused myself.:o
About the uninitialized variable, this is how the ICS video wrote the code:

ListDelegate delegate;
QListView view;
view.setItemDelegate(&delegate);
view.show();

Which does not compile and outputs:

F:\Documents\QT Projects\QListView_test/main.cpp:8: undefined reference to `ListDelegate::ListDelegate(QObject*)'
:-1: error: collect2: ld returned 1 exit status

And the ListDelegate class is written exactly as the video. But you are definitely right about my C++ skills...:crying:

wysota
12th January 2010, 04:30
You didn't implement the constructor for the ListDelegate class.

been_1990
12th January 2010, 23:23
How do I implement it? I thought that ListDelegate(QObject *parent=0);was the constructor for the ListDelegate class.

wysota
12th January 2010, 23:31
How do I implement it? I thought that ListDelegate(QObject *parent=0);was the constructor for the ListDelegate class.

Come on, man. It's a declaration of the constructor. Where is its body?

been_1990
13th January 2010, 03:43
Don't kill me for asking, but how do I implement it? The video didn't write anything else, the source code, QAbstractItemDelegate.cpp the body of the constructor is blank , it's just like that:

QAbstractItemDelegate::QAbstractItemDelegate(QObje ct *parent = 0)
{

}

And now when I try to compile I get this other error:


F:/Documents/QT Projects/QListView_test/main.cpp:10: error: no matching function for call to 'QListView::setItemDelegate(ListDelegate&)'
c:\Qt\2009.05\qt\include\QtGui/../../src/gui/itemviews/qabstractitemview.h:135: note: candidates are: void QAbstractItemView::setItemDelegate(QAbstractItemDe legate*)

Im trying to understand, doing my best. Thanks for your help.:D

wysota
13th January 2010, 12:24
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

been_1990
14th January 2010, 17:30
For some unknown reason it compiled and now runs perfectly. With main :



ListDelegate delegate;

QListView view;

view.setItemDelegate(&delegate);

view.show();

And ListDelegate constructor:


ListDelegate::ListDelegate(QObject *parent = 0)
{

}


I still am clueless as to why. I read the pixelator example on QT, and it's custom delegate class constructor is the same as mine, empty.
Now if I try to use a custom QAbstractListModel I get the same issues as I previously had with QAbstractItemDelegate.

wysota
14th January 2010, 18:21
The unknown reason is that you added the body of the constructor.

been_1990
16th January 2010, 00:00
Ok. Thanks for your help. :D