Model View Tutorial Part1 CVirtualCdRack/de: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Decode HTML entity names)
Line 23: Line 23:
# <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="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="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="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"><</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="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">    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>
Line 38: Line 38:
# <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_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">=</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="de1">    m_view<span class="sy0">-></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="de2">    m_view<span class="sy0">-></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">    setCentralWidget<span class="br0">(</span>m_view<span class="br0">)</span><span class="sy0">;</span></div>
# <div class="de1"> </div>
# <div class="de1"> </div>
Line 47: Line 47:
# <div class="de1">    createStatusBar<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"> </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="kw2">connect</span><span class="br0">(</span><span class="sy0">&</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 class="de1"><span class="br0">}</span></div>


Line 56: Line 56:
<div class="cpp-qt geshi">
<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"><</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">></span></div>
# <div class="de1"><span class="sy0">&lt;!</span>DOCTYPE CdRack<span class="sy0">&gt;</span></div>
# <div class="de1"><span class="sy0"><!</span>DOCTYPE CdRack<span class="sy0">></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"><</span>CdRack version<span class="sy0">=</span><span class="st0">"1.0"</span><span class="sy0">></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="de1">    <span class="sy0"><</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">></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="de2">    <span class="sy0"><</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">></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"><</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">></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"><</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">></span></div>
# <div class="de1"><span class="sy0">&lt;/</span>CdRack<span class="sy0">&gt;</span></div>
# <div class="de1"><span class="sy0"></</span>CdRack<span class="sy0">></span></div>
# <div class="de1"><span class="sy0">--</span></div>
# <div class="de1"><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.

↑Ü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. --


**