鱼C论坛

 找回密码
 立即注册
查看: 1997|回复: 0

[技术原创] 【Qt入门教程06】官方入门教程|番外篇

[复制链接]
发表于 2014-12-9 23:57:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ~风介~ 于 2015-11-20 19:58 编辑

code one:
  1. //main.cpp
  2. #include <QtWidgets>

  3. int main(int argc, char *argv[])
  4. {
  5.     QApplication app(argc, argv);

  6.     QTextEdit textEdit;
  7.     textEdit.show();

  8.     return app.exec();
  9. }
复制代码
code two:
  1. //main.cpp
  2. #include <QtWidgets>

  3. int main(int argc, char *argv[])
  4. {
  5.     QApplication app(argc, argv);

  6.     QTextEdit *textEdit = new QTextEdit;
  7.     QPushButton *quitButton = new QPushButton("&Quit");

  8.     QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

  9.     QVBoxLayout *layout = new QVBoxLayout;
  10.     layout->addWidget(textEdit);
  11.     layout->addWidget(quitButton);

  12.     QWidget window;
  13.     window.setLayout(layout);

  14.     window.show();

  15.     return app.exec();
  16. }
复制代码


code three:
  1. //main.cpp
  2. #include <QtWidgets>

  3. class Notepad : public QWidget
  4. {
  5.     Q_OBJECT

  6. public:
  7.     Notepad();

  8. private slots:
  9.     void quit();

  10. private:
  11.     QTextEdit *textEdit;
  12.     QPushButton *quitButton;

  13. };

  14. Notepad::Notepad()
  15. {
  16.     textEdit = new QTextEdit;
  17.     quitButton = new QPushButton(tr("Quit"));

  18.     connect(quitButton, SIGNAL(clicked()), this, SLOT(quit()));

  19.     QVBoxLayout *layout = new QVBoxLayout;
  20.     layout->addWidget(textEdit);
  21.     layout->addWidget(quitButton);

  22.     setLayout(layout);

  23.     setWindowTitle(tr("Notepad"));
  24. }

  25. void Notepad::quit()
  26. {
  27.     QMessageBox messageBox;
  28.     messageBox.setWindowTitle(tr("Notepad"));
  29.     messageBox.setText(tr("Do you really want to quit?"));
  30.     messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  31.     messageBox.setDefaultButton(QMessageBox::No);
  32.     if (messageBox.exec() == QMessageBox::Yes)
  33.         qApp->quit();
  34. }

  35. int main(int argc, char **argv)
  36. {
  37.     QApplication app(argc, argv);

  38.     Notepad notepad;
  39.     notepad.show();

  40.     return app.exec();
  41. }
复制代码
code four:
  1. //main.cpp
  2. #include <QtWidgets>

  3. class Notepad : public QMainWindow
  4. {
  5.     Q_OBJECT

  6. public:
  7.     Notepad();

  8. private slots:
  9.     void load();
  10.     void save();

  11. private:
  12.     QTextEdit *textEdit;

  13.     QAction *loadAction;
  14.     QAction *saveAction;
  15.     QAction *exitAction;

  16.     QMenu *fileMenu;
  17. };

  18. Notepad::Notepad()
  19. {

  20.     loadAction = new QAction(tr("&Load"), this);
  21.     saveAction = new QAction(tr("&Save"), this);
  22.     exitAction = new QAction(tr("E&xit"), this);

  23.     connect(loadAction, SIGNAL(triggered()), this, SLOT(load()));
  24.     connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
  25.     connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

  26.     fileMenu = menuBar()->addMenu(tr("&File"));
  27.     fileMenu->addAction(loadAction);
  28.     fileMenu->addAction(saveAction);
  29.     fileMenu->addSeparator();
  30.     fileMenu->addAction(exitAction);

  31.     textEdit = new QTextEdit;
  32.     setCentralWidget(textEdit);

  33.     setWindowTitle(tr("Notepad"));
  34. }

  35. void Notepad::load()
  36. {

  37. }

  38. void Notepad::save()
  39. {

  40. }

  41. int main(int argc, char **argv)
  42. {
  43.     QApplication app(argc, argv);

  44.     Notepad notepad;
  45.     notepad.show();

  46.     return app.exec();
  47. };
复制代码
code five:
  1. //main.cpp
  2. #include <QtWidgets>

  3. class Notepad : public QMainWindow
  4. {
  5.     Q_OBJECT

  6. public:
  7.     Notepad();

  8. private slots:
  9.     void open();
  10.     void save();

  11. private:
  12.     QTextEdit *textEdit;

  13.     QAction *openAction;
  14.     QAction *saveAction;
  15.     QAction *exitAction;

  16.     QMenu *fileMenu;
  17. };

  18. Notepad::Notepad()
  19. {

  20.     openAction = new QAction(tr("&Load"), this);
  21.     saveAction = new QAction(tr("&Save"), this);
  22.     exitAction = new QAction(tr("E&xit"), this);

  23.     connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
  24.     connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
  25.     connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

  26.     fileMenu = menuBar()->addMenu(tr("&File"));
  27.     fileMenu->addAction(openAction);
  28.     fileMenu->addAction(saveAction);
  29.     fileMenu->addSeparator();
  30.     fileMenu->addAction(exitAction);

  31.     textEdit = new QTextEdit;
  32.     setCentralWidget(textEdit);

  33.     setWindowTitle(tr("Notepad"));
  34. }

  35. void Notepad::open()
  36. {
  37.     QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "",
  38.         tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));

  39.     if (!fileName.isEmpty()) {
  40.         QFile file(fileName);
  41.         if (!file.open(QIODevice::ReadOnly)) {
  42.             QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
  43.             return;
  44.         }
  45.         QTextStream in(&file);
  46.         textEdit->setText(in.readAll());
  47.         file.close();
  48.     }
  49. }

  50. void Notepad::save()
  51. {

  52.     QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "",
  53.         tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));

  54.     if (!fileName.isEmpty()) {
  55.         QFile file(fileName);
  56.         if (!file.open(QIODevice::WriteOnly)) {
  57.             // error message
  58.         } else {
  59.             QTextStream stream(&file);
  60.             stream << textEdit->toPlainText();
  61.             stream.flush();
  62.             file.close();
  63.         }
  64.     }
  65. }

  66. int main(int argc, char **argv)
  67. {
  68.     QApplication app(argc, argv);

  69.     Notepad notepad;
  70.     notepad.show();

  71.     return app.exec();
  72. }
复制代码









想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-13 01:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表