Model View Tutorial Part1 CVirtualCdRack/de: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
<div class="factbox right">


===Table of Content===
* [[#453994a4a1b12fa9f20637bb33fc4d63|Die Klasse VirtualCdRack]]
** [[#7ea8f52b947073e2281fe24121242836|Struktur der XML-Datei mit den CDs]]
</div>
[http://qt.io/groups/qt_german/wiki/Model_View_Tutorial ↑Übersicht Model/View Tutorial:CD Verwaltung↑]<br />[http://qt.io/groups/qt_german/wiki/Model_View_Tutorial_Teil1_CdRack Allgemeines: Klasse CdRack→]
=Die Klasse VirtualCdRack=
Die Klasse '''VirtualCdRack''' bildet das Hauptfenster der Anwendung.<br /> Sie erstellt die Menüs, Toolbars und die Statusbar.
Des weiteren hält sie eine Instanz der Datenklasse CdRack, eine Instanz des Datenmodells CdModel.<br /> Sie erstellt eine ''QTabelView''-Instanz und verbindet sie mit dem Modell.
<div class="cpp-qt geshi">
# <div class="de1">VirtualCdRack<span class="sy0">::</span><span class="me2">VirtualCdRack</span><span class="br0">(</span>[http://doc.qt.io/QWidget.html <span class="kw5">QWidget</span>] <span class="sy0">*</span>parent<span class="br0">)</span></div>
# <div class="de1">    <span class="sy0">:</span> [http://doc.qt.io/QMainWindow.html <span class="kw5">QMainWindow</span>]<span class="br0">(</span>parent<span class="br0">)</span></div>
# <div class="de1"><span class="br0">{</span></div>
# <div class="de1">    <span class="kw1">for</span><span class="br0">(</span><span class="kw4">int</span> i <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0">&lt;</span> MaxRecentFiles<span class="sy0">;</span> <span class="sy0">++</span>i<span class="br0">)</span></div>
# <div class="de2">        m_recentFileActs<span class="br0">[</span>i<span class="br0">]</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span></div>
# <div class="de1">    setWindowIcon<span class="br0">(</span>[http://doc.qt.io/QIcon.html <span class="kw5">QIcon</span>]<span class="br0">(</span><span class="st0">":/images/VirtualCdRack.png"</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1">    init<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1">    setCurrentFile<span class="br0">(</span><span class="st0">""</span><span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1">    readSettings<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de2"><span class="br0">}</span></div>
# <div class="de1"> </div>
# <div class="de1"> </div>
# <div class="de1"><span class="kw4">void</span> VirtualCdRack<span class="sy0">::</span><span class="me2">init</span><span class="br0">(</span><span class="br0">)</span></div>
# <div class="de1"><span class="br0">{</span></div>
# <div class="de2">    m_isUntitled <span class="sy0">=</span> true<span class="sy0">;</span></div>
# <div class="de1"> </div>
# <div class="de1">    m_model <span class="sy0">=</span> <span class="kw1">new</span> CdModel<span class="br0">(</span>m_data<span class="sy0">,</span> <span class="kw1">this</span><span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1">    m_view <span class="sy0">=</span> <span class="kw1">new</span> QTableView<span class="sy0">;</span></div>
# <div class="de1">    m_view<span class="sy0">-&gt;</span><span class="me3">setModel</span><span class="br0">(</span>m_model<span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de2">    m_view<span class="sy0">-&gt;</span><span class="me3">setAlternatingRowColors</span><span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1">    setCentralWidget<span class="br0">(</span>m_view<span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1"> </div>
# <div class="de1">    createActions<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1">    createMenus<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de2">    createToolBars<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1">    createStatusBar<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1"> </div>
# <div class="de1">    <span class="kw2">connect</span><span class="br0">(</span><span class="sy0">&amp;</span>m_data<span class="sy0">,</span> SIGNAL<span class="br0">(</span>contentsChanged<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">,</span> <span class="kw1">this</span><span class="sy0">,</span> SLOT<span class="br0">(</span>documentWasModified<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1"><span class="br0">}</span></div>
</div>
==Struktur der <span class="caps">XML</span>-Datei mit den CDs==
<div class="cpp-qt geshi">
# <div class="de1"><span class="sy0">&lt;</span>?xml version<span class="sy0">=</span><span class="st0">"1.0"</span> encoding<span class="sy0">=</span><span class="st0">"UTF-8"</span>?<span class="sy0">&gt;</span></div>
# <div class="de1"><span class="sy0">&lt;!</span>DOCTYPE CdRack<span class="sy0">&gt;</span></div>
# <div class="de1"><span class="sy0">&lt;</span>CdRack version<span class="sy0">=</span><span class="st0">"1.0"</span><span class="sy0">&gt;</span></div>
# <div class="de1">    <span class="sy0">&lt;</span>CD title<span class="sy0">=</span><span class="st0">"On and On"</span> author<span class="sy0">=</span><span class="st0">"Fiddler's Green"</span> genre<span class="sy0">=</span><span class="st0">"Irish Folk"</span> year<span class="sy0">=</span><span class="st0">"1997"</span><span class="sy0">&gt;</span></div>
# <div class="de2">    <span class="sy0">&lt;</span>CD title<span class="sy0">=</span><span class="st0">"Von Spitzbuben und anderen Halunken"</span> author<span class="sy0">=</span><span class="st0">"Schandmaul"</span> genre<span class="sy0">=</span><span class="st0">"Mittelalter-Rock"</span> year<span class="sy0">=</span><span class="st0">"2001"</span><span class="sy0">&gt;</span></div>
# <div class="de1">    <span class="sy0">&lt;</span>CD title<span class="sy0">=</span><span class="st0">"Look Sharp"</span> author<span class="sy0">=</span><span class="st0">"Roxette"</span> genre<span class="sy0">=</span><span class="st0">"Rock"</span> year<span class="sy0">=</span><span class="st0">"1988"</span><span class="sy0">&gt;</span></div>
# <div class="de1">    <span class="sy0">&lt;</span>CD title<span class="sy0">=</span><span class="st0">"Oceanborn"</span> author<span class="sy0">=</span><span class="st0">"Nightwish"</span> genre<span class="sy0">=</span><span class="st0">"Hardrock"</span> year<span class="sy0">=</span><span class="st0">"1998"</span><span class="sy0">&gt;</span></div>
# <div class="de1"><span class="sy0">&lt;/</span>CdRack<span class="sy0">&gt;</span></div>
# <div class="de1"><span class="sy0">--</span></div>
</div>
<br /> **

Revision as of 13:56, 25 February 2015

↑Übersicht Model/View Tutorial:CD Verwaltung↑
Allgemeines: Klasse CdRack→

Die Klasse VirtualCdRack

Die Klasse VirtualCdRack bildet das Hauptfenster der Anwendung.
Sie erstellt die Menüs, Toolbars und die Statusbar.

Des weiteren hält sie eine Instanz der Datenklasse CdRack, eine Instanz des Datenmodells CdModel.
Sie erstellt eine QTabelView-Instanz und verbindet sie mit dem Modell.

  1. VirtualCdRack::VirtualCdRack(QWidget *parent)
  2.     : QMainWindow(parent)
  3. {
  4.     for(int i = 0; i < MaxRecentFiles; ++i)
  5.         m_recentFileActs[i] = 0;
  6.     setWindowIcon(QIcon(":/images/VirtualCdRack.png"));
  7.     init();
  8.     setCurrentFile("");
  9.     readSettings();
  10. }
  11.  
  12.  
  13. void VirtualCdRack::init()
  14. {
  15.     m_isUntitled = true;
  16.  
  17.     m_model = new CdModel(m_data, this);
  18.     m_view = new QTableView;
  19.     m_view->setModel(m_model);
  20.     m_view->setAlternatingRowColors(true);
  21.     setCentralWidget(m_view);
  22.  
  23.     createActions();
  24.     createMenus();
  25.     createToolBars();
  26.     createStatusBar();
  27.  
  28.     connect(&m_data, SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
  29. }

Struktur der XML-Datei mit den CDs

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE CdRack>
  3. <CdRack version="1.0">
  4.     <CD title="On and On" author="Fiddler's Green" genre="Irish Folk" year="1997">
  5.     <CD title="Von Spitzbuben und anderen Halunken" author="Schandmaul" genre="Mittelalter-Rock" year="2001">
  6.     <CD title="Look Sharp" author="Roxette" genre="Rock" year="1988">
  7.     <CD title="Oceanborn" author="Nightwish" genre="Hardrock" year="1998">
  8. </CdRack>
  9. --


**