0

How should this be done by using the model->setData() method call?

I have derived a class called "MyStandardItemModel" from QStandardItemModel. I have made my third and fourth columns non-editable by overriding the protected virtual flags method. This is how it goes:

#define TX_PACKET_COLUMN (4u)
#define RX_PACKET_COLUMN (5u)

Qt::ItemFlags MyStandardItemModel::flags(const QModelIndex& index) const
{
    if (index.column() == TX_PACKET_COLUMN || index.column() == RX_PACKET_COLUMN)
    {
        return (QStandardItemModel::flags(index) & ~Qt::ItemIsEditable);
    }
    else
    {
        return QStandardItemModel::flags(index);
    }
}

...

//Set model
ui->testCaseTableView->setModel(model);

Having this done, I am not able to edit the cells in the third and fourth column.

Now, I want that when I double click on these cells, a pop-up dialog comes up. I will modify some data in the editable field of that dialog, and then copy it back to the non editable cells inside the code.

I tried to just write a doubleclick() handler for the QTreeView and just copy some data to the cells just to see if it is possible to copy data to the non-editable cells.

This operation is failing, and the data is not written into the non-editable cells.

Here you can find the double click handler:

void MainWindow::on_testCaseTableView_doubleClicked(const QModelIndex &index)
{
    QVariant variant;
    variant.toString() = "AA";

    if((index.column() == TX_PACKET_COLUMN)||(index.column() == RX_PACKET_COLUMN))
    {
        model->setData(index, variant);   // set new value
    }

}

The setData(..) operation is clearing the already written data in the cells, but string "AA" is not getting written. Please suggest how to copy some data to non-editable cells inside the code.

enter image description here

László Papp
  • 51,870
  • 39
  • 111
  • 135
Katoch
  • 2,709
  • 9
  • 51
  • 84
  • Why do you not inherit QTIM instead of QAIM btw when it is about tables? – László Papp Sep 28 '13 at 09:37
  • Whether the cells are editable or not should not make a difference to how you can programatically set and display values. The answer probably lies in the models ::setData and ::data methods. I would debug those. – user2672165 Sep 28 '13 at 09:43
  • 1
    variant.toString() = "AA"; -> looks wrong, and the bug will be in your setData() implementation for the model subclass. Can you share that code? Do you emit the data changed signal properly there? – László Papp Sep 28 '13 at 09:44

2 Answers2

2

QVariant set is empty. Nothing needs to be wrong in your model. Error is on this line:

variant.toString() = "AA";

change to:

QVariant variant("AA"); // just for testing anyway
user2672165
  • 2,986
  • 19
  • 27
  • 1
    @Lazlo Papp : If you had read more carefully you had noted that it said in the question that "The setData(..) operation is clearing the already written data in the cells". My logic was not flawed. I acknowledge that you noted the faulty line first. I come to think of it when I was out in my garden. Should have acknowledged you yes. I downvoted your answer because it was very agressive to focus on character encoding in this case. It was because in my own view I realized all this on my own, but I understand if you thought I stole your idea. Still it seems that I understood it all this time. – user2672165 Sep 28 '13 at 16:31
1

As I indicated in my comment, you have to fix this first issue:

instead of:

QVariant variant;
variant.toString() = "AA";

you should write

QVariant variant = QLatin1String("AA");

In general, you would look into the setData(...) implementation for such cases whether you emit the data changed signal properly and so forth, but here you are entering a prior issue which can lead to problems, so let us fix that.

Note, you should use QLatin1String to avoid the unnecessary explicit conversion from raw char* to QString. This is a good practice in general, and this is available with Qt 4 as well as Qt 5.

Although, you could also use the QStringLiteral macro for creating a QString very efficiently with template magic out of your raw literal, but that requires C++11.

László Papp
  • 51,870
  • 39
  • 111
  • 135