XML Data into QTableView: Some Problems: Empty Table, Messed up Header Data etc.
-
@SGaist The problem was, I was too dumb to see where to put the "-1". :(
Now I must find a way to read not only 1 but all "Stadt" Elements.
-
Follow the XBel example, it should give you what you need
-
@SGaist Thanks for the hint. I will take a look at it - tomorrow. Good night. And thanks for being continuously helpful and patient :) I am sure I will be back.
Oliver
-
Hello.
I decided to try my Luck for one more time without the XBEL Example. The following Code (Snippet) puts the Data from the XML File in the QTableView:
if(xmlReader.name() == "Stadt") { QString setItemTestStadt; setItemTestStadt = xmlReader.readElementText(); int row = tableViewMainViewIndex.row(); int column = tableViewMainViewIndex.column(); row = 0; column = 0; QStandardItem *item = new QStandardItem(QString(setItemTestStadt)); modelForTableViewMainView->insertRow(xmlReader.readElementText().count()); modelForTableViewMainView->setData(modelForTableViewMainView->index(row,column,tableViewMainViewIndex), xmlReader.readElementText()); modelForTableViewMainView->setItem(row, column, item); }
Cant believe that I have been so close for 2 weeks. //Facepalm
<s>Next "Problem" that I have: The Header Labels are correctly resized to fit their Content. But the Items Text is not. Already looked through the Web and through the Forum. Do I really have to subClass $Something?
Currently I try the following Settings:
tableViewMainView->setVisible(false); QRect originalViewport = tableViewMainView->viewport()->geometry(); QRect newViewport = originalViewport; newViewport.setWidth(std::numeric_limits<int>::max()); tableViewMainView->viewport()->setGeometry(newViewport); tableViewMainView->resizeRowsToContents(); tableViewMainView->resizeColumnsToContents(); tableViewMainView->viewport()->setGeometry(originalViewport); tableViewMainView->horizontalHeader()->resizeSections(QHeaderView::ResizeToContents); tableViewMainView->verticalHeader()->resizeSections(QHeaderView::ResizeToContents); //And after reading the Data into the TableView tableViewMainView->setVisible(true); ```</s> EDIT: Never mind. I forgot that that I must call resetColumnsToContents each time I update the TableView. Thanks again :) Oliver
-
Morning Folks.
A new Question:
I have different Views where the different Data from the XML File get split. 1 thing I will try is:
When- Row 0 in tableViewMainView is active / selected, then select
- all Items in Row 0 in tableViewAdditionalInfo
I tried different things but no luck yet.
//Not sure which Slot to use. Or if any of those SLOTs are even correct. connect(tableViewMainView,SIGNAL(pressed(QModelIndex)),this,SLOT(selectCurrentRowDependingOnTableViewMainView())); //What I try right now: tableViewMainView->setSelectionBehavior(QAbstractItemView::SelectRows); tableViewAdditionalInfo->setSelectionBehavior(QAbstractItemView::SelectRows); int selectedRow; selectedRow = tableViewMainView->rowAt(tableViewMainViewIndex.row()); tableViewMainView->rowAt(tableViewMainViewIndex.row()); tableViewAdditionalInfo->selectRow(selectedRow);
Thanks
-
And another Problem.
I want to display the Text of the XML Element "Name" in 2 different Views. But the Rows 0 + Column + of the 2nd View stay empty:if(xmlReader.name() == "Name") { //Set the Name in first View: QString setItemName; setItemName = xmlReader.readElementText(); int row1 = tableViewMainViewIndex.row(); int column1 = tableViewMainViewIndex.column(); row1 = 0; column1 = 3; QStandardItem *item = new QStandardItem(QString(setItemName)); modelForTableViewMainView->insertRows(modelForTableViewMainView->rowCount(), xmlReader.readElementText().count(), tableViewMainViewIndex); modelForTableViewMainView->setData(modelForTableViewMainView->index(row1,column1,tableViewMainViewIndex), xmlReader.readElementText()); modelForTableViewMainView->setItem(row1, column1, item); if(xmlReader.readElementText().isEmpty()) { xmlReader.readNext(); } //Set the Name in the 2nd View: QString setItemName2; setItemName2 = xmlReader.readElementText(); int row2 = tableViewAdditionalInfoIndex.row(); int column2 = tableViewAdditionalInfoIndex.column(); row2 = 0; column2 = 0; QStandardItem *item2 = new QStandardItem(QString(setItemName2)); modelForTableViewAdditionalInfo->insertRow(xmlReader.readElementText().count(), tableViewAdditionalInfoIndex); modelForTableViewAdditionalInfo->setData(modelForTableViewAdditionalInfo->index(row2,column2,tableViewAdditionalInfoIndex), xmlReader.readElementText()); modelForTableViewAdditionalInfo->setItem(row2, column2, item2); if(xmlReader.readElementText().isEmpty()) { xmlReader.readNext(); } }
I am not sure whats wrong. Even calling the "xmlReader.name() == "Name" at a later point , let the cells stay empty.
++++
EDIT: I finally found a workaround://Instead of: QStandardItem *item2 = new QStandardItem(QString(setItemName2)); //I do: QStandardItem *item2 = new QStandardItem(modelForTableViewMainView->item(0,3)->text());
But I still have the Problem mentioned 2 posts above: How can I select 2 Rows in different Views simultaneously?
Any hint is welcome :)
-
You can use the first view selectionChanged signal and then the second view selection model to do what you want.
-
Morning.
I connected the tableViewMainView:
connect(tableViewMainView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(selectCurrentRowDependingOnTableViewMainView()));
I try to scrollTo() by using the following Method:
void CDatabase::selectCurrentRowDependingOnTableViewMainView() { QPoint pos; int row=tableViewMainView->rowAt(pos.y()); int column=tableViewMainView->columnAt(pos.x()); if(modelForTableViewMainView->item(row,column)->isSelectable()) { QModelIndex selectedRow1Index=modelForTableViewMainView->index(row,column); tableViewAdditionalInfo->scrollTo(selectedRow1Index,QAbstractItemView::EnsureVisible); } }
I get no Error Messages but it still does not work as intended.
-
Should the items rows/cols from the second view be the same has the one from the main view ?
-
Evening.
What I want is:- If I select any Column in Row 0 in Table 1 = select Row 0 in Table 2
- If I select any Column in Row 5 in Table 1 = select Row 5 in Table 2
etc. etc.
-
In that case, use the QItemSelection parameters from the selectionChanged signal to retrieve these rows you want to select