Results 1 to 3 of 3

Thread: Passing QVariant a pointer

  1. #1
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Passing QVariant a pointer

    I'm both a Qt newbie and a C++ newbie, so bear with me.

    I've created a QAbstractTableModel for use in a QTableView table.
    The basic data in the model for each row of the table is a list (QList) of structures that hold three things:
    1. A single letter name (type QChar)
    2. An integer
    3. A pointer to some data

    The first two are to be displayed in the table. The third is not to be displayed in the QTableView, but is simply a pointer to data that is linked to the row.

    I've implemented a "setData()" function for accessing the row data, as per an example I'm using for help, with the following parameters:
    Qt Code:
    1. bool QAbstractItemModel::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole )
    To copy to clipboard, switch view to plain text mode 

    When I pass the setData function the first two variable types (QChar or int) 'value', there is no problem using the data; I simply use the QVariant conversion functions to convert from QVariant to the correct types and then place the value in the appropriate table entry.
    But when I want to use setData to update the pointer, I'm not sure how to do it.... Can I pass QVariant a pointer?

    userTableModel.h
    Qt Code:
    1. #ifndef USERTABLEMODEL_H
    2. #define USERTABLEMODEL_H
    3.  
    4. #include <QAbstractTableModel>
    5. #include <QList>
    6.  
    7. //Structure to store individual user information
    8. struct userInfo
    9. {
    10. QChar userName;
    11. int userNumber;
    12. int *userData;
    13. };
    14.  
    15. class userTableModel : public QAbstractTableModel
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. userTableModel(QObject *parent=0);
    21. userTableModel(QList<userInfo> userInfoList, QObject *parent=0);
    22.  
    23. int rowCount(const QModelIndex &parent) const;
    24. int columnCount(const QModelIndex &parent) const;
    25. QVariant data(const QModelIndex &index, int role) const;
    26. QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    27. Qt::ItemFlags flags(const QModelIndex &index) const;
    28. bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole);
    29. bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex());
    30. bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex());
    31. QList<userInfo> getList();
    32.  
    33. private:
    34. QString systemName;
    35. int systemNumber;
    36.  
    37. QList<userInfo> listOfUserInfo; //List of user info
    38. };
    39.  
    40. #endif
    To copy to clipboard, switch view to plain text mode 

    setData() from userTableModel.cpp (note line 17)
    Qt Code:
    1. bool userTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
    2. {
    3. if (index.isValid() && role == Qt::EditRole) {
    4. int row = index.row();
    5.  
    6. //Select the correct row
    7. userInfo p = listOfUserInfo.value(row);
    8.  
    9. //User's Name (Initial)
    10. if(index.column() == 0)
    11. p.userName = value.toChar();
    12. //User's Number
    13. else if(index.column() == 1)
    14. p.userNumber = value.toInt();
    15. //Pointer to User's Data
    16. else if(index.column() == 2)
    17. //Not Sure what to put here!!!
    18.  
    19. else
    20. return false;
    21.  
    22. listOfUserInfo.replace(row, p);
    23. emit(dataChanged(index, index));
    24.  
    25. return true;
    26. }
    27.  
    28. return false;
    29. }
    To copy to clipboard, switch view to plain text mode 

    How would I use the same function (setData()) to update the "userInfo.userData" pointer?

  2. #2
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing QVariant a pointer

    Try this
    in setData():

    Qt Code:
    1. else if(index.column() == 2)
    2. *(p.userData) = value.toInt();
    To copy to clipboard, switch view to plain text mode 

    data() function should return *(p.userData) in case of second column

  3. #3
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing QVariant a pointer

    I have noticed line 7 in your code userInfo p = listOfUserInfo.value(row);
    QList<T>::value() function returns copy of structure that is referenced by list. More over operator=() will call default copy constructor
    Try
    Qt Code:
    1. userInfo &p = listOfUserInfo.value[row];
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 4th December 2009, 17:03
  2. QPixmap and QVariant
    By cafu in forum Qt Programming
    Replies: 16
    Last Post: 27th October 2009, 13:23
  3. VARIANT <-> QVariant
    By will49 in forum Qt Programming
    Replies: 2
    Last Post: 26th September 2009, 23:39
  4. QVariant to QAxObject*
    By QDrow in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2008, 09:35
  5. Passing a pointer in Signal/Slot Connection
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2007, 19:04

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.