Incompatibility between the IDE and the book I read to learn Qt5
-
Hello dear guys,
I'm really disappointed :-(
I do my best to learn Qt5 but apparently I have no good choice :-(We had a massive discussion here about the documentations and other resources. I then passed primary steps and became ready to read a good book. I installed Qt Creator 3.6.0 as the (latest Qt) IDE and took the book C++-GUI-Programming-with-Qt-4-2nd-Edition as the reference to have a real start on learning Qt5.
The problem is that, following the instructions written in the book and doing them on that IDE, brings many difficulties. There are many incompatibilities between what I read and what I should do.
Since I really love Qt, I had (and have) to learn Qt5. So I did whatever I could to match the IDE and the book to be able to proceed in it.
There of course are some interchanges that are simple to do. Like replacing
<QtGui>
with<QtWidgets>
(on my IDE) and don't doing the qmake from a command line (it seems not to be needed for that IDE).I can cope with these minor differences but please read this link which is a PDF file containing a project of that book.
I really don't know how to implement it on my IDE. I spent hours to solve the differences but they are not one or two ones.
As well as, I fear to face such confusions again and again; some time later when working, and have to create threads here for helping every time. This will not be a good method for learning.
I should learn Qt5. This is my principle. Please help me.
PS:
- I have good experience in C++.
- My machine is MS Windows 7.
- My primary purpose is being able to create apps for Windows.
-
@tomy
Did you have a look from when the book is?The book is targeting Qt 4 and also a very old version of Qt creator. No wonder that nothing is really matching anymore. Unfortunately, there is apparently nobody interested in updating or reqwriting a book for Qt 5. I guess no publisher sees enough money in it.
However, you can use Qt 4 together with newest IDE. The examples shall be compatible with the book.
You may even use the old Qt creator and the Qt version as used for the book. All versions shall be available in the archive.The changes from Qt 4 to 5 are minimal. There are typically only a few too adjust.
-
Yes, I knew that the book is really old.
I never change my IDE version because I don't like to work on an old product when there is a better new one. Pardon me for my being that frank.
I need to be familiar with the differences progressively.What about that project please? How to implement it on my IDE?
Thanks so much for your comments.
-
As far as I could, I figured out the changes and wrote the followings.
First I designed this form. Then wrote the following codes:The
gotocell.h
file:#ifndef GOTOCELL_H #define GOTOCELL_H #include <QDialog> class QLineEdit; class QLabel; class QPushButton; class QSpaceItem; namespace Ui { class GoToCell; } class GoToCell : public QDialog { Q_OBJECT public: explicit GoToCell(QWidget *parent = 0); ~GoToCell(); private slots: void on_LineEdit_TextChanged(); private: Ui::GoToCell *ui; QPushButton* okButton; QPushButton* cancelButton; QSpaceItem* spacerItem; QLineEdit* lineEdit; QLabel* label; }; #endif // GOTOCELL_H
The
gotocell.cpp
file:#include "gotocell.h" #include "ui_gotocell.h" #include <QtWidgets> GoToCell::GoToCell(QWidget *parent) : QDialog(parent), ui(new Ui::GoToCell) { ui->setupUi(this); QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}"); lineEdit -> setValidator(new QRegExpValidator(regExp, this)); connect(lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(on_LineEdit_TextChanged())); connect(okButton, SIGNAL(clicked()), this, SLOT(accept())); connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject())); } //--------------------------------- void GoToCell::on_LineEdit_TextChanged(){ okButton ->setEnabled(lineEdit->hasAcceptableInput()); } //--------------------------- GoToCell::~GoToCell() { delete ui; }
And the
main.cpp
file:#include "gotocell.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); GoToCell* gtc = new GoToCell; gtc -> show(); return app.exec(); }
After running, the program crashes and shows the message below:
QMetaObject::connectSlotsByName: No matching signal for on_LineEdit_TextChanged()Any idea on my main problem on this thread or about this error please?
-
Hi,
lineEdit
is not initialized nor is okButton andon_LineEdit_TextChanged
uses the format for automatic connection used by the widgets built with designer. The error message means that your Designer based UI doesn't contain any QLineEdit matching the name LineEdit. -
I've been working through this tutorial .. I note that you have misnamed "cancelButton" to "cancleButton" in Qt Creator screenshot.
-
Hi @tomy
Rename your slot (//on_LineEdit_TextChanged()//), it works.This is because, when you create setupUI() function, which was created at the time of Dialog/window using QtDesinger. And it does the search function/slot basing on name like "on_widgetName_eventNaeme()" . But if you make some own slot/function similar like this starting with" on_" prefix like what you did in your case "on_LineEdit_TextChanged()" ,which is not related directly to widgetname with valid event name, connect() fails.
So when you make own connection or slot, you shouldn't USE the "on_" prefix for the slot's name (to avoid confusion)because connectSlotsByName is also called at the end of setupUi which would again throw the warning/error that there is nNo matching signal for on_LineEdit_TextChanged()/slot.
-
Hi SGaist, thanks for your explanation.
lineEdit
is not initialized nor is okButtonBut I used them and even changed their properties in the design mode!
and
on_LineEdit_TextChanged
uses the format for automatic connection used by the widgets built with designer.I don't exactly know what "the format for automatic connection" means but I think you meant, since I haven't initialize, say, the lineEdit (this way:
lineEdit = new QLineEdit;
), so the program uses the one I made with the designer. If so, OK, this is what I want. That is, I want the works I have done on the designer mode to be used not useless.The error message means that your Designer based UI doesn't contain any QLineEdit matching the name LineEdit.
Why should it match the name LineEdit please?
I haven't used the name LineEdit in my codes. I've used lineEdit which exists in designer form. If you meant that I have used LineEdit in theon_LineEdit_TextChanged()
function/slot, according to my experience on C++, the names of the user defined functions has nothing to do with the works they do.PS: I even changed the name of that slot to
on_lineEdit_TextChanged()
. The same result.@eureka said:
I've been working through this tutorial .. I note that you have misnamed "cancelButton" to "cancleButton" in Qt Creator screenshot.
I corrected it, thanks. :-)
@Ni.Sumi said:
Hi @tomy
Rename your slot (//on_LineEdit_TextChanged()//), it works.Thank you for your explanations. I tried to understand them.
Please read my comment on the name of functions/slots above. And that, I even changed the name of that slot. -
So lineEdit belongs to the UI created with Designer, you should access it using
ui->lineEdit
. What you are currently using is a raw pointer pointing to nothing.The automatic connection and function naming scheme has nothing to do with core C++ principles. You should take a look at the Designer manual. Things like automatic connection and how it works is explained there (what @Ni-Sumi already wrote with a bit more practical examples)
-
There are some great guides on what's new in Qt5 and how to transition to Qt5. I suggest finding those and reading them over. There's really not too much you need to know. The most important thing to do first IMO is to learn the newer Signal and Slot syntax. I find it to be much more powerful than the old way, plus it's much more efficient. You should make a habit of replacing the old Signal and Slot code with the new style. Here is a guide for transitioning from Qt4 to Qt5. You should get the hang of it after a while. I managed to do so using the resources available to me. It's not ideal to learn an older framework and adapt it to the newer one, but that's just what you'll have to do for now. Luckily, there's all kinds of resources (like examples and tutorials) all over the Web using Qt5, so it's not too hard to pick it up. Good luck on your Qt journey. I've only been using it for a few months and I've grown to like it more than any other framework. It starts to grow on you and things just start making sense. It's so consistent that you'll be using classes without ever reading the documentation.
-
@SGaist said:
So lineEdit belongs to the UI created with Designer, you should access it using
ui->lineEdit
. What you are currently using is a raw pointer pointing to nothing.The automatic connection and function naming scheme has nothing to do with core C++ principles. You should take a look at the Designer manual. Things like automatic connection and how it works is explained there (what @Ni-Sumi already wrote with a bit more practical examples)
I found this in Designer manual which seems to be fine and useful (especially for a beginner), but, and just as usual, there are imperfect parts in it!
I was precisely reading the explanations until arrived at the Calculator Form point. But neither there nor in Calculator Form's page, the explanations are complete!For example, please look at the that Calculator Form's page:
-
It doesn't specify what type of the forms I need to create using the Qt creator!
(I guess I should create one from File -> New File or Project -> Qt -> Qt Designer Form -> Widget) -
In the next part, it says:
"To ensure that the example can use this file, we need to include a FORMS declaration in the example's project file:
FORMS = calculatorform.ui"
I think it means that I should type the above statement inside a project file (here, the calculatorform .pro). But creating a form using the way I mentioned just create a .ui file, "form.ui", and not any other file (for example, a .pro file)!
Seemingly, if I can create such a .pro file and put that statement into it, I should do some action (building it using the Build button), so that the program will produce a header file, then, for proceeding the track.
-
-
@JordanHarris
Hello,The most important thing to do first IMO is to learn the newer Signal and Slot syntax. I find it to be much more powerful than the old way, plus it's much more efficient.
Indeed, this:
connect(mySpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), mySlider, &QSlider::setValue);
instead of this:
connect(mySpinBox, SIGNAL(valueChanged(int)), mySlider, SLOT(setValue(int));
is really simpler and much more beautiful ...
Regarding efficiency, well that point is simply moot. Quite irrelevant, if I may add, unless you can prove that the bottelneck for an application are the signal-slot invocations, which I don't believe you can.
Kind regards.
-
If I can, I state my opinion upon the other subjects later. But what is important for me at this point is to move forward along the Qt's path. Please read my previous post. You all have experienced these cases and are able to cube with them.
If possible, please guide me on what are questions for me. -
@tomy
Right, sorry for the sarcasm put and the somewhat off-topic-ish comment, as it doesn't really relate to your question. I don't really auto-connect my signals and slots and I believe it to be a terrible idea in the first place. This tutorial seems somewhat useful. Taking it from the top:- The
.pro
file is the definition of your project encapsulating the names of the source files to be compiled, the headers, the resources, forms, linker flags and compiler flags and other Qt (and non-Qt) related stuff. The file is needed byqmake
, which will read the file and retranslate it into a makefile that the compiler can use to build your project. - The
.ui
file is an XML file that is created by the designer (integrated into QtCreator) and which contains the definitions of your forms. Whenqmake
is preparing your project for building, it will runuic
(the user interface compiler) to read the form and create a form header that you can include in your source to access the form.
Suppose you have a form that is for a widget (it's pretty much the same for a dialog or a main window).
The user interface compiler will create the file "ui_moduletoolboxpage.h" from the "moduletoolboxpage.ui" file. This header contains:
/******************************************************************************** ** Form generated from reading UI file 'moduletoolboxpage.ui' ** ** Created by: Qt User Interface Compiler version 5.3.0 ** ** WARNING! All changes made in this file will be lost when recompiling UI file! ********************************************************************************/ #ifndef UI_MODULETOOLBOXPAGE_H #define UI_MODULETOOLBOXPAGE_H #include <QtCore/QVariant> #include <QtWidgets/QAction> #include <QtWidgets/QApplication> #include <QtWidgets/QButtonGroup> #include <QtWidgets/QHeaderView> #include <QtWidgets/QVBoxLayout> #include <QtWidgets/QWidget> QT_BEGIN_NAMESPACE class Ui_ModuleToolboxPage { public: QVBoxLayout *verticalLayout; void setupUi(QWidget *ModuleToolboxPage) { if (ModuleToolboxPage->objectName().isEmpty()) ModuleToolboxPage->setObjectName(QStringLiteral("ModuleToolboxPage")); ModuleToolboxPage->resize(719, 384); verticalLayout = new QVBoxLayout(ModuleToolboxPage); verticalLayout->setObjectName(QStringLiteral("verticalLayout")); retranslateUi(ModuleToolboxPage); QMetaObject::connectSlotsByName(ModuleToolboxPage); } // setupUi void retranslateUi(QWidget *ModuleToolboxPage) { ModuleToolboxPage->setWindowTitle(QApplication::translate("ModuleToolboxPage", "Form", 0)); } // retranslateUi }; namespace Ui { class ModuleToolboxPage: public Ui_ModuleToolboxPage {}; } // namespace Ui QT_END_NAMESPACE #endif // UI_MODULETOOLBOXPAGE_H
The class name comes from the object name:
class Ui_ModuleToolboxPage
is named after the root object, which in my case (see the picture) is namedModuleToolboxPage
. And this line:QMetaObject::connectSlotsByName(ModuleToolboxPage);
connects slots automatically depending on the slot's name.
To use that form, you include this generated header (remember, it's generated automatically from
qmake
/uic
when the form file is registered in the project file like this:FORM += moduletoolboxpage.ui
, and this is done automatically as well when you create it in QtCreator). So suppose I have a dialog I want to initialize with this form, I'd do something like this:#include "ui_moduletoolboxpage.h" //< Note that you're including the generated header class MyDialog : public QDialog { Q_OBJECT public: MyDialog(QWidget * parent = NULL); private: Ui::ModuleToolboxPage ui; //< This is the object that represents the form created in the designer } MyDialog::MyDialog(QWidget * parent) : QDialog(parent) { ui.setupUi(this); //< This line creates all the objects described in the form and attaches them to my dialog. It initializes my dialog with my form }
Well this got a bit out of hand with all the code and the image, but I hope it's helpful.
Kind regards.
- The
-
Thank you very much for you comprehensive reply.
Before doing them, are those tutorials applicable on my Qt Creator 3.6.0?
What is I face incompatibilities between them?As you may know, I'm reading the book C++ GUI Programming With Qt 4 2nd Edition but since that book was confusing me I had to get helps here in forums.
Do you suggest that I finish these tuts and then get back to the book? -
@tomy
Hello,Before doing them, are those tutorials applicable on my Qt Creator 3.6.0?
I don't see a reason why they wouldn't be. The screenshot and the generated header I've taken from my own project, and I'm using QtCreator 3.6, so they're very up to date.
Do you suggest that I finish these tuts and then get back to the book?
No, I've sourced them as "further reading" that should help you with your book and your projects.
Kind regards.
-
Hello dear kshegunov and thanks for your talks.
I appreciate your suggestions and I will certainly try to do them in near future, but what I need now is something else.
Please read my above post (the one that I've bookmarked it) and try to answer my questions there.
After that part and the other, I understand some main topics and then continue to reading my book and also I do the tuts you helped me by introducing them.
Thanks again. -
@tomy
You might be the only one seeing the bookmark, AFAIK it is local.
Therefore probably nobody is going find and answer your specific question.Even though it is not strongly encouraged in this forum, I suggest that you are starting a new thread with a title already giving an understanding of your problems.
6/18