当前位置:   article > 正文

Qt开发-Qt中的事件处理(上)_qt 修改事件

qt 修改事件

一、图形界面应用程序的消息处理模型

二、Qt的事件处理

1、Qt平台将系统产生的消息转换为Qt事件(每一个系统消息对象Qt平台的一个事件)

(1)、Qt事件是一个QEvent的对象

(2)、Qt事件用于描述程序内部或者外部发生的动作

(3)、任意的QObject对象都具备事件处理的能力

2、GUI应用程序的事件处理方式

(1)、Qt事件产生后立即被分发到QWidget对象

(2)、QWidget中的event(QEvent*)进行事件处理

(3)、event()根据事件类型调用不同的事件处理函数

(4)、在事件处理函数中发送Qt中预定义的信号

(5)、调用信号关联的槽函数

3、情景分析:按钮点击

(1)、接收到鼠标事件(代表一个系统消息)

(2)、调用event(QEvent*)成员函数

(3)、调用MouseReleaseEvent(QMouseEvent*)成员函数

(4)、调用clicked()成员函数

(5)、触发信号SIGNAL(clicked())

4、事件(QEvent)和信号(SIGNAL)不同

(1)、事件由具体对象进行处理

(2)、信号由具体对象主动产生

(3)、改写事件处理函数可能导致程序行为发生改变

(4)、信号是否存在对应的槽函数不会改变程序的行为

(5)、一般而言,信号在具体的事件处理函数中产生

三、文本编辑器的关闭操作

  1. void MainWindow::closeEvent(QCloseEvent *e)//没有对应的信号来处理,只能重写事件处理函数
  2. {
  3. preEditorChanged();
  4. if(!m_isTextChanged)
  5. {
  6. QMainWindow::closeEvent(e);//调用父类的关闭事件处理函数
  7. }
  8. else
  9. {
  10. e->ignore();//点取消的话就忽略这个对话框
  11. }
  12. }

完整代码:

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMenuBar>
  4. #include <QMenu>
  5. #include <QAction>
  6. #include <QString>
  7. #include <QtGui/QMainWindow>
  8. #include <QToolBar>
  9. #include <QIcon>
  10. #include <QSize>
  11. #include <QStatusBar>
  12. #include <QLabel>
  13. #include <QPlainTextEdit>
  14. #include <QFileDialog>
  15. class MainWindow : public QMainWindow
  16. {
  17. Q_OBJECT
  18. private:
  19. QPlainTextEdit mainEdit;
  20. QLabel statusLabel;
  21. QString m_filePath;//记得在构造函数里初始化
  22. bool m_isTextChanged;//构造函数里初始化为false
  23. MainWindow(QWidget *parent = 0);
  24. MainWindow(const MainWindow& obj);
  25. MainWindow* operator = (const MainWindow& obj);
  26. bool construct();
  27. bool initMenuBar();//菜单栏
  28. bool initToolBar();//工具栏
  29. bool initStatusBar();//状态栏
  30. bool initinitMainEditor();//编辑窗口
  31. bool initFileMenu(QMenuBar* mb);//文件菜单
  32. bool initEditMenu(QMenuBar* mb);//编辑菜单
  33. bool initFormatMenu(QMenuBar* mb);//格式菜单
  34. bool initViewMenu(QMenuBar* mb);//视图菜单
  35. bool initHelpMenu(QMenuBar* mb);//帮助菜单
  36. bool initFileToolItem(QToolBar* tb);//工具选项
  37. bool initEditToolItem(QToolBar* tb);
  38. bool initFormatToolItem(QToolBar* tb);
  39. bool initViewToolItem(QToolBar* tb);
  40. bool makeAction(QAction*& action,QMenu* menu, QString text, int key);//菜单项
  41. bool makeAction(QAction*& action,QToolBar* tb, QString tip, QString icon);
  42. QString showFileDialog(QFileDialog::AcceptMode mode, QString title);//文件对话框
  43. void showErrorMessage(QString message);//错误消息对话框
  44. int showQuesstionMessage(QString message);//问题消息对话框
  45. QString saveCurrentData(QString path = "");
  46. void preEditorChanged();
  47. private slots:
  48. void onFileNew();
  49. void onFileOpen();
  50. void onFlieSave();
  51. void onFileSaveAs();
  52. void onTextChanged();
  53. protected:
  54. void closeEvent(QCloseEvent *e);//重写关闭窗口的事件处理函数
  55. public:
  56. static MainWindow* NewInstance();
  57. ~MainWindow();
  58. };
  59. #endif // MAINWINDOW_H
  1. #include "MainWindow.h"
  2. #include <QDebug>
  3. MainWindow::MainWindow(QWidget *parent)
  4. : QMainWindow(parent), statusLabel(this)
  5. {
  6. m_filePath = "";
  7. m_isTextChanged = false;
  8. setWindowTitle("NotePad-[New]");
  9. }
  10. bool MainWindow::construct()
  11. {
  12. bool ret = true;
  13. ret = ret && initMenuBar();
  14. ret = ret && initToolBar();
  15. ret = ret && initStatusBar();
  16. ret = ret && initinitMainEditor();
  17. return ret;
  18. }
  19. MainWindow* MainWindow::NewInstance()
  20. {
  21. MainWindow* ret = new MainWindow();
  22. if((ret==NULL) || (!ret->construct()))
  23. {
  24. delete ret;
  25. ret = NULL;
  26. }
  27. return ret;
  28. }
  29. bool MainWindow::initMenuBar()//菜单栏
  30. {
  31. bool ret = true;
  32. QMenuBar* mb = menuBar();//一定要注意是menuBar(),这是普通成员函数,不是构造函数
  33. ret = ret && initFileMenu(mb);//传一个参数是为了在initFileMenu()函数将menu加入菜单栏
  34. ret = ret && initEditMenu(mb);
  35. ret = ret && initFormatMenu(mb);
  36. ret = ret && initViewMenu(mb);
  37. ret = ret && initHelpMenu(mb);
  38. return ret;
  39. }
  40. bool MainWindow::initToolBar()//工具栏
  41. {
  42. bool ret = true;
  43. QToolBar* tb = addToolBar("Tool Bar");
  44. //tb->setMovable(false);
  45. //tb->setFloatable(false);
  46. tb->setIconSize(QSize(16,16));
  47. ret = ret && initFileToolItem(tb);
  48. tb->addSeparator();
  49. ret = ret && initEditToolItem(tb);
  50. tb->addSeparator();
  51. ret = ret && initFormatToolItem(tb);
  52. tb->addSeparator();
  53. ret = ret && initViewToolItem(tb);
  54. return ret;
  55. }
  56. bool MainWindow::initStatusBar()//状态栏
  57. {
  58. bool ret = true;
  59. QStatusBar* sb = statusBar();
  60. QLabel* label = new QLabel("Made By LGC");
  61. if(label != NULL)
  62. {
  63. statusLabel.setMinimumWidth(200);
  64. statusLabel.setAlignment(Qt::AlignHCenter);
  65. statusLabel.setText("Ln:1 Col:1");
  66. label->setMinimumWidth(200);
  67. label->setAlignment(Qt::AlignHCenter);
  68. sb->addPermanentWidget(new QLabel());//单纯加入分隔符
  69. sb->addPermanentWidget(&statusLabel);
  70. sb->addPermanentWidget(label);
  71. }
  72. else
  73. {
  74. ret = false;
  75. }
  76. return ret;
  77. }
  78. bool MainWindow::initinitMainEditor()//编辑窗口
  79. {
  80. bool ret = true;
  81. mainEdit.setParent(this);
  82. setCentralWidget(&mainEdit);
  83. connect(&mainEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
  84. return ret;
  85. }
  86. /************************************************文件菜单********************************************************/
  87. bool MainWindow::initFileMenu(QMenuBar* mb)
  88. {
  89. bool ret = true;
  90. QMenu* menu = new QMenu("File(&F)");//创建文件菜单,(&F)是为了可以Alt+F打开
  91. ret = (menu != NULL);
  92. if(ret)
  93. {
  94. QAction* action = NULL;
  95. //New
  96. ret = ret && makeAction(action, menu, "New(&N)",Qt::CTRL + Qt::Key_N);
  97. if(ret)
  98. {
  99. connect(action, SIGNAL(triggered()), this, SLOT(onFileNew()));
  100. menu->addAction(action);
  101. }
  102. menu->addSeparator();
  103. //Open
  104. ret = ret && makeAction(action, menu,"Open(&O)...",Qt::CTRL + Qt::Key_O);
  105. if(ret)
  106. {
  107. connect(action, SIGNAL(triggered()), this, SLOT(onFileOpen()));
  108. menu->addAction(action);
  109. }
  110. menu->addSeparator();
  111. //Save
  112. ret = ret && makeAction(action, menu,"Save(&S)",Qt::CTRL + Qt::Key_S);
  113. if(ret)
  114. {
  115. connect(action, SIGNAL(triggered()), this ,SLOT(onFlieSave()));
  116. menu->addAction(action);
  117. }
  118. menu->addSeparator();
  119. //Save As
  120. ret = ret && makeAction(action, menu, "Save As(&A)...",0);
  121. if(ret)
  122. {
  123. connect(action, SIGNAL(triggered()), this, SLOT(onFileSaveAs()));
  124. menu->addAction(action);
  125. }
  126. menu->addSeparator();
  127. //print
  128. ret = ret && makeAction(action, menu, "Print(&P)...",Qt::CTRL + Qt::Key_P);
  129. if(ret)
  130. {
  131. menu->addAction(action);
  132. }
  133. menu->addSeparator();
  134. //Exit
  135. ret = ret && makeAction(action, menu,"Exit(&X)",0);
  136. if(ret)
  137. {
  138. menu->addAction(action);//将菜单项加入到菜单
  139. }
  140. }
  141. if(ret)
  142. {
  143. mb->addMenu(menu);//将菜单加入到菜单栏
  144. }
  145. else
  146. {
  147. delete mb;
  148. }
  149. return ret;
  150. }
  151. /************************************************编辑菜单********************************************************/
  152. bool MainWindow::initEditMenu(QMenuBar* mb)
  153. {
  154. bool ret = true;
  155. QMenu* menu = new QMenu("Edit(&E)");
  156. ret = (menu != NULL);
  157. if(ret)
  158. {
  159. QAction* action = NULL;
  160. //Undo
  161. ret = ret && makeAction(action, menu,"Undo(&U)",Qt::CTRL + Qt::Key_Z);
  162. if(ret)
  163. {
  164. menu->addAction(action);
  165. }
  166. menu->addSeparator();
  167. //Redo
  168. ret = ret && makeAction(action, menu,"Redo(&R)...",Qt::CTRL + Qt::Key_Y);
  169. if(ret)
  170. {
  171. menu->addAction(action);
  172. }
  173. menu->addSeparator();
  174. //Cut
  175. ret = ret && makeAction(action, menu,"Cut(&T)",Qt::CTRL + Qt::Key_X);
  176. if(ret)
  177. {
  178. menu->addAction(action);
  179. }
  180. menu->addSeparator();
  181. //Copy
  182. ret = ret && makeAction(action, menu,"Copy(&C)...",Qt::CTRL + Qt::Key_C);
  183. if(ret)
  184. {
  185. menu->addAction(action);
  186. }
  187. menu->addSeparator();
  188. //Pase
  189. ret = ret && makeAction(action, menu,"Pase(&P)...",Qt::CTRL + Qt::Key_V);
  190. if(ret)
  191. {
  192. menu->addAction(action);
  193. }
  194. menu->addSeparator();
  195. //Delete
  196. ret = ret && makeAction(action, menu, "Delete(&L)",Qt::Key_Delete);
  197. if(ret)
  198. {
  199. menu->addAction(action);
  200. }
  201. menu->addSeparator();
  202. //Find
  203. ret = ret && makeAction(action, menu,"Find(&F)...",Qt::CTRL + Qt::Key_F);
  204. if(ret)
  205. {
  206. menu->addAction(action);
  207. }
  208. menu->addSeparator();
  209. //Replace
  210. ret = ret && makeAction(action, menu,"Replace(&R)...",Qt::CTRL + Qt::Key_H);
  211. if(ret)
  212. {
  213. menu->addAction(action);
  214. }
  215. menu->addSeparator();
  216. //Goto
  217. ret = ret && makeAction(action, menu,"Goto(&G)",Qt::CTRL + Qt::Key_G);
  218. if(ret)
  219. {
  220. menu->addAction(action);
  221. }
  222. menu->addSeparator();
  223. //Select All
  224. ret = ret && makeAction(action, menu, "Select All(&A)",Qt::CTRL + Qt::Key_A);
  225. if(ret)
  226. {
  227. menu->addAction(action);
  228. }
  229. }
  230. if(ret)
  231. {
  232. mb->addMenu(menu);
  233. }
  234. else
  235. {
  236. delete mb;
  237. }
  238. return ret;
  239. }
  240. /************************************************格式菜单********************************************************/
  241. bool MainWindow::initFormatMenu(QMenuBar* mb)
  242. {
  243. bool ret = true;
  244. QMenu* menu = new QMenu("Format(&O)");
  245. ret = (menu != NULL);
  246. if(ret)
  247. {
  248. QAction* action = NULL;
  249. //Auto Wrap
  250. ret = ret && makeAction(action, menu,"Auto Wrap(&W)",0);
  251. if(ret)
  252. {
  253. menu->addAction(action);
  254. }
  255. menu->addSeparator();
  256. //Font
  257. ret = ret && makeAction(action, menu,"Font(&F)...",0);
  258. if(ret)
  259. {
  260. menu->addAction(action);
  261. }
  262. }
  263. if(ret)
  264. {
  265. mb->addMenu(menu);
  266. }
  267. else
  268. {
  269. delete mb;
  270. }
  271. return ret;
  272. }
  273. /************************************************视图菜单********************************************************/
  274. bool MainWindow::initViewMenu(QMenuBar* mb)
  275. {
  276. bool ret = true;
  277. QMenu* menu = new QMenu("View(&V)");
  278. ret = (menu != NULL);
  279. if(ret)
  280. {
  281. QAction* action = NULL;
  282. //Tool Bar
  283. ret = ret && makeAction(action, menu,"Tool Bar(&T)",0);
  284. if(ret)
  285. {
  286. menu->addAction(action);
  287. }
  288. menu->addSeparator();
  289. //Status Bar
  290. ret = ret && makeAction(action, menu,"Status Bar(&S)",0);
  291. if(ret)
  292. {
  293. menu->addAction(action);
  294. }
  295. }
  296. if(ret)
  297. {
  298. mb->addMenu(menu);
  299. }
  300. else
  301. {
  302. delete mb;
  303. }
  304. return ret;
  305. }
  306. /************************************************帮助菜单********************************************************/
  307. bool MainWindow::initHelpMenu(QMenuBar* mb)
  308. {
  309. bool ret = true;
  310. QMenu* menu = new QMenu("Help(&H)");
  311. ret = (menu != NULL);
  312. if(ret)
  313. {
  314. QAction* action = NULL;
  315. //User Manual
  316. ret = ret && makeAction(action, menu,"User Manual",0);
  317. if(ret)
  318. {
  319. menu->addAction(action);
  320. }
  321. menu->addSeparator();
  322. //About NotePad
  323. ret = ret && makeAction(action, menu,"About NotePad...",0);
  324. if(ret)
  325. {
  326. menu->addAction(action);
  327. }
  328. }
  329. if(ret)
  330. {
  331. mb->addMenu(menu);
  332. }
  333. else
  334. {
  335. delete mb;
  336. }
  337. return ret;
  338. }
  339. /*****************************************工具************************************************************/
  340. bool MainWindow::initFileToolItem(QToolBar* tb)
  341. {
  342. bool ret = true;
  343. QAction* action = NULL;
  344. ret = ret && makeAction(action, tb, "New", ":/Res/pic/new.png");
  345. if(ret)
  346. {
  347. connect(action, SIGNAL(triggered()), this, SLOT(onFileNew()));
  348. tb->addAction(action);
  349. }
  350. ret = ret && makeAction(action, tb,"Open", ":/Res/pic/open.png");
  351. if(ret)
  352. {
  353. connect(action, SIGNAL(triggered()), this, SLOT(onFileOpen()));
  354. tb->addAction(action);
  355. }
  356. ret = ret && makeAction(action, tb,"Save", ":/Res/pic/save.png");
  357. if(ret)
  358. {
  359. connect(action, SIGNAL(triggered()), this ,SLOT(onFlieSave()));
  360. tb->addAction(action);
  361. }
  362. ret = ret && makeAction(action, tb,"Save As", ":/Res/pic/saveas.png");
  363. if(ret)
  364. {
  365. connect(action, SIGNAL(triggered()), this, SLOT(onFileSaveAs()));
  366. tb->addAction(action);
  367. }
  368. ret = ret && makeAction(action, tb,"Print", ":/Res/pic/print.png");
  369. if(ret)
  370. {
  371. tb->addAction(action);
  372. }
  373. return ret;
  374. }
  375. bool MainWindow::initEditToolItem(QToolBar* tb)
  376. {
  377. bool ret = true;
  378. QAction* action = NULL;
  379. ret = ret && makeAction(action, tb,"Undo", ":/Res/pic/undo.png");
  380. if(ret)
  381. {
  382. tb->addAction(action);
  383. }
  384. ret = ret && makeAction(action, tb,"Redo", ":/Res/pic/redo.png");
  385. if(ret)
  386. {
  387. tb->addAction(action);
  388. }
  389. ret = ret && makeAction(action, tb, "Cut", ":/Res/pic/cut.png");
  390. if(ret)
  391. {
  392. tb->addAction(action);
  393. }
  394. ret = ret && makeAction(action, tb,"Copy", ":/Res/pic/copy.png");
  395. if(ret)
  396. {
  397. tb->addAction(action);
  398. }
  399. ret = ret && makeAction(action, tb,"Paste", ":/Res/pic/paste.png");
  400. if(ret)
  401. {
  402. tb->addAction(action);
  403. }
  404. ret = ret && makeAction(action, tb,"Find", ":/Res/pic/find.png");
  405. if(ret)
  406. {
  407. tb->addAction(action);
  408. }
  409. ret = ret && makeAction(action, tb,"Replace", ":/Res/pic/replace.png");
  410. if(ret)
  411. {
  412. tb->addAction(action);
  413. }
  414. ret = ret && makeAction(action, tb,"Goto", ":/Res/pic/goto.png");
  415. if(ret)
  416. {
  417. tb->addAction(action);
  418. }
  419. return ret;
  420. }
  421. bool MainWindow::initFormatToolItem(QToolBar* tb)
  422. {
  423. bool ret = true;
  424. QAction* action = NULL;
  425. ret = ret && makeAction(action, tb, "Auto Wrap", ":/Res/pic/wrap.png");
  426. if(ret)
  427. {
  428. tb->addAction(action);
  429. }
  430. ret = ret && makeAction(action, tb,"Font", ":/Res/pic/font.png");
  431. if(ret)
  432. {
  433. tb->addAction(action);
  434. }
  435. return ret;
  436. }
  437. bool MainWindow::initViewToolItem(QToolBar* tb)
  438. {
  439. bool ret = true;
  440. QAction* action = NULL;
  441. ret = ret && makeAction(action, tb,"Tool Bar", ":/Res/pic/tool.png");
  442. if(ret)
  443. {
  444. tb->addAction(action);
  445. }
  446. ret = ret && makeAction(action, tb,"Status Bar", ":/Res/pic/status.png");
  447. if(ret)
  448. {
  449. tb->addAction(action);
  450. }
  451. return ret;
  452. }
  453. bool MainWindow::makeAction(QAction*& action,QMenu* menu, QString text, int key)//菜单项
  454. {
  455. bool ret = true;
  456. action = new QAction(text, menu);
  457. if(action != NULL)
  458. {
  459. action->setShortcut(QKeySequence(key));//创建快捷键
  460. }
  461. else
  462. {
  463. ret = false;
  464. }
  465. return ret;
  466. }
  467. bool MainWindow::makeAction(QAction*& action,QToolBar* tb, QString tip, QString icon)
  468. {
  469. bool ret = true;
  470. action = new QAction("", tb);
  471. if(action != NULL)
  472. {
  473. action->setToolTip(tip);
  474. action->setIcon(QIcon(icon));
  475. }
  476. else
  477. {
  478. ret = false;
  479. }
  480. return ret;
  481. }
  482. MainWindow::~MainWindow()
  483. {
  484. }
  1. #include <QFileDialog>
  2. #include <QStringList>
  3. #include <QFile>
  4. #include <QDebug>
  5. #include <QMessageBox>
  6. #include "MainWindow.h"
  7. #include <QMap>
  8. QString MainWindow::showFileDialog(QFileDialog::AcceptMode mode, QString title)
  9. {
  10. QString ret = "";
  11. QFileDialog fd;
  12. QStringList filters;
  13. QMap<QString, QString> map;
  14. const char* fileArray[][2]=
  15. {
  16. {"Text(*.txt)", ".txt"},
  17. {"All Files(*.*)", "*" },
  18. {NULL, NULL}
  19. };
  20. for(int i=0; fileArray[i][0] != NULL; i++)
  21. {
  22. filters.append(fileArray[i][0]);
  23. map.insert(fileArray[i][0], fileArray[i][1]);
  24. }
  25. fd.setWindowTitle(title);
  26. fd.setAcceptMode(mode);
  27. fd.setNameFilters(filters);
  28. if(mode==QFileDialog::AcceptOpen)
  29. {
  30. fd.setFileMode(QFileDialog::ExistingFile);
  31. }
  32. if(fd.exec()==QFileDialog::Accepted)
  33. {
  34. ret = fd.selectedFiles()[0];
  35. QString posix = map[fd.selectedNameFilter()];//把下拉中选中的后缀对应键值取出
  36. if(posix != "*" && !ret.endsWith(posix))
  37. {
  38. ret += posix;
  39. }
  40. }
  41. return ret;
  42. }
  43. void MainWindow::showErrorMessage(QString message)
  44. {
  45. QMessageBox mb(this);
  46. mb.setWindowTitle("Quession");
  47. mb.setText(message);
  48. mb.setIcon(QMessageBox::Critical);
  49. mb.setStandardButtons(QMessageBox::Ok);
  50. mb.exec();
  51. }
  52. int MainWindow::showQuesstionMessage(QString message)
  53. {
  54. QMessageBox mb(this);
  55. mb.setWindowTitle("Error");
  56. mb.setText(message);
  57. mb.setIcon(QMessageBox::Question);
  58. mb.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
  59. return mb.exec();
  60. }
  61. QString MainWindow::saveCurrentData(QString path)
  62. {
  63. QString ret = path;
  64. if(ret == "")
  65. {
  66. ret = showFileDialog(QFileDialog::AcceptSave, "Save");
  67. }
  68. if(ret != "")
  69. {
  70. QFile file(ret);
  71. if(file.open(QIODevice::WriteOnly | QIODevice::Text))
  72. {
  73. QTextStream out(&file);
  74. out << QString(mainEdit.toPlainText());
  75. file.close();
  76. setWindowTitle("NotePad - [" + ret + "]");
  77. m_isTextChanged = false;//保存后修改状态值
  78. }
  79. else
  80. {
  81. showErrorMessage(QString("Open file Error!\n\n") + "\"" + m_filePath + "\"");
  82. ret = "";
  83. }
  84. }
  85. return ret;
  86. }
  87. void MainWindow::preEditorChanged()
  88. {
  89. if(m_isTextChanged)
  90. {
  91. int r = showQuesstionMessage("Do you want to Save?");
  92. switch (r)
  93. {
  94. case QMessageBox::Yes:
  95. saveCurrentData(m_filePath);
  96. break;
  97. case QMessageBox::No:
  98. m_isTextChanged = false;
  99. break;
  100. case QMessageBox::Cancel:
  101. break;
  102. }
  103. }
  104. }
  105. void MainWindow::onFileNew()
  106. {
  107. preEditorChanged();
  108. if(!m_isTextChanged)
  109. {
  110. mainEdit.clear();
  111. m_filePath = "";
  112. m_isTextChanged = false;
  113. setWindowTitle("NotePad-[New]");
  114. }
  115. }
  116. void MainWindow::onFileOpen()
  117. {
  118. preEditorChanged();
  119. if(!m_isTextChanged)
  120. {
  121. QString path = showFileDialog(QFileDialog::AcceptOpen, "open");
  122. if(path != "")
  123. {
  124. QFile file(path);
  125. if(file.open(QIODevice::ReadOnly | QIODevice::Text))
  126. {
  127. mainEdit.setPlainText(QString(file.readAll()));
  128. file.close();
  129. m_filePath = path;//报存当前文件路径
  130. setWindowTitle("NotePad - [" + m_filePath + "]");
  131. }
  132. else
  133. {
  134. showErrorMessage(QString("Open file Error!\n\n") + "\"" + m_filePath + "\"");
  135. }
  136. }
  137. }
  138. }
  139. void MainWindow::onFlieSave()
  140. {
  141. QString path = saveCurrentData(m_filePath);
  142. if(path != "")
  143. {
  144. m_filePath = path;
  145. }
  146. }
  147. void MainWindow::onFileSaveAs()
  148. {
  149. QString path = saveCurrentData();//使用默认参数
  150. if(path != "")
  151. {
  152. m_filePath = path;
  153. }
  154. }
  155. void MainWindow::onTextChanged()
  156. {
  157. if(!m_isTextChanged)
  158. {
  159. setWindowTitle("*" + windowTitle());
  160. }
  161. m_isTextChanged = true;
  162. }
  163. void MainWindow::closeEvent(QCloseEvent *e)//没有对应的信号来处理,只能重写事件处理函数
  164. {
  165. preEditorChanged();
  166. if(!m_isTextChanged)
  167. {
  168. QMainWindow::closeEvent(e);//调用父类的关闭事件处理函数
  169. }
  170. else
  171. {
  172. e->ignore();//点取消的话就忽略这个对话框
  173. }
  174. }
  1. #include <QtGui/QApplication>
  2. #include "MainWindow.h"
  3. #include <QTextCodec>
  4. int main(int argc, char *argv[])
  5. {
  6. QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK")); //路径名支持中文
  7. QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK")); //QString支持中文
  8. QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK")); //string支持中文
  9. QApplication a(argc, argv);
  10. MainWindow* w = MainWindow::NewInstance();
  11. int ret = -1;
  12. if(w != NULL)
  13. {
  14. w->show();
  15. ret = a.exec();
  16. }
  17. delete w;
  18. return ret;
  19. }

四、小结

(1)、事件(QEvent)和信号(SIGNAL)不同

(2)、事件由QObject对象进行处理

(3)、信号由QObject对象触发

(4)、重写事件处理函数可能改变程序行为

(5)、信号的触发不会对程序行为造成影响

(6)、事件处理是在实际工程开发中的应用非常普遍

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/949523
推荐阅读
相关标签
  

闽ICP备14008679号