1

I had to make my own classes for table view (to implement drag) for model (now it uses a list of MyTableItem) and header (to handle clicking column)

View:

class DragTableView : public QTableView
{
public:
    DragTableView(QWidget *parent = 0);
    void Initialzie(Overseer *Overseer);

protected:
    void mouseMoveEvent(QMouseEvent *event);

private:
    void performDrag();
};

Header:

class ClickableHeaderView : public QHeaderView
{
Q_OBJECT
public:
      ClickableHeaderView(ObjectMaster* Master);
      ~ClickableHeaderView(){};


public slots:
      void sectionClicked(int index);
};

Model:

class CustomModelOfCustomObjects : public QAbstractTableModel
{
    Q_OBJECT
public:
    CustomModelOfCustomObjects();

    //(...) 
    ///SOME FUNCTIONS LIKE ADDING ITEM, REMOVING IT, ETC

    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;

    bool insertRows(int position, int rows, const QModelIndex &index);

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

private:
    QList <MyTableItem> listOfObjects;
};

MyTableItem is just a class containing a few QStrings and ints representing data.

I handle mouse click ok, passed info further to my Overseer class, that stores models (and gives pointer to it to table view) and managed to sort it with this kKeep in mind, this function is Model's functions) :

//Example for sorting by "Name"
void sortItemsBy_Name()
{
    qSort(listOfObjects.begin(), listOfObjects.end(), compare_Names);
}

bool compare_Names(MyTableItem &v1, MyTableItem &v2)
{
    int result = QString::localeAwareCompare(v1.get_Name() , v2.get_Name());
    if(result >0)
    return true;
    else
    return false;
}

It was all good up until this point. List is sorted correctly, but what is displayed - isn't. I tried to use:

emit dataChanged(QModelIndex(), QModelIndex());

It refreshed TableView's display, but every element was at wrong position, while in list - everything was fine (I checked it in debug).

EDIT 1: Solutions presented in Sorting Qt Table Model - QTableView doesn't get updated didn't do the trick.

EDIT 2: I noticed that my table view will look exactly the same - if I use layoutAboutToBeChanged / dataChanged / NOTHING.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Arker
  • 169
  • 2
  • 14
  • I think you must emit QAbstractItemModel::layoutAboutToBeChanged() and QAbstractItemModel::layoutChanged() instead of dataChanged() – Fabio Mar 30 '16 at 09:26
  • I also tried that - same result. EDIT: according to this page ( http://doc.qt.io/qt-4.8/qabstractitemmodel.html ) "For example, dataChanged() is emitted whenever items of data made available by the model are changed. Changes to the headers supplied by the model cause headerDataChanged() to be emitted. If the structure of the underlying data changes, the model can emit layoutChanged() to indicate to any attached views that they should redisplay any items shown, taking the new structure into account." – Arker Mar 30 '16 at 09:31
  • Possible duplicate of [Sorting Qt Table Model - QTableView doesn't get updated](http://stackoverflow.com/questions/20471456/sorting-qt-table-model-qtableview-doesnt-get-updated) – Fabio Mar 30 '16 at 09:31
  • I would agree about dupliacte, but I tried that topic first and it didn't help. – Arker Mar 30 '16 at 09:40
  • You say that "every element was at wrong position". You mean that their position changed when you updated the table? Can you add the code for this update? – Moreira Mar 30 '16 at 10:32
  • sortItemsBy_Name() containswhole code - even without tose "emit" inside - result is the same and tose elements seems to be placed randomly. – Arker Mar 30 '16 at 10:35
  • Where are you calling `sortItemsBy_Name()`? You need to call `emit dataChanged(QModelIndex(), QModelIndex());` right after it. Debug the code and make sure that `dataChanged()` is called *after* `sortItemsBy_Name()`. – Donotalo Mar 30 '16 at 11:15
  • I got emit dataChanged(QModelIndex(), QModelIndex()); as last line in sortItemsBy_Name(), but it didnt change anything and i can't call it outside of it - sortItemsBy_Name() is called outside of this class – Arker Mar 30 '16 at 11:23

1 Answers1

0

I solved it. Turned out that forceing repaint() works, but wasn't best solution either. I searched to "previous" functions and I noticed there was break; missing in one case of switch .

Arker
  • 169
  • 2
  • 14