سؤال

I'm trying to improve this example for a diagram editor.

The example uses a nodes class with a few attributes unrelated to my needs. Right now I want to add a "list of arrays" to this node class in order to then populate a QTableView in the "properties" dialog. I already designed the properties dialog with the QTableView. I'm not even sure this is feasible/makes sense.

Basically the table must have 4 columns: name, type, value and unit.

Each row of the table is a certain "property" I need the node to have.

My question is: how can/should I model the table at class level? I ask this because I have been looking to QList, QVariant, QMap, QMultiMap and I can't figure out out to use them correctly, and none of the examples I found so far are any help either, at least for what I need to do. I saw something about the QStandardItemModel class, and I think it relates to the solution, but I can't understand how/why.

To top it off, I'm a Qt/C++ beginner, so much of the dynamics and jargon in Qt/C++ are still elluding me.

If anyone could give me some pointers, that would be great.

EDIT: This isn't getting much attention, and I don't know if its because I wasn't clear enough, but anyway, try to picture this:

  • a Node has a PropertyList
  • that PropertyList lists Properties
  • Properties are sort of dictionaries, as they have always 4 attributes and respective values: name:(string), type:(string), value:(double) and unit:(string).
  • one Node can only have one PropertyList; a PropertyList can have several Properties. Some Nodes will have 3 properties, others will have 4, etc...

Now, the Nodes are represented in a diagram via a QGraphicsScene with QGraphicsItem. You can access a PropertiesDialog widget that has a QTableWidget in it. This table will show the PropertyList.

I want to know what kind of "structure" I can create/define that enables me to easily insert/read data in the table widget - ie, I load the data into the PropertiesList of the Node and it shows up in the table widget; if I change the data in the table widget, it passes on to the PropertiesList of the Node.

Hope this helps clearing out any doubts that may arise.

هل كانت مفيدة؟

المحلول

Well, after a lot of hair pulling, I got what I needed.

My first step was to create a Property class. This class has functions to set or get a name string, a type string, a value double and a unit string.

Next, I updated the Node class to include functions to add and remove Property object pointers to a QList<Property *>. Also included was a listProperties function that returns all the Property objects from a certain Node.

After this, the function to populate the QTableWidget with a certain Node's properties was coded like this:

    propertiesList = node->listMyProperties();

    for (int row = 0; row < propertiesList.size(); ++row) {
        Property *property = propertiesList.at(row);
        addRow();
        tableWidget->item(row, 0)->setData(Qt::DisplayRole, property->propertyName());
        tableWidget->item(row, 1)->setData(Qt::DisplayRole, property->propertyType());
        tableWidget->item(row, 2)->setData(Qt::DisplayRole, property->propertyValue());
        tableWidget->item(row, 3)->setData(Qt::DisplayRole, property->propertyUnit());
    }

And the addRow() function:

void PropertiesDialog::addRow()
{
    int row = tableWidget->rowCount();

    tableWidget->insertRow(row);

    QTableWidgetItem *item0 = new QTableWidgetItem;
    item0->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    tableWidget->setItem(row, 0, item0);

    QTableWidgetItem *item1 = new QTableWidgetItem;
    item1->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    tableWidget->setItem(row, 1, item1);

    QTableWidgetItem *item2 = new QTableWidgetItem;
    item2->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    tableWidget->setItem(row, 2, item2);

    QTableWidgetItem *item3 = new QTableWidgetItem;
    item3->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    tableWidget->setItem(row, 3, item3);

    tableWidget->setCurrentItem(item0);
}

This produces what I needed: to have a class to hold the property values related to each node, and present them on a QTableWidget. Next step is to make the reverse path, meaning, when edits occur in the QTableWidget, those changes should propagate to the class. Now I think I can find my way, hope this helps anyone trying to find something related. I'll also update the tags and maybe edit the title to make it more relevant/meaningful.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top