Results 1 to 7 of 7

Thread: Qt4 Books Guide problem, cant run programs on Qt5

  1. #1
    Join Date
    Jun 2013
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Question Qt4 Books Guide problem, cant run programs on Qt5

    i downloaded a book about qt programming but its outdated because its version is something like qt4. while i got the latest Qt5.0.2 creator and compiler which i downloaded from qt-project.org/downloads. I've read from chapter 1 and everything seems to work fine but when i reached chapter 2, which includes header, thebody and the main, i received a lot of errors so here is my code...im running windows

    in the creator i clicked--File->New Project->Other Project->Empty Qt Project->then i named it finddialog->next2x->Finished

    then in the project named finddialog i right clicked it->Add New->C++ Source File->i named it main.cpp->then finished.

    then in the project named finddialog again i right clicked it->Add New->C++ Class->class name: finddialog, base class: i chose QWidget->then Finished.

    so here is the code in the header file...
    Qt Code:
    1. #ifndef FINDDIALOG_H
    2. 2 #define FINDDIALOG_H
    3.  
    4. 3 #include <QDialog>
    5.  
    6. 4 class QCheckBox;
    7. 5 class QLabel;
    8. 6 class QLineEdit;
    9. 7 class QPushButton;
    10. 8 class FindDialog : public QDialog
    11. 9 {
    12. 10 Q_OBJECT
    13.  
    14. 11 public:
    15. 12 FindDialog(QWidget *parent = 0);
    16. 13 signals:
    17. 14 void findNext(const QString &str, Qt::CaseSensitivity cs);
    18. 15 void findPrevious(const QString &str, Qt::CaseSensitivity cs);
    19. 16 private slots:
    20. 17 void findClicked();
    21. 18 void enableFindButton(const QString &text);
    22.  
    23. 19 private:
    24. 20 QLabel *label;
    25. 21 QLineEdit *lineEdit;
    26. 22 QCheckBox *caseCheckBox;
    27. 23 QCheckBox *backwardCheckBox;
    28. 24 QPushButton *findButton;
    29. 25 QPushButton *closeButton;
    30. 26 };
    31.  
    32. 27 #endif
    To copy to clipboard, switch view to plain text mode 

    then in the finddialog.cpp here is the code
    Qt Code:
    1. 1 #include <QtGui>
    2.  
    3. 2 #include "finddialog.h"
    4. 3 FindDialog::FindDialog(QWidget *parent)
    5. 4 : QDialog(parent)
    6. 5 {
    7. 6 label = new QLabel(tr("Find &what:"));
    8. 7 lineEdit = new QLineEdit;
    9. 8 label->setBuddy(lineEdit);
    10.  
    11. 9 caseCheckBox = new QCheckBox(tr("Match &case"));
    12. 10 backwardCheckBox = new QCheckBox(tr("Search &backward"));
    13.  
    14. 11 findButton = new QPushButton(tr("&Find"));
    15. 12 findButton->setDefault(true);
    16. 13 findButton->setEnabled(false);
    17.  
    18. 14 closeButton = new QPushButton(tr("Close"));
    19.  
    20. 15 connect(lineEdit, SIGNAL(textChanged(const QString &)),
    21. 16 this, SLOT(enableFindButton(const QString &)));
    22. 17 connect(findButton, SIGNAL(clicked()),
    23. 18 this, SLOT(findClicked()));
    24. 19 connect(closeButton, SIGNAL(clicked()),
    25. 20 this, SLOT(close()));
    26. 21 QHBoxLayout *topLeftLayout = new QHBoxLayout;
    27. 22 topLeftLayout->addWidget(label);
    28. 23 topLeftLayout->addWidget(lineEdit);
    29.  
    30. 24 QVBoxLayout *leftLayout = new QVBoxLayout;
    31. 25 leftLayout->addLayout(topLeftLayout);
    32. 26 leftLayout->addWidget(caseCheckBox);
    33. 27 leftLayout->addWidget(backwardCheckBox);
    34.  
    35. 28 QVBoxLayout *rightLayout = new QVBoxLayout;
    36. 29 rightLayout->addWidget(findButton);
    37. 30 rightLayout->addWidget(closeButton);
    38. 31 rightLayout->addStretch();
    39.  
    40. 32 QHBoxLayout *mainLayout = new QHBoxLayout;
    41. 33 mainLayout->addLayout(leftLayout);
    42. 34 mainLayout->addLayout(rightLayout);
    43. 35 setLayout(mainLayout);
    44. 36 setWindowTitle(tr("Find"));
    45. 37 setFixedHeight(sizeHint().height());
    46. 38 }
    47. 39 void FindDialog::findClicked()
    48. 40 {
    49. 41 QString text = lineEdit->text();
    50. 42 Qt::CaseSensitivity cs =
    51. 43 caseCheckBox->isChecked() ? Qt::CaseSensitive
    52. 44 : Qt::CaseInsensitive;
    53. 45 if (backwardCheckBox->isChecked()) {
    54. 46 emit findPrevious(text, cs);
    55. 47 } else {
    56. 48 emit findNext(text, cs);
    57. 49 }
    58. 50 }
    59.  
    60. 51 void FindDialog::enableFindButton(const QString &text)
    61. 52 {
    62. 53 findButton->setEnabled(!text.isEmpty());
    63. 54 }
    To copy to clipboard, switch view to plain text mode 

    and the last one in is the main.cpp here it is....
    Qt Code:
    1. 1 #include <QWidgets/QApplication>
    2.  
    3. 2 #include "finddialog.h"
    4.  
    5. 3 int main(int argc, char *argv[])
    6. 4 {
    7. 5 QApplication app(argc, argv);
    8. 6 FindDialog *dialog = new FindDialog;
    9. 7 dialog->show();
    10. 8 return app.exec();
    11. 9 }
    To copy to clipboard, switch view to plain text mode 

    When run and compile this i got 46 errors.
    In the .pro file i did type this QT += widgets

    Did i miss something more important? The codes the i entered above will surely works on qt4 but im currently running the latest Qt5.0.2 packaged which i thought will not give me problems...

  2. #2
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Qt4 Books Guide problem, cant run programs on Qt5

    include
    Qt Code:
    1. #include<QtWidgets>
    To copy to clipboard, switch view to plain text mode 

    instead of
    Qt Code:
    1. #include <QtGui/QWidget>
    To copy to clipboard, switch view to plain text mode 

    and omit what you added in the .pro file.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt4 Books Guide problem, cant run programs on Qt5

    @saman_artorious a better way is to avoid including the module and include a the header for each needed class and he shouldn't omit the QT += widgets from .pro file, that is needed to add the library to linkage stage (so the module into the include lines is not enough to replace the adding to QT into the qmake project file)

    @hot_tea see this documentation page, basically the classes derived from QWidget are moved into widgets module so the #include <QtGui> wont include headers for the classes you need, to solve this problem include the headers of the widgets you need not the module (and if you correctly add QT += widgets into the .pro file the source will become compilable with both Qt4 and Qt5)
    So into the finddialog.cpp replace #include <QtGui>
    Qt Code:
    1. //#include <QtGui> - don't use this
    2. #include <QCheckBox> //include header for each individual qt class you use
    3. #include <QLabel>
    4. #include <QLineEdit>
    5. #include <QPushButton>
    6. #include "finddialog.h"
    To copy to clipboard, switch view to plain text mode 

    And also when you ask for help please tell us the errors you get, usually we can't compile your code, so if there are more issues tell us the errors

  4. #4
    Join Date
    Jun 2013
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt4 Books Guide problem, cant run programs on Qt5

    Ok so i've tried what you've said and the errors lessened, so i decided to take pictures about my problem, here it is hope you can help me now..

    this is in the headerheader.jpg


    this is in the finddialog.cpp Top portion: finddialogTOP.jpg


    this is the continuation of finddialog.cpp Bottom portion: finddialogBOTTOM.jpg


    and then the main.cpp which includes the errors: main.jpg

    hope you can help me....

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt4 Books Guide problem, cant run programs on Qt5

    Most of the errors are related to the missing includes for headers for QVBoxLayout and QHBoxLayout so the resolution is to add those two includes in finddialog.cpp

    And as for the one related to the case sensitivity in findClicked() function see if you didn't modified something there, because the code there (the code from your first post) seems correct.

  6. #6
    Join Date
    Jun 2013
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt4 Books Guide problem, cant run programs on Qt5

    yes it works now, i've included the QHBoxLayout and QVBoxLayout in the finddialog.cpp then i guess i have typos in case sensivity area anyway i got it all work now with your help and thank you again,

    by the way before i continue my reading in qt book, do you have any more tips or suggestion so i cant ask again cuz i might bother someone again which is not good for the busy people around working for the better tomorrow...I mean as long as i add this QT += widgets in the .pro file, ill be running both qt 4 and 5, then also i'll goind to read the link you gave to me then nothing more.....



    case Solved

  7. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt4 Books Guide problem, cant run programs on Qt5

    Quote Originally Posted by hot_tea View Post
    ...by the way before i continue my reading in qt book, do you have any more tips or suggestion so i cant ask again cuz i might bother someone again which is not good for the busy people around working for the better tomorrow...
    You are not bothering us, my suggestion was only to tell us information about the error you get, because we can't guess and usually as long as we just read some pieces of code we might overlook some common simple to fix errors that we can answer very quickly if you tell the error in the first post.
    And as a suggestion i have only an advice to you: learn a little more C++, i don't want to be rude but it seems C++ is quite a new language for you and if you learn C++ in the same time with Qt it might seem overwhelming - and it might take all the fun out of Qt as a book suggestion i say you search for "Thinking in C++" and you will find two volumes that are free to download.
    Quote Originally Posted by hot_tea View Post
    ...I mean as long as i add this QT += widgets in the .pro file, ill be running both qt 4 and 5, then also i'll goind to read the link you gave to me then nothing more.....
    Well, not really... that will give an error on Qt4, because Qt4 doesn't have the widgets module, but you can use this line: greaterThan(QT_MAJOR_VERSION, 4): QT += widgets in the .pro file, and that will add the widgets module only if Qt major version is higher than 4 so it won't add it for Qt4, it will add it only for Qt5.

    There are other things that changed with version 5, some are in the link that i gave you in my first post and also there are some things where the recommended usage has changed, an example is QThread, in the book you might find a class that is derived from a QThread and run method is overridden, while the recommended usage is presented in the documentation, see here - another recommendation is to use the book to get to know Qt and the basic concepts like: widgets and layouts, parent-child,signals and slots, implicit sharing, but don't forget to use the documentation too, especially because the book about Qt are a little old (don't get me wrong, those are still good but they need a little bit of documentation studying to get the new functionality or the new recommended way to use a functionality)

Similar Threads

  1. Replies: 2
    Last Post: 26th November 2012, 08:09
  2. Replies: 4
    Last Post: 13th November 2012, 21:46
  3. Some QT books
    By sophister in forum Qt Programming
    Replies: 6
    Last Post: 10th May 2009, 13:31
  4. Problem with a UDP connection between two programs
    By fraubrj in forum Qt Programming
    Replies: 8
    Last Post: 10th January 2009, 13:54
  5. Problem about timer in multithread programs
    By vql in forum General Programming
    Replies: 4
    Last Post: 17th October 2007, 15:00

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.