views:

333

answers:

3

Hi..

I want to make a customized list view, in that I will add items dynamically, I have written code for the same, but it's giving me some problems,

  1. code is compiling fine but it will give some linker error,
  2. I tried the same code by keeping different header files, it runs well, but i am not getting control to my delegate class only.

Please can you suggest what the mistake(s) might be?

#include <QtGui>
#include <QApplication>
#include <QtGui/QMainWindow>


class ItemDeligate:public QStyledItemDelegate
    {
public:
    ItemDeligate(QObject *parent = 0):QStyledItemDelegate(parent)
    {}
    ~ItemDeligate(){}

public:
    enum ItemDataRole { SubTextRole = Qt::UserRole + 100};

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

    };

QSize ItemDeligate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //here i know implimentation

}

void ItemDeligate::paint(QPainter *p, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //here i know the implimentation
    QStyledItemDelegate::paint(p,option,index);
}


class Test : public QMainWindow
{
    Q_OBJECT

public:
    Test(QWidget *parent = 0);
    ~Test(){};

    QPixmap GreenPixmap;
    QList<QStandardItem *> ItemList;
    QStandardItemModel *ListModel;    
    QVBoxLayout Listlayout;    
    QListView *MylistView;    
    QAction *AddItemMenu;

public slots:
    void addItem();
};


Test::Test(QWidget *parent)
    : QMainWindow(parent)
{
         MylistView = new QListView();
             QDesktopWidget* desktopWidget = QApplication::desktop();
         QRect clientRect = desktopWidget->geometry();

         MylistView->setMinimumSize(QSize(clientRect.width()-7,clientRect.height()-1));

         MylistView->setViewMode(QListView::ListMode);
         MylistView->setMovement(QListView::Free);
         MylistView->setItemDelegate(new ItemDeligate(MylistView));
         MylistView->setSelectionMode(QAbstractItemView::SingleSelection);
         bool val =GreenPixmap.load(":/new/prefix1/temp/test.png");

         ListModel = new QStandardItemModel();
         ListModel->appendColumn(ItemList);    
         MylistView->setModel(ListModel); 
             Listlayout.addWidget(MylistView);
          Listlayout.addWidget(MylistView);  
          this->setLayout(&Listlayout);
          AddItemMenu = new QAction("Add",this);      
         menuBar()->addAction(AddItemMenu); 
         val = connect(AddItemMenu,SIGNAL(triggered()),this,SLOT(addItem()));

}

void Test::addItem()
    {
            QStandardItem *Items = new QStandardItem(QIcon(GreenPixmap),"Avatar");
        Items->setData("WAKE UP",ItemDeligate::SubTextRole);
        ItemList.append(Items);
        ListModel->appendColumn(ItemList);

    }

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Test test;
    test.showMaximized();
    test.showMaximized();

    return a.exec();
}

Errors caused tool to abort.

mwldsym2.exe: Undefined symbol: 'void * Test::qt_metacast(char const *) (?qt_metacast@Test@@UAEPAXPBD@Z)'
mwldsym2.exe: referenced from 'const Test::`vftable' (??_7Test@@6B@~)' in main.o
mwldsym2.exe: Undefined symbol: 'struct QMetaObject const * Test::metaObject(void) const (?metaObject@Test@@UBEPBUQMetaObject@@XZ)'
mwldsym2.exe: referenced from 'const Test::`vftable' (??_7Test@@6B@~)' in main.o
+3  A: 

Judging from the error messages I would have to guess that you forgot to run moc.

Idan K
Hey.. i am new to QT,can you please give me the exact solution that how can i solve the same.i am not getting only :(
Shadow
+2  A: 

Make sure the header where the Test class is, is added to the HEADERS variable in the project file. Like this:

HEADERS += Test.h

Rerun qmake and make.

Godspeed

eburger
ya,, i tested.. by creating header file and CPPit worked well.. but the problem i am facing is adding item to list.control is not going to delegate methods only.. i hv mentioned adding procedure in above code
Shadow
I'm sorry I don't quite undestand what you're saying. Since your question number 1 is already solved (move the Test class declaration into a separate header file and add that to your .pro file, rerun qmake) I think you should write a new question about whatever isn't working (I don't know what you mean by "control is not going to delegate methods only"). Most people (including me) will just look at your linker error and respond to that.
eburger
A: 

You didn't specify what platform you're developing on.

If you're using Linux or Mac, are you generating your Makefile using qmake? If not, you're probably missing some important Qt-specific steps. For instance, all Qt-derived classes need to be run through the meta-object compiler (moc.) This provides the class with the specific functionality that Qt provides that isn't a part of C++ (such as slots, signals, etc,) if I recall correctly.

Make sure you have a Qt project (.pro) file. From this, you can generate a GNU make file using QMake.

Typically, you'll want to run:

qmake make

to build your application whenever you add new source files. Otherwise you can just run "make."

Tom