首页 > 代码库 > Qt5中的QtGui

Qt5中的QtGui

我在学习Qt查看Qt Creater提供的例子时,遇到了一个小问题。就是明明在代码中包含了QtGui,然而编译的时候还是提示找不到QLabel的定义,以及其他一些类的定义,但是这是官方提供的文档的啊,不应该没通过编译就提供吧,所以就想肯定是自己哪里出了问题,在网上搜了一下果然,归根到底还是版本问题吧,提供的文档估计是版本qt4的,而我自己使用的是qt5,它两个之间的一个区别就是Qt5把关于控件的头文件都移到 <QtWidgets>中了,所以如果在 Qt5 中使用控件应该包含 <QtWidgets>而非<QtGui>。

  1 /****************************************************************************  2  **  3  ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).  4  ** Contact: http://www.qt-project.org/legal  5  **  6  ** This file is part of the examples of the Qt Toolkit.  7  **  8  ** $QT_BEGIN_LICENSE:BSD$  9  ** You may use this file under the terms of the BSD license as follows: 10  ** 11  ** "Redistribution and use in source and binary forms, with or without 12  ** modification, are permitted provided that the following conditions are 13  ** met: 14  **   * Redistributions of source code must retain the above copyright 15  **     notice, this list of conditions and the following disclaimer. 16  **   * Redistributions in binary form must reproduce the above copyright 17  **     notice, this list of conditions and the following disclaimer in 18  **     the documentation and/or other materials provided with the 19  **     distribution. 20  **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names 21  **     of its contributors may be used to endorse or promote products derived 22  **     from this software without specific prior written permission. 23  ** 24  ** 25  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26  ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 36  ** 37  ** $QT_END_LICENSE$ 38  ** 39  ****************************************************************************/ 40  41 #include <QtWidgets> 42 #include <QTranslator> 43 #include <QLocale> 44 #include <QLibraryInfo> 45  46  47  QWizardPage *createIntroPage() 48  { 49      QWizardPage *page = new QWizardPage; 50      page->setTitle("Introduction"); 51  52      QLabel *label = new QLabel("This wizard will help you register your copy " 53                                 "of Super Product Two."); 54      label->setWordWrap(true); 55  56      QVBoxLayout *layout = new QVBoxLayout; 57      layout->addWidget(label); 58      page->setLayout(layout); 59  60      return page; 61  } 62  63  QWizardPage *createRegistrationPage() 64  { 65      QWizardPage *page = new QWizardPage; 66      page->setTitle("Registration"); 67      page->setSubTitle("Please fill both fields."); 68  69      QLabel *nameLabel = new QLabel("Name:"); 70      QLineEdit *nameLineEdit = new QLineEdit; 71  72      QLabel *emailLabel = new QLabel("Email address:"); 73      QLineEdit *emailLineEdit = new QLineEdit; 74  75      QGridLayout *layout = new QGridLayout; 76      layout->addWidget(nameLabel, 0, 0); 77      layout->addWidget(nameLineEdit, 0, 1); 78      layout->addWidget(emailLabel, 1, 0); 79      layout->addWidget(emailLineEdit, 1, 1); 80      page->setLayout(layout); 81  82      return page; 83  } 84  85  QWizardPage *createConclusionPage() 86  { 87      QWizardPage *page = new QWizardPage; 88      page->setTitle("Conclusion"); 89  90      QLabel *label = new QLabel("You are now successfully registered. Have a " 91                                 "nice day!"); 92      label->setWordWrap(true); 93  94      QVBoxLayout *layout = new QVBoxLayout; 95      layout->addWidget(label); 96      page->setLayout(layout); 97  98      return page; 99  }100 101  int main(int argc, char *argv[])102  {103      QApplication app(argc, argv);104 105      QString translatorFileName = QLatin1String("qt_");106      translatorFileName += QLocale::system().name();107      QTranslator *translator = new QTranslator(&app);108      if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))109          app.installTranslator(translator);110 111      QWizard wizard;112      wizard.addPage(createIntroPage());113      wizard.addPage(createRegistrationPage());114      wizard.addPage(createConclusionPage());115 116      wizard.setWindowTitle("Trivial Wizard");117  #ifdef Q_OS_SYMBIAN118      wizard.showMaximized();119  #else120      wizard.show();121  #endif122 123      return app.exec();124  }

然后程序就顺利运行了。