Model View Tutorial Part3/de: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Decode HTML entity names)
Line 24: Line 24:


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


Line 68: Line 68:
# <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>
# <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>
# <div class="de1">    CdDisk newDisk<span class="sy0">;</span></div>
# <div class="de1">    CdDisk newDisk<span class="sy0">;</span></div>
# <div class="de2">    newDisk.<span class="me1">m_author</span> <span class="sy0">=</span> tr<span class="br0">(</span><span class="st0">"&lt;unknown&gt;"</span><span class="br0">)</span><span class="sy0">;</span></div>
# <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>
# <div class="de1">    newDisk.<span class="me1">m_genre</span> <span class="sy0">=</span> tr<span class="br0">(</span><span class="st0">"&lt;unknown&gt;"</span><span class="br0">)</span><span class="sy0">;</span></div>
# <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>
# <div class="de1">    newDisk.<span class="me1">m_title</span> <span class="sy0">=</span> tr<span class="br0">(</span><span class="st0">"&lt;unknown&gt;"</span><span class="br0">)</span><span class="sy0">;</span></div>
# <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>
# <div class="de1">    newDisk.<span class="me1">m_year</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span></div>
# <div class="de1">    newDisk.<span class="me1">m_year</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span></div>
# <div class="de1">    m_data.<span class="me1">addCD</span><span class="br0">(</span>newDisk<span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1">    m_data.<span class="me1">addCD</span><span class="br0">(</span>newDisk<span class="br0">)</span><span class="sy0">;</span></div>
Line 82: Line 82:
<div class="cpp-qt geshi">
<div class="cpp-qt geshi">


# <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">&amp;</span> index<span class="br0">)</span></div>
# <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>
# <div class="de1"><span class="br0">{</span></div>
# <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">&amp;&amp;</span> <span class="br0">(</span><span class="nu0">0</span> <span class="sy0">&lt;=</span> index.<span class="me1">row</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="sy0">&amp;&amp;</span> <span class="br0">(</span>index.<span class="me1">row</span><span class="br0">(</span><span class="br0">)</span> <span class="sy0">&lt;</span> rowCount<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><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>
# <div class="de1">    <span class="br0">{</span></div>
# <div class="de1">    <span class="br0">{</span></div>
# <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>
# <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>
Line 98: Line 98:
<div class="cpp-qt geshi">
<div class="cpp-qt geshi">


# <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">&amp;</span> rszColumnName<span class="br0">)</span></div>
# <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>
# <div class="de1"><span class="br0">{</span></div>
# <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>
# <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>
Line 118: Line 118:
# <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>
# <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>
# <div class="de1"><span class="br0">{</span></div>
# <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">&lt;=</span> nColumn<span class="br0">)</span> <span class="sy0">&amp;&amp;</span> <span class="br0">(</span>nColumn <span class="sy0">&lt;</span> columnCount<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><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>
# <div class="de1">    <span class="br0">{</span></div>
# <div class="de1">    <span class="br0">{</span></div>
# <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>
# <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>

Revision as of 17:35, 12 March 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

  1. void CdModel::addCd()
  2. {
  3.     beginInsertRows(QModelIndex(), m_data.size(), m_data.size());
  4.     CdDisk newDisk;
  5.     newDisk.m_author = tr("<unknown>");
  6.     newDisk.m_genre = tr("<unknown>");
  7.     newDisk.m_title = tr("<unknown>");
  8.     newDisk.m_year = 0;
  9.     m_data.addCD(newDisk);
  10.     endInsertRows();
  11. }

removeCd

  1. void CdModel::removeCd(const QModelIndex& index)
  2. {
  3.     if(index.isValid() && (0 <= index.row()) && (index.row() < rowCount()))
  4.     {
  5.         beginRemoveRows(QModelIndex(), index.row(), index.row());
  6.         m_data.removeCD(index.row());
  7.         endRemoveRows();
  8.     }
  9. }

addColumn

  1. bool CdModel::addColumn(const QString& rszColumnName)
  2. {
  3.     if(!m_data.columnExists(rszColumnName))
  4.     {
  5.         beginInsertColumns(QModelIndex(), columnCount(), columnCount());
  6.         m_data.addColumn(rszColumnName);
  7.         endInsertColumns();
  8.         return true;
  9.     }
  10.     return false;
  11. }

removeColumn

  1. void CdModel::removeColumn(int nColumn)
  2. {
  3.     if((m_columns.count() <= nColumn) && (nColumn < columnCount()))
  4.     {
  5.         beginRemoveColumns(QModelIndex(), nColumn, nColumn);
  6.         m_data.removeColumn(nColumn - m_columns.count());
  7.         endRemoveColumns();
  8.     }
  9. }