Tanky WooRSS

Qt demo --- 2.TextEdit

26 Mar 2011
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

这个demo在Qt demo里是Main Windows里的Application。

截图:

textEdit

知识点:

1.QINITRESOURCE     Initializes the resources specified by the .qrc file with the specified base name. Normally, Qt resources are loaded automatically at startup. The QINITRESOURCE() macro is necessary on some platforms for resources stored in a static library.

2.QSettings class     app.setOrganizationName("CppLeYuan");     app.setApplicationName("TextEdit");

 3.事件的accept()和ignore()

Setting the accept parameter indicates that the event receiver wants the event. Unwanted events might be propagated to the parent widget.

Setting the ignore parameter indicates that the event receiver does not want the event. Unwanted events might be propagated to the parent widget.

在豆子博客第20篇讲的不错:

http://devbean.blog.51cto.com/448512/225519

 

4.Qt打开文件的函数QFileDialog::getopenFileName()

注意里面的参数。

 

5.saveAs()和saveFile()的不同应用

注意saveAs调用getopenFileName(),而saveFile是通过file.open打开。

 

6.QMessageBox的使用

StandardButton QMessageBox::warning ( QWidget * parent,
                                      const QString & title,
                                      const QString & text, S
                                      tandardButtons buttons = Ok,
                                      StandardButton defaultButton = NoButton )

 

7.setWindowModified函数的使用

 

8.新建QAction

新建QAction,以及设置快捷键setShortcuts,设置提示信息setStatusTip,最后连接信号和槽。

eg.

    newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
    newAct->setShortcuts(QKeySequence::New);   
    newAct->setStatusTip(tr("Create a new file"));   
    connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));

 

9.通过addMenu新建QMenu,并且添加QAction

eg.

    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(newAct);
    fileMenu->addAction(openAct);
    fileMenu->addAction(saveAct);

 

10.通过addToolBar新建toolBar,并且添加QAction

    fileToolBar = addToolBar(tr("File"));
    fileToolBar->addAction(newAct);
    fileToolBar->addAction(openAct);
    fileToolBar->addAction(saveAct);

 

11.statusBar()的使用

 

12.保存和设置用户偏好。

自定义函数readSettings和writeSettings这块还不懂。加亮做个记号!

 

13.关于信号槽机制与事件的区别。

这一块暂时还未完全明白。最近一段时间还得在坛子里向其他高手多请教一下,然后另开帖详细总结。

而且《C++ GUI With Qt4》第七章的ticker代码是一个理解事件不错的代码。

 

总结:

总体来说,这个demo值得多多分析几次,在Qt demo的Main Windows里面,还有其他几个诸如MDI,如何保存最近打开一项等等功能,这些都可以最后结合在一起,实现一个完整的TextEdit纯文本编辑器。