Results 1 to 8 of 8

Thread: QTableView questions

  1. #1
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView questions

    I'm using a QTableView and I supply headerData but the headers do not appear. The option is checked in ui editor and QHeaderView is defined. Why don't they appear?

    Also, why is there a white box next to some of my data items? Can I change its contents?

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView questions

    Hey Otter - need to see some code - how are you defining the view and the header?

  3. #3
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView questions

    The table's a special view for a 1D list of elements. Column #3 shows a string representation of the element, columns #1 & #2 are supposed to have buttons for deleting & moving the element to the end of the list. Stack inherits QAbstractItemModel and QList<DNumber>. DNumber has methods to parse to and from a QString.

    Qt Code:
    1. QVariant Stack::data ( const QModelIndex & index, int role ) const {
    2. switch(index.column())
    3. {
    4. case 0:
    5. return QVariant();
    6. case 1:
    7. return QVariant();
    8. case 2:
    9. return const_cast<DNumber*>(&at(index.row()))->toString();
    10. }
    11. return QVariant();
    12. }
    13.  
    14. QModelIndex Stack::index ( int row, int column, const QModelIndex & parent) const {
    15. return this->createIndex(row, column, parent.internalPointer());
    16. }
    17.  
    18. QModelIndex Stack::parent ( const QModelIndex & index ) const {
    19. return QModelIndex();
    20. }
    21.  
    22. int Stack::columnCount ( const QModelIndex & parent ) const {
    23. return 3;
    24. }
    25.  
    26. int Stack::rowCount ( const QModelIndex & parent ) const {
    27. return this->size();
    28. }
    29.  
    30. QVariant Stack::headerData ( int section, Qt::Orientation orientation, int role) const
    31. {
    32. if(orientation == Qt::Horizontal)
    33. {
    34. switch(section)
    35. {
    36. case 0:
    37. return "Roll";
    38. case 1:
    39. return "Drop";
    40. case 2:
    41. return "Value";
    42. default:
    43. return "???";
    44. }
    45. }
    46. else
    47. {
    48. int size = this->size();
    49. if(section == size - 1)
    50. {
    51. return "X";
    52. }
    53. else if(section == size - 2)
    54. {
    55. return "Y";
    56. }
    57. else
    58. {
    59. return size - section;
    60. }
    61. }
    62. }
    63.  
    64. // is a slot, the widget with the table also has a spinner connected to this to change the number of digits displayed as the value changes
    65. void Stack::setDecimalPlaces(int i) {
    66. mDecimalPlaces = i;
    67. emit dataChanged(index(0, 0), index(size()-1, 2));
    68. }
    To copy to clipboard, switch view to plain text mode 

    My implementation of QAbstractItemModel might not be the greatest Should I use QAbstractTableModel? That emit statement does nothing and I think it's becuase my index() is wrong.

  4. #4
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView questions

    Arrg... new problem. Stack won't compile. I get this error:
    Qt Code:
    1. 'stack\Stack.h:0: Warning: No relevant classes found. No output generated.
    2. g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -mthreads -Wl -Wl,-subsystem,windows -o release\Demurr.exe object_script.Demurr.Release -L"f:\Programs\Qt4.6\qt\lib" -lopengl32 -lglu32 -lgdi32 -luser32 -lmingw32 -lqtmain F:/Programs/mapm/libmapm.a -lQtOpenGL4 -lQtGui4 -lQtCore4
    3. mingw32-make[1]: Leaving directory `G:/Documents/Projects/Demurr2'
    4. mingw32-make: Leaving directory `G:/Documents/Projects/Demurr2'
    5. ./release\Stack.o:Stack.cpp:(.text+0x2e0): undefined reference to `vtable for Stack'
    6. ./release\Stack.o:Stack.cpp:(.text+0x328): undefined reference to `vtable for Stack'
    7. ./release\Stack.o:Stack.cpp:(.text+0x36c): undefined reference to `vtable for Stack'
    8. ./release\Stack.o:Stack.cpp:(.text+0x3d4): undefined reference to `vtable for Stack'
    9. ./release\Stack.o:Stack.cpp:(.text+0x4bc): undefined reference to `vtable for Stack'
    10. collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 
    I'm sure MOC is being invoked, it's compiliing moc_Stack.cpp. What is going on? This error has popped up before randomly and I fixed it by adding the file to the pro file, and it's definitely in the pro file. Here is Stack.h:
    Qt Code:
    1. /*
    2.  * Stack.h
    3.  *
    4.  * Created on: Feb 27, 2010
    5.  * Author: Eric
    6.  */
    7.  
    8. #include <QAbstractItemModel>
    9. #include <QTableView>
    10. #include "math/DNumber.h"
    11. #include "stacktabwidget.h"
    12.  
    13. #ifndef STACK_H_
    14. #define STACK_H_
    15.  
    16. class Stack : public QAbstractItemModel, public QList<DNumber> {
    17. Q_OBJECT
    18. public:
    19. Stack(QTableView *stw, int dplaces, int accuracy);
    20. ~Stack();
    21.  
    22. // functions overridden from QAbstractItemModel
    23. QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
    24. QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const;
    25. QModelIndex parent ( const QModelIndex & index ) const;
    26. int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
    27. int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
    28. QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
    29.  
    30. private:
    31. // internal settings
    32. int mDecimalPlaces;
    33. int mAccuracy;
    34. QTableView *mTableView;
    35.  
    36. // get & set
    37. public slots:
    38. void setDecimalPlaces(int i);
    39. void setAccuracy(int i);
    40. };
    41.  
    42. #endif /* STACK_H_ */
    To copy to clipboard, switch view to plain text mode 
    I've implemented all the functions.

  5. #5
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView questions

    Took out #include "stacktabwidget.h" and it works again. But my original question remains.

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTableView questions

    Try this:
    Qt Code:
    1. QVariant Stack::headerData ( int section, Qt::Orientation orientation, int role) const
    2. {
    3.  
    4. if(role != Qt::DisplayRole) return QVariant(); // only run your code for the display role
    5.  
    6. if(orientation == Qt::Horizontal)
    7. {
    8. . . . .
    9. . . . .
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to norobro for this useful post:

    space_otter (28th February 2010)

  8. #7
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView questions

    Sweet! That worked.

    The white boxes are still there. What are they for?

  9. #8
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTableView questions

    Try putting the same statement in data().

Similar Threads

  1. New to QT and have a few questions.
    By vbman213 in forum Newbie
    Replies: 3
    Last Post: 1st February 2010, 06:50
  2. Some QTableView questions
    By OlegK in forum Qt Programming
    Replies: 0
    Last Post: 30th December 2009, 16:08
  3. Replies: 2
    Last Post: 26th November 2009, 04:45
  4. MVC Questions
    By ComaWhite in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2009, 05:31
  5. QTableView questions
    By db in forum Newbie
    Replies: 2
    Last Post: 13th September 2007, 16:18

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.