Model View Tutorial Part3/de: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(added code-tags for removeColumn)
 
(3 intermediate revisions by the same user not shown)
Line 62: Line 62:
==addCd==
==addCd==


<div class="cpp-qt geshi">
<code>
 
void CdModel::addCd()
# <div class="de1"><span class="kw4">void</span> CdModel<span class="sy0">::</span><span class="me2">addCd</span><span class="br0">(</span><span class="br0">)</span></div>
{
# <div class="de1"><span class="br0">{</span></div>
    beginInsertRows(QModelIndex(), m_data.size(), m_data.size());
# <div class="de1">    beginInsertRows<span class="br0">(</span>[http://doc.qt.io/QModelIndex.html <span class="kw5">QModelIndex</span>]<span class="br0">(</span><span class="br0">)</span><span class="sy0">,</span> m_data.<span class="me1">size</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">,</span> m_data.<span class="me1">size</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div>
    CdDisk newDisk;
# <div class="de1">    CdDisk newDisk<span class="sy0">;</span></div>
    newDisk.m_author = tr("<unknown>");
# <div class="de2">    newDisk.<span class="me1">m_author</span> <span class="sy0">=</span> tr<span class="br0">(</span><span class="st0">"<unknown>"</span><span class="br0">)</span><span class="sy0">;</span></div>
    newDisk.m_genre = tr("<unknown>");
# <div class="de1">    newDisk.<span class="me1">m_genre</span> <span class="sy0">=</span> tr<span class="br0">(</span><span class="st0">"<unknown>"</span><span class="br0">)</span><span class="sy0">;</span></div>
    newDisk.m_title = tr("<unknown>");
# <div class="de1">    newDisk.<span class="me1">m_title</span> <span class="sy0">=</span> tr<span class="br0">(</span><span class="st0">"<unknown>"</span><span class="br0">)</span><span class="sy0">;</span></div>
    newDisk.m_year = 0;
# <div class="de1">    newDisk.<span class="me1">m_year</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span></div>
    m_data.addCD(newDisk);
# <div class="de1">    m_data.<span class="me1">addCD</span><span class="br0">(</span>newDisk<span class="br0">)</span><span class="sy0">;</span></div>
    endInsertRows();
# <div class="de2">    endInsertRows<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
}
# <div class="de1"><span class="br0">}</span></div>
</code>
 
</div>


==removeCd==
==removeCd==
 
<code>
<div class="cpp-qt geshi">
void CdModel::removeCd(const QModelIndex& index)
 
{
# <div class="de1"><span class="kw4">void</span> CdModel<span class="sy0">::</span><span class="me2">removeCd</span><span class="br0">(</span><span class="kw4">const</span> [http://doc.qt.io/QModelIndex.html <span class="kw5">QModelIndex</span>]<span class="sy0">&</span> index<span class="br0">)</span></div>
if(index.isValid() && (0 <= index.row()) && (index.row() < rowCount()))
# <div class="de1"><span class="br0">{</span></div>
{
# <div class="de1">    <span class="kw1">if</span><span class="br0">(</span>index.<span class="me1">isValid</span><span class="br0">(</span><span class="br0">)</span> <span class="sy0">&&</span> <span class="br0">(</span><span class="nu0">0</span> <span class="sy0"><=</span> index.<span class="me1">row</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="sy0">&&</span> <span class="br0">(</span>index.<span class="me1">row</span><span class="br0">(</span><span class="br0">)</span> <span class="sy0"><</span> rowCount<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="br0">)</span></div>
beginRemoveRows(QModelIndex(), index.row(), index.row());
# <div class="de1">    <span class="br0">{</span></div>
m_data.removeCD(index.row());
# <div class="de2">        beginRemoveRows<span class="br0">(</span>[http://doc.qt.io/QModelIndex.html <span class="kw5">QModelIndex</span>]<span class="br0">(</span><span class="br0">)</span><span class="sy0">,</span> index.<span class="me1">row</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">,</span> index.<span class="me1">row</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div>
endRemoveRows();
# <div class="de1">        m_data.<span class="me1">removeCD</span><span class="br0">(</span>index.<span class="me1">row</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div>
}
# <div class="de1">        endRemoveRows<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
}
# <div class="de1">    <span class="br0">}</span></div>
</code>
# <div class="de1"><span class="br0">}</span></div>
 
</div>


==addColumn==
==addColumn==
 
<code>
<div class="cpp-qt geshi">
bool CdModel::addColumn(const QString& rszColumnName)
 
{
# <div class="de1"><span class="kw4">bool</span> CdModel<span class="sy0">::</span><span class="me2">addColumn</span><span class="br0">(</span><span class="kw4">const</span> [http://doc.qt.io/QString.html <span class="kw5">QString</span>]<span class="sy0">&</span> rszColumnName<span class="br0">)</span></div>
if(!m_data.columnExists(rszColumnName))
# <div class="de1"><span class="br0">{</span></div>
{
# <div class="de1">    <span class="kw1">if</span><span class="br0">(</span><span class="sy0">!</span>m_data.<span class="me1">columnExists</span><span class="br0">(</span>rszColumnName<span class="br0">)</span><span class="br0">)</span></div>
beginInsertColumns(QModelIndex(), columnCount(), columnCount());
# <div class="de1">    <span class="br0">{</span></div>
m_data.addColumn(rszColumnName);
# <div class="de2">        beginInsertColumns<span class="br0">(</span>[http://doc.qt.io/QModelIndex.html <span class="kw5">QModelIndex</span>]<span class="br0">(</span><span class="br0">)</span><span class="sy0">,</span> columnCount<span class="br0">(</span><span class="br0">)</span><span class="sy0">,</span> columnCount<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div>
endInsertColumns();
# <div class="de1">        m_data.<span class="me1">addColumn</span><span class="br0">(</span>rszColumnName<span class="br0">)</span><span class="sy0">;</span></div>
return true;
# <div class="de1">        endInsertColumns<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
}
# <div class="de1">        <span class="kw1">return</span> true<span class="sy0">;</span></div>
return false;
# <div class="de1">    <span class="br0">}</span></div>
}
# <div class="de2">    <span class="kw1">return</span> false<span class="sy0">;</span></div>
</code>
# <div class="de1"><span class="br0">}</span></div>
 
</div>


==removeColumn==
==removeColumn==
 
<code>
<div class="cpp-qt geshi">
void CdModel::removeColumn(int nColumn)
 
{
# <div class="de1"><span class="kw4">void</span> CdModel<span class="sy0">::</span><span class="me2">removeColumn</span><span class="br0">(</span><span class="kw4">int</span> nColumn<span class="br0">)</span></div>
if((m_columns.count() <= nColumn) && (nColumn < columnCount()))
# <div class="de1"><span class="br0">{</span></div>
{
# <div class="de1">    <span class="kw1">if</span><span class="br0">(</span><span class="br0">(</span>m_columns.<span class="me1">count</span><span class="br0">(</span><span class="br0">)</span> <span class="sy0"><=</span> nColumn<span class="br0">)</span> <span class="sy0">&&</span> <span class="br0">(</span>nColumn <span class="sy0"><</span> columnCount<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="br0">)</span></div>
beginRemoveColumns(QModelIndex(), nColumn, nColumn);
# <div class="de1">    <span class="br0">{</span></div>
m_data.removeColumn(nColumn - m_columns.count());
# <div class="de2">        beginRemoveColumns<span class="br0">(</span>[http://doc.qt.io/QModelIndex.html <span class="kw5">QModelIndex</span>]<span class="br0">(</span><span class="br0">)</span><span class="sy0">,</span> nColumn<span class="sy0">,</span> nColumn<span class="br0">)</span><span class="sy0">;</span></div>
endRemoveColumns();
# <div class="de1">        m_data.<span class="me1">removeColumn</span><span class="br0">(</span>nColumn <span class="sy0">-</span> m_columns.<span class="me1">count</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div>
}
# <div class="de1">        endRemoveColumns<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
}
# <div class="de1">    <span class="br0">}</span></div>
</code>
# <div class="de1"><span class="br0">}</span></div>
 
</div>

Latest revision as of 13:10, 1 June 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

← Teil 2: Delegate und View
↑Übersicht Model/View Tutorial:CD Verwaltung↑
Teil 4: Copy / Paste und Drag and Drop →

Hinzufügen / Löschen von Zeilen / Spalten

Wenn Zeilen oder Spalten zu einem Model hinzugefügt werden, muss bevore die Daten verändert werden die Methode beginInsertRows/beginInsertColumns und nach Änderung der Daten endInsertRows/endInsertColumns aufgerufen werden. Synchron dazu muss beim Löschen von Zeilen / Spalten beginRemoveRows/beginRemoveColumns und endRemoveRos/endRemoveColumns aufgerufen werden.

In unserem Datenmodell gibt es dafür 4 Methoden:

  • void addCd();
  • void removeCd(const QModelIndex& index);
  • bool addColumn(const QString& rszColumnName);
  • void removeColumn(int nColumn);

ichtig ist, das das Ändern der Daten von beginXXX endXXX umklammert ist, damit die Views in diesem Zeitraum issen, dass sie nicht auf diese Daten zugreifen dürfen.

beginInsertXXX

Egal ob Spalten oder Zeilen eingefügt werden sollen, die Logik ist dieselbe:

Die Methoden syntax ist: beginInsertXXX(parent, start, end)

  • parent spezifiziert das Eltern element, bei einer Tabelle ist das immer ein ungültiger Model Index
  • start ist der erste neue Index, im Beispiel unten 4
  • end ist der letzte neue Index, in unserem Beispiel 6

Hier ein Beispiel aus der Qt-Dokumentation:
Qt docu

Nach erfolgreichem Einfügen in die unterlagerten Daten muss noch entsprechend endInsertXXX aufgerufen werden.

beginRemoveXXX

Egal ob Spalten oder Zeilen gelöscht werden sollen, die Logik ist dieselbe:

Die Methoden syntax ist: beginRemoveXXX(parent, start, end)

  • parent spezifiziert das Eltern element, bei einer Tabelle ist das immer ein ungültiger Model Index
  • start ist der erste zu löschende Index, im Beispiel unten 4
  • end ist der letzte zu löschende Index, in unserem Beispiel 6

Hier ein Beispiel aus der Qt-Dokumentation:
Qt docu

Nach erfolgreichem Löschen in den unterlagerten Daten muss noch entsprechend endRemoveXXX aufgerufen werden.

Im folgenden die Implementierungen:

addCd

void CdModel::addCd()
{
    beginInsertRows(QModelIndex(), m_data.size(), m_data.size());
    CdDisk newDisk;
    newDisk.m_author = tr("<unknown>");
    newDisk.m_genre = tr("<unknown>");
    newDisk.m_title = tr("<unknown>");
    newDisk.m_year = 0;
    m_data.addCD(newDisk);
    endInsertRows();
}

removeCd

void CdModel::removeCd(const QModelIndex& index)
{
	if(index.isValid() && (0 <= index.row()) && (index.row() < rowCount()))
	{
		beginRemoveRows(QModelIndex(), index.row(), index.row());
		m_data.removeCD(index.row());
		endRemoveRows();
	}
}

addColumn

bool CdModel::addColumn(const QString& rszColumnName)
{
	if(!m_data.columnExists(rszColumnName))
	{
		beginInsertColumns(QModelIndex(), columnCount(), columnCount());
		m_data.addColumn(rszColumnName);
		endInsertColumns();
		return true;
	}
	return false;
}

removeColumn

void CdModel::removeColumn(int nColumn)
{
	if((m_columns.count() <= nColumn) && (nColumn < columnCount()))
	{
		beginRemoveColumns(QModelIndex(), nColumn, nColumn);
		m_data.removeColumn(nColumn - m_columns.count());
		endRemoveColumns();
	}
}