可以学习到
(1)菜单栏的创建,状态条。
(2)读取和保存应用程序的配置。
(3)spliter的布局。
(4)QWidgetTreeList以及QStingList
(5)构造一个QMainWindow窗体实例的方法
mailclient.h
#ifndef MAILCLIENT_H #define MAILCLIENT_H #include <QMainWindow> class QIcon; class QSplitter; class QTextEdit; class QTreeWidget; class MailClient : public QMainWindow { Q_OBJECT public: MailClient(); protected: void closeEvent(QCloseEvent *event); private: void addFolder(const QIcon &icon, const QString &name); void addMessage(const QString &subject, const QString &from, const QString &date);//标题。发信人。时间。 void readSettings(); void writeSettings(); QSplitter *mainSplitter; QSplitter *rightSplitter; QTreeWidget *foldersTreeWidget; QTreeWidget *messagesTreeWidget; QTextEdit *textEdit; }; #endif
mailclient.cpp
#include <QtGui> #include "mailclient.h" //构造函数 MailClient::MailClient() { //文件夹和垃圾站的icon QIcon folderIcon(style()->standardPixmap(QStyle::SP_DirClosedIcon)); QIcon trashIcon(style()->standardPixmap(QStyle::SP_FileIcon)); //俩QTreeeWidget的header是QStringList QStringList folderLabels; folderLabels << tr("Folders"); foldersTreeWidget = new QTreeWidget; foldersTreeWidget->setHeaderLabels(folderLabels); addFolder(folderIcon, tr("Inbox")); addFolder(folderIcon, tr("Outbox")); addFolder(folderIcon, tr("Sent")); addFolder(trashIcon, tr("Trash")); QStringList messageLabels; messageLabels << tr("Subject") << tr("Sender") << tr("Date"); messagesTreeWidget = new QTreeWidget; messagesTreeWidget->setHeaderLabels(messageLabels); addMessage(tr("Happy New Year!"), tr("Grace K. <[email protected]>"), tr("2006-12-31")); addMessage(tr("Radically new concept!"), tr("Grace K. <[email protected]>"), tr("2006-12-22")); addMessage(tr("Accounts"), tr("[email protected]"), tr("2006-12-31")); addMessage(tr("Expenses"), tr("Joe Bloggs <[email protected]>"), tr("2006-12-25")); addMessage(tr("Re: Expenses"), tr("Andy <[email protected]>"), tr("2007-01-02")); addMessage(tr("Re: Accounts"), tr("Joe Bloggs <[email protected]>"), tr("2007-01-03")); addMessage(tr("Re: Accounts"), tr("Andy <[email protected]>"), tr("2007-01-03")); messagesTreeWidget->resizeColumnToContents(0); messagesTreeWidget->resizeColumnToContents(1); textEdit = new QTextEdit; textEdit->setReadOnly(true);//文本编辑框只读 //文本框内的内容是HTML格式的。 textEdit->setHtml("<table bolder=0>" "<tr><td align=right><b>Subject:</b>" "<td><p>Happy New Year!" "<tr><td align=right><b>Date:</b>" "<td><p>Sun, 31 Dec 2006" "<tr><td align=right><b>From:</b>" "<td><p>Grace K. <[email protected]>" "<tr><td align=right><b>To:</b>" "<td>[email protected]" "</table>" "<br>I want to seize this occasion to thank " "everybody for the year that has gone, and want " "to wish you the best for next year. It has been " "a pleasure to work with you all on the Hawk " "project, and I'm sure we'll get concrete " "results shortly." "<br><br>Happy New Year!" "<br>--Grace"); //菜单栏构造 menuBar()->addMenu(tr("&File")); menuBar()->addMenu(tr("&Edit")); menuBar()->addMenu(tr("&View")); menuBar()->addMenu(tr("F&older")); menuBar()->addMenu(tr("&Message")); menuBar()->addMenu(tr("&Settings")); menuBar()->addMenu(tr("&Help")); //状态条 statusBar()->showMessage(tr("No new messages on server")); //右边的spliter是垂直的。 rightSplitter = new QSplitter(Qt::Vertical); rightSplitter->addWidget(messagesTreeWidget); rightSplitter->addWidget(textEdit); // rightSplitter->setStretchFactor(1, 1); //主布局是水平的spliter mainSplitter = new QSplitter(Qt::Horizontal); mainSplitter->addWidget(foldersTreeWidget); mainSplitter->addWidget(rightSplitter); // mainSplitter->setStretchFactor(1, 1); //主布局作为中央窗体。 setCentralWidget(mainSplitter); setWindowTitle(tr("Mail Client")); readSettings(); } //重载关闭事件函数的目的在于关闭时保存设置。 void MailClient::closeEvent(QCloseEvent *event) { writeSettings(); event->accept(); } //加入一个文件夹。 void MailClient::addFolder(const QIcon &icon, const QString &name) { QTreeWidgetItem *root; if (foldersTreeWidget->topLevelItemCount() == 0) { root = new QTreeWidgetItem(foldersTreeWidget); root->setText(0, tr("Mail")); foldersTreeWidget->setItemExpanded(root, true); } else { root = foldersTreeWidget->topLevelItem(0); } QTreeWidgetItem *newItem = new QTreeWidgetItem(root); newItem->setText(0, name); newItem->setIcon(0, icon); if (!foldersTreeWidget->currentItem()) foldersTreeWidget->setCurrentItem(newItem); } //添加一个新邮件消息。 void MailClient::addMessage(const QString &subject, const QString &from, const QString &date) { QTreeWidgetItem *newItem = new QTreeWidgetItem(messagesTreeWidget); newItem->setText(0, subject); newItem->setText(1, from); newItem->setText(2, date); if (!messagesTreeWidget->currentItem()) messagesTreeWidget->setCurrentItem(newItem); } void MailClient::readSettings() { QSettings settings("Software Inc.", "Mail Client"); settings.beginGroup("mainWindow"); resize(settings.value("size", QSize(480, 360)).toSize()); mainSplitter->restoreState( settings.value("mainSplitter").toByteArray()); rightSplitter->restoreState( settings.value("rightSplitter").toByteArray()); settings.endGroup(); } void MailClient::writeSettings() { QSettings settings("Software Inc.", "Mail Client"); settings.beginGroup("mainWindow"); settings.setValue("size", size()); settings.setValue("mainSplitter", mainSplitter->saveState()); settings.setValue("rightSplitter", rightSplitter->saveState()); settings.endGroup(); }
俩都是QTreeWidget
header是QStringList