Results 1 to 13 of 13

Thread: I got it wrong, can't compile

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "delegate.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. //Widget w;
    8. //w.show();
    9. ListDelegate *delegate;
    10. QListView view;
    11. view.setItemDelegate(delegate);
    12. view.show();
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    delegate.h
    Qt Code:
    1. #ifndef DELEGATE_H
    2. #define DELEGATE_H
    3. #include <QtGui>
    4.  
    5. class ListDelegate : public QAbstractItemDelegate
    6. {
    7. public:
    8. ListDelegate(QObject *parent=0);
    9.  
    10. void paint(QPainter *painter,
    11. const QStyleOptionViewItem &option,
    12. const QModelIndex &index) const;
    13.  
    14. QSize sizeHint(const QStyleOptionViewItem &option,
    15. const QModelIndex &index)const;
    16. };
    17.  
    18. QSize ListDelegate::sizeHint(const QStyleOptionViewItem &option,
    19. const QModelIndex &index) const
    20. {
    21. return QSize(100,40);
    22. }
    23.  
    24. void ListDelegate::paint(QPainter *painter,
    25. const QStyleOptionViewItem &option,
    26. const QModelIndex &index) const
    27. {
    28. QColor background;
    29.  
    30. if(option.state & QStyle::State_Selected)
    31. background = Qt::darkRed;
    32. else
    33. background = Qt::lightGray;
    34.  
    35. painter->setFont(QFont("Arial",12,QFont::Bold));
    36. painter->drawText(option.rect, Qt::AlignCenter,
    37. index.model()->data(index).toString());
    38. }
    39. #endif // DELEGATE_H
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to been_1990 for this useful post:


  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: 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().
    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.


  4. The following user says thank you to wysota for this useful post:


  5. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I got it wrong, can't compile

    What "uninitialized variable in main()"? The program just stops working, crashes.

  6. The following user says thank you to been_1990 for this useful post:


  7. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: 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.
    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.


  8. The following user says thank you to wysota for this useful post:


  9. #5
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I got it wrong, can't compile

    Sorry about it, terrible mistake. I confused myself.
    About the uninitialized variable, this is how the ICS video wrote the code:
    Qt Code:
    1. ListDelegate delegate;
    2. QListView view;
    3. view.setItemDelegate(&delegate);
    4. view.show();
    To copy to clipboard, switch view to plain text mode 

    Which does not compile and outputs:
    Qt Code:
    1. F:\Documents\QT Projects\QListView_test/main.cpp:8: undefined reference to `ListDelegate::ListDelegate(QObject*)'
    2. :-1: error: collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 

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

  10. The following user says thank you to been_1990 for this useful post:


  11. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: I got it wrong, can't compile

    You didn't implement the constructor for the ListDelegate class.
    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.


  12. The following user says thank you to wysota for this useful post:


  13. #7
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.

  14. The following user says thank you to been_1990 for this useful post:


  15. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: I got it wrong, can't compile

    Quote Originally Posted by been_1990 View Post
    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?
    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.


  16. The following user says thank you to wysota for this useful post:


  17. #9
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. {
    2.  
    3. }
    To copy to clipboard, switch view to plain text mode 

    And now when I try to compile I get this other error:
    Qt Code:
    1. F:/Documents/QT Projects/QListView_test/main.cpp:10: error: no matching function for call to 'QListView::setItemDelegate(ListDelegate&)'
    2. c:\Qt\2009.05\qt\include\QtGui/../../src/gui/itemviews/qabstractitemview.h:135: note: candidates are: void QAbstractItemView::setItemDelegate(QAbstractItemDelegate*)
    To copy to clipboard, switch view to plain text mode 

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

  18. The following user says thank you to been_1990 for this useful post:


  19. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: I got it wrong, can't compile

    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.


  20. The following user says thank you to wysota for this useful post:


  21. #11
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I got it wrong, can't compile

    For some unknown reason it compiled and now runs perfectly. With main :

    Qt Code:
    1. ListDelegate delegate;
    2.  
    3. QListView view;
    4.  
    5. view.setItemDelegate(&delegate);
    6.  
    7. view.show();
    To copy to clipboard, switch view to plain text mode 

    And ListDelegate constructor:

    Qt Code:
    1. ListDelegate::ListDelegate(QObject *parent = 0)
    2. {
    3.  
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

  22. The following user says thank you to been_1990 for this useful post:


  23. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: I got it wrong, can't compile

    The unknown reason is that you added the body of the constructor.
    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.


  24. The following user says thank you to wysota for this useful post:


  25. #13
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Smile Re: I got it wrong, can't compile

    Ok. Thanks for your help.

  26. The following 2 users say thank you to been_1990 for this useful post:


Similar Threads

  1. What's wrong??
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 25th June 2008, 09:07
  2. What's wrong with my actions???
    By fullmetalcoder in forum Qt Programming
    Replies: 6
    Last Post: 4th March 2007, 19:49
  3. What is wrong with this simple example ?
    By igor in forum Qt Programming
    Replies: 10
    Last Post: 16th January 2007, 12:26
  4. What's wrong with this connection??
    By SkripT in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2006, 20:44
  5. Help please - what am I doing wrong?
    By Jimmy2775 in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2006, 23:06

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.