I got it wrong, can't compile
I tried following the MVF video but wasnt able to get what I wrote down to compile:
main.cpp
Code:
#include <QtGui/QApplication>
#include "delegate.h"
int main(int argc, char *argv[])
{
//Widget w;
//w.show();
ListDelegate *delegate;
view.setItemDelegate(delegate);
view.show();
return a.exec();
}
delegate.h
Code:
#ifndef DELEGATE_H
#define DELEGATE_H
#include <QtGui>
{
public:
};
{
}
void ListDelegate
::paint(QPainter *painter,
{
if(option.
state & QStyle::State_Selected) background = Qt::darkRed;
else
background = Qt::lightGray;
painter->drawText(option.rect, Qt::AlignCenter,
index.model()->data(index).toString());
}
#endif // DELEGATE_H
Re: I got it wrong, can't compile
What is the error you get? By the way, this code won't work as you are using an uninitialized variable in main().
Re: I got it wrong, can't compile
What "uninitialized variable in main()"? The program just stops working, crashes.
Re: I got it wrong, can't compile
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.
Re: I got it wrong, can't compile
Sorry about it, terrible mistake. I confused myself.:o
About the uninitialized variable, this is how the ICS video wrote the code:
Code:
ListDelegate delegate;
view.setItemDelegate(&delegate);
view.show();
Which does not compile and outputs:
Code:
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:
Re: I got it wrong, can't compile
You didn't implement the constructor for the ListDelegate class.
Re: I got it wrong, can't compile
How do I implement it? I thought that ListDelegate(QObject *parent=0);was the constructor for the ListDelegate class.
Re: I got it wrong, can't compile
Quote:
Originally Posted by
been_1990
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?
Re: I got it wrong, can't compile
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:
And now when I try to compile I get this other error:
Code:
F:/Documents/QT Projects/QListView_test/main.cpp:10: error: no matching function for call to 'QListView::setItemDelegate(ListDelegate&)'
Im trying to understand, doing my best. Thanks for your help.:D
Re: I got it wrong, can't compile
Re: I got it wrong, can't compile
For some unknown reason it compiled and now runs perfectly. With main :
Code:
ListDelegate delegate;
view.setItemDelegate(&delegate);
view.show();
And ListDelegate constructor:
Code:
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.
Re: I got it wrong, can't compile
The unknown reason is that you added the body of the constructor.
Re: I got it wrong, can't compile
Ok. Thanks for your help. :D