0

My QTableView doesn't show strings from a QStringList.

In QTableWidget I have QTableWidgetItems. Must I set the strings manually or will the view show them automatically? In all the tutorials I don't see a "->setItem", they appear automatically.

I have 2 QLineEdits that give the QStrings to my Model :

void View::pushButtonClicked() {
  meinModel->setData(txtname->text(), txtvalue->text());
}

In setData I push the Strings in two QLists.

names.push_back(name);
values.push_back(value);

I emit a dataChanged signal with the index from topleft and bottomright.

QModelIndex topLeft = createIndex(names.size()+1,0);
QModelIndex bottomRights = createIndex(names.size()-1,1);
emit dataChanged(topLeft, bottomRights);

I have a QAbstractTableModel and so i override the columnCount, rowCount and data Method. In my data() Method I return my value and name:

QString returnValue;
if(0 == index.column()) { returnValue = names.at(index.row()); }

All of this compiles without warnings, but doesn't work correctly :( Is there something I'm doing obviously wrong?

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
thelittlePanda
  • 101
  • 2
  • 10
  • You don't show enough code to tell what's wrong. For example, the `setData` in your model doens't have the signature of `QAbstractItemModel::setData`. – Kuba hasn't forgotten Monica Jul 25 '14 at 10:18
  • possible duplicate of [Qt 5.2 Model-View-Pattern: How to inform model object about changes in underlying data structure](http://stackoverflow.com/questions/21478746/qt-5-2-model-view-pattern-how-to-inform-model-object-about-changes-in-underlyin) – Kuba hasn't forgotten Monica Jul 25 '14 at 10:20

1 Answers1

0

One obvious problem is that you didn't get the semantics of dataChanged correctly. dataChanged means that an existing item has changed its value. When you change the structure of the model by adding/removing rows or columns, you have to enclose the modification in beginXxx and endXxx calls - see this answer for details.

For example:

void MyModel::setData(const QString & name, const QString & value) {
  beginInsertRows(QModelIndex(), names.size(), names.size());
  names.push_back(name);
  values.push_back(value);
  endInsertRows();
}
Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Right ^^ i have forget to write that again in the setDat. I had it at some point deleted because the program with "begin.. and end.." crashes when you want to add something with the button :/ – thelittlePanda Jul 25 '14 at 10:36
  • 1
    @thelittlePanda You absolutely must post a complete, self-contained example that reproduces the problem. Create a new project with just "main.cpp" in it, when you get the problem to reproduce, copy-paste it into your question. That way you won't forget any of such important details, and you won't waste everyone's time (*yours* included). We will be going in circles for the remainder of the year otherwise. – Kuba hasn't forgotten Monica Jul 25 '14 at 10:40
  • Sorry. I thought that here was a forum where you can ask when you have a Problem. With people who are eager to help( and Sacrifice your time).. Then the problem was just that I forgot something to write in again. I'm just a beginner :( I check my PRG again.. – thelittlePanda Jul 25 '14 at 11:07
  • @thelittlePanda Sure you can ask, but for the question to be useful and **answerable**, it must not neglect the most basic information. If you fail to even correctly copy-paste the code, how can we answer anything? What I refer to by wasting time is pointing out mistakes that aren't really in the code that you're running, but in your sloppily written question. Quality questions lead to quality answers. – Kuba hasn't forgotten Monica Jul 25 '14 at 13:14