Yes. The function is definately being used when I click the headers. Here is the whole code of both classes. Just in case I'm doing something wrong elsewhere.
treewidget.h
#ifndef TREEWIDGET_H
#define TREEWIDGET_H
#include <QTreeWidget>
#include <QPoint>
class TreeWidgetItem;
Q_OBJECT
public:
TreeWidgetItem* currentItem() const;
TreeWidgetItem
* itemAt
(const QPoint & p
) const;
TreeWidgetItem* itemAt(int x, int y) const;
TreeWidgetItem* takeTopLevelItem(int index);
TreeWidgetItem* topLevelItem(int index) const;
};
#endif // TREEWIDGET_H
#ifndef TREEWIDGET_H
#define TREEWIDGET_H
#include <QTreeWidget>
#include <QPoint>
class TreeWidgetItem;
class TreeWidget : public QTreeWidget {
Q_OBJECT
public:
TreeWidget(QWidget* parent = 0);
TreeWidgetItem* currentItem() const;
TreeWidgetItem* itemAt(const QPoint & p) const;
TreeWidgetItem* itemAt(int x, int y) const;
TreeWidgetItem* takeTopLevelItem(int index);
TreeWidgetItem* topLevelItem(int index) const;
};
#endif // TREEWIDGET_H
To copy to clipboard, switch view to plain text mode
treewidget.cpp
#include "treewidget.h"
#include "treewidgetitem.h"
setColumnCount(4);
headerItem()->setText(0, tr("Category") );
headerItem()->setText(1, tr("Start") );
headerItem()->setText(2, tr("End") );
headerItem()->setText(3, tr("Note") );
setAlternatingRowColors(true);
}
TreeWidgetItem* TreeWidget::currentItem() const {
}
TreeWidgetItem
* TreeWidget
::itemAt(const QPoint & p
) const {}
TreeWidgetItem* TreeWidget::itemAt(int x, int y) const {
}
TreeWidgetItem* TreeWidget::takeTopLevelItem(int index) {
return (TreeWidgetItem
*)QTreeWidget::takeTopLevelItem(index
);
}
TreeWidgetItem* TreeWidget::topLevelItem(int index) const {
return (TreeWidgetItem
*)QTreeWidget::topLevelItem(index
);
}
#include "treewidget.h"
#include "treewidgetitem.h"
TreeWidget::TreeWidget(QWidget * parent) : QTreeWidget(parent) {
setColumnCount(4);
headerItem()->setText(0, tr("Category") );
headerItem()->setText(1, tr("Start") );
headerItem()->setText(2, tr("End") );
headerItem()->setText(3, tr("Note") );
setAlternatingRowColors(true);
}
TreeWidgetItem* TreeWidget::currentItem() const {
return (TreeWidgetItem*)QTreeWidget::currentItem();
}
TreeWidgetItem* TreeWidget::itemAt(const QPoint & p) const {
return (TreeWidgetItem*)QTreeWidget::itemAt(p);
}
TreeWidgetItem* TreeWidget::itemAt(int x, int y) const {
return (TreeWidgetItem*)QTreeWidget::itemAt(x, y);
}
TreeWidgetItem* TreeWidget::takeTopLevelItem(int index) {
return (TreeWidgetItem*)QTreeWidget::takeTopLevelItem(index);
}
TreeWidgetItem* TreeWidget::topLevelItem(int index) const {
return (TreeWidgetItem*)QTreeWidget::topLevelItem(index);
}
To copy to clipboard, switch view to plain text mode
treewidgetitem.h
#ifndef TREEWIDGETITEM_H
#define TREEWIDGETITEM_H
#include <QTreeWidgetItem>
#include <QDateTime>
class TreeWidget;
Q_OBJECT
public:
TreeWidgetItem(TreeWidget* parent, int type = UserType);
TreeWidgetItem* clone() const;
QVariant data
(int column,
int role
) const;
void setData
(int column,
int role,
const QVariant & value
);
private:
};
#endif // TREEWIDGETITEM_H
#ifndef TREEWIDGETITEM_H
#define TREEWIDGETITEM_H
#include <QTreeWidgetItem>
#include <QDateTime>
class TreeWidget;
class TreeWidgetItem : public QObject, public QTreeWidgetItem {
Q_OBJECT
public:
TreeWidgetItem(TreeWidget* parent, int type = UserType);
TreeWidgetItem* clone() const;
QVariant data(int column, int role) const;
void read(QDataStream & in);
void setData(int column, int role, const QVariant & value);
void write(QDataStream & out) const;
bool operator<(const QTreeWidgetItem & other) const;
private:
QString _category;
QDateTime _start;
QDateTime _end;
QString _note;
};
#endif // TREEWIDGETITEM_H
To copy to clipboard, switch view to plain text mode
treewidgetitem.cpp
#include "treewidgetitem.h"
#include "treewidget.h"
} // intentionally empty
TreeWidgetItem* TreeWidgetItem::clone() const {
return NULL; // FIXME: Create this function
}
QVariant TreeWidgetItem
::data(int column,
int role
) const { if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::ToolTipRole) {
if (column == 0)
if (column == 1)
if (column == 2)
if (column == 3)
}
if (role == Qt::UserRole) {
if (column == 0)
if (column == 1)
if (column == 2)
if (column == 3)
}
}
// FIXME: Create this function
}
void TreeWidgetItem
::setData(int column,
int role,
const QVariant & value
) { if (role == Qt::EditRole) {
if (column == 0)
_category = value.toString();
if (column == 1)
_start
= (QDateTime::fromString(value.
toString()).
isValid() ?
QDateTime::fromString(value.
toString()) : _start
);
if (column == 2)
_end
= (QDateTime::fromString(value.
toString()).
isValid() ?
QDateTime::fromString(value.
toString()) : _end
);
if (column == 3)
_note = value.toString();
}
if (role == Qt::UserRole) {
if (column == 0)
_category = value.toString();
if (column == 1)
_start = value.toDateTime();
if (column == 2)
_end = value.toDateTime();
if (column == 3)
_note = value.toString();
}
}
void TreeWidgetItem
::write(QDataStream & /*out*/) const { // FIXME: Create this function
}
#include <iostream>
using namespace std;
cerr << "TEST" << endl;
switch (treeWidget()->sortColumn()) {
case 0:
return _category < ((const TreeWidgetItem &)other)._category;
case 1:
return _start < ((const TreeWidgetItem &)other)._start;
case 2:
return _end < ((const TreeWidgetItem &)other)._end;
case 3:
return _note < ((const TreeWidgetItem &)other)._note;
}
return false;
}
#include "treewidgetitem.h"
#include "treewidget.h"
TreeWidgetItem::TreeWidgetItem(TreeWidget * parent, int type) : QTreeWidgetItem((QTreeWidget*)parent, type) {
} // intentionally empty
TreeWidgetItem* TreeWidgetItem::clone() const {
return NULL; // FIXME: Create this function
}
QVariant TreeWidgetItem::data(int column, int role) const {
if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::ToolTipRole) {
if (column == 0)
return QVariant(_category);
if (column == 1)
return QVariant(_start.toString());
if (column == 2)
return (_end.isValid() ? QVariant(_end.toString()) : QVariant(QString("<running>")));
if (column == 3)
return QVariant(_note);
}
if (role == Qt::UserRole) {
if (column == 0)
return QVariant(_category);
if (column == 1)
return QVariant(_start);
if (column == 2)
return QVariant(_end);
if (column == 3)
return QVariant(_note);
}
return QVariant();
}
void TreeWidgetItem::read(QDataStream & /*in*/) {
// FIXME: Create this function
}
void TreeWidgetItem::setData(int column, int role, const QVariant & value) {
if (role == Qt::EditRole) {
if (column == 0)
_category = value.toString();
if (column == 1)
_start = (QDateTime::fromString(value.toString()).isValid() ? QDateTime::fromString(value.toString()) : _start);
if (column == 2)
_end = (QDateTime::fromString(value.toString()).isValid() ? QDateTime::fromString(value.toString()) : _end);
if (column == 3)
_note = value.toString();
}
if (role == Qt::UserRole) {
if (column == 0)
_category = value.toString();
if (column == 1)
_start = value.toDateTime();
if (column == 2)
_end = value.toDateTime();
if (column == 3)
_note = value.toString();
}
}
void TreeWidgetItem::write(QDataStream & /*out*/) const {
// FIXME: Create this function
}
#include <iostream>
using namespace std;
bool TreeWidgetItem::operator<(const QTreeWidgetItem & other) const {
cerr << "TEST" << endl;
switch (treeWidget()->sortColumn()) {
case 0:
return _category < ((const TreeWidgetItem &)other)._category;
case 1:
return _start < ((const TreeWidgetItem &)other)._start;
case 2:
return _end < ((const TreeWidgetItem &)other)._end;
case 3:
return _note < ((const TreeWidgetItem &)other)._note;
}
return false;
}
To copy to clipboard, switch view to plain text mode
I inherited QObject too, because I want to add signals and slots later.
So, what am I doing wrong?
Thanks!
Bookmarks