PDA

View Full Version : QTableView questions



space_otter
28th February 2010, 08:27
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?

waynew
28th February 2010, 15:10
Hey Otter - need to see some code - how are you defining the view and the header?

space_otter
28th February 2010, 19:07
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.


QVariant Stack::data ( const QModelIndex & index, int role ) const {
switch(index.column())
{
case 0:
return QVariant();
case 1:
return QVariant();
case 2:
return const_cast<DNumber*>(&at(index.row()))->toString();
}
return QVariant();
}

QModelIndex Stack::index ( int row, int column, const QModelIndex & parent) const {
return this->createIndex(row, column, parent.internalPointer());
}

QModelIndex Stack::parent ( const QModelIndex & index ) const {
return QModelIndex();
}

int Stack::columnCount ( const QModelIndex & parent ) const {
return 3;
}

int Stack::rowCount ( const QModelIndex & parent ) const {
return this->size();
}

QVariant Stack::headerData ( int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Horizontal)
{
switch(section)
{
case 0:
return "Roll";
case 1:
return "Drop";
case 2:
return "Value";
default:
return "???";
}
}
else
{
int size = this->size();
if(section == size - 1)
{
return "X";
}
else if(section == size - 2)
{
return "Y";
}
else
{
return size - section;
}
}
}

// 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
void Stack::setDecimalPlaces(int i) {
mDecimalPlaces = i;
emit dataChanged(index(0, 0), index(size()-1, 2));
}

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

space_otter
28th February 2010, 19:40
Arrg... new problem. Stack won't compile. I get this error:

'stack\Stack.h:0: Warning: No relevant classes found. No output generated.
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
mingw32-make[1]: Leaving directory `G:/Documents/Projects/Demurr2'
mingw32-make: Leaving directory `G:/Documents/Projects/Demurr2'
./release\Stack.o:Stack.cpp:(.text+0x2e0): undefined reference to `vtable for Stack'
./release\Stack.o:Stack.cpp:(.text+0x328): undefined reference to `vtable for Stack'
./release\Stack.o:Stack.cpp:(.text+0x36c): undefined reference to `vtable for Stack'
./release\Stack.o:Stack.cpp:(.text+0x3d4): undefined reference to `vtable for Stack'
./release\Stack.o:Stack.cpp:(.text+0x4bc): undefined reference to `vtable for Stack'
collect2: ld returned 1 exit status
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:

/*
* Stack.h
*
* Created on: Feb 27, 2010
* Author: Eric
*/

#include <QAbstractItemModel>
#include <QTableView>
#include "math/DNumber.h"
#include "stacktabwidget.h"

#ifndef STACK_H_
#define STACK_H_

class Stack : public QAbstractItemModel, public QList<DNumber> {
Q_OBJECT
public:
Stack(QTableView *stw, int dplaces, int accuracy);
~Stack();

// functions overridden from QAbstractItemModel
QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const;
QModelIndex parent ( const QModelIndex & index ) const;
int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;

private:
// internal settings
int mDecimalPlaces;
int mAccuracy;
QTableView *mTableView;

// get & set
public slots:
void setDecimalPlaces(int i);
void setAccuracy(int i);
};

#endif /* STACK_H_ */
I've implemented all the functions.

space_otter
28th February 2010, 19:44
Took out #include "stacktabwidget.h" and it works again. But my original question remains.

norobro
28th February 2010, 23:56
Try this:

QVariant Stack::headerData ( int section, Qt::Orientation orientation, int role) const
{

if(role != Qt::DisplayRole) return QVariant(); // only run your code for the display role

if(orientation == Qt::Horizontal)
{
. . . .
. . . .

space_otter
1st March 2010, 00:56
Sweet! That worked. :)

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

norobro
1st March 2010, 01:42
Try putting the same statement in data().