My first program in QT
-
Hi!
I am using QT Creator 2.4.1 based on QT 4.7.4 on Ubuntu 10.04
[1] I created a C++ file with a form.
[2] I drew a button and a text label on the form.
[3] I right-clicked the button and chose "Go to Slot".
[4] At the "Select Signal" Dialogue I chose "clicked()" and "OK"
[5] I the inserted the code snippet :
void MainWindow::on_pushButton_clicked()
{
lineEdit->setText("Hello");
}[6] I then chose "Build Project 'first' "
[7] I received this error:
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:-1: In member function 'void MainWindow::on_pushButton_clicked()':
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: 'lineEdit' was not declared in this scope.[1] Shouldn't QT have taken care of the declaration itself?
[2] If not, where would i have to declare lineEdit and would I also have to declare the pushbutton also?I'm used to C++ builder and Delphi where all that is taken care of automatically for such a simple program.
QT is obviously a quality program and I know I will enjoy working with it. I am not a proficient programmer in C++ and this is my first foray into QT. The signals and slots are confusing. I don't know why the developer chose to go the confusing java route instead of continuing with properties methos, and functions as in C++ :( Proprietary maybe?
Please help. Thanks,
Paul
I should add that I have tried googling for simple tuts also I tried to follow an example in the help but they were to complicated for a first forms program and had to much extraneous widgets and such. -
Whew! I got a bunch of errors that time! Here they are:
@/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\342' in program/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\200' in program
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\234' in program
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\342' in program
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\200' in program
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\235' in program
And last but not least:
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:-1: In member function 'void MainWindow::on_pushButton_clicked()':
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: 'Hello' was not declared in this scope@I think I know why you added the ui: to reference the form?
-
[quote author="p3aul" date="1337358650"]
[1] Shouldn't QT have taken care of the declaration itself?
[2] If not, where would i have to declare lineEdit and would I also have to declare the pushbutton also?
[/quote]You can always try to understand the code using your C++ knowledge.
For a Normal C++ application:
Source files:
- *.cpp
- *.h
can be compiled by your C++ compiler, then linked by your linker.
While a Normal Qt application:
Source files:
-
x.cpp
-
x.h
-
x.h [Contains Q_OBJECT] ==> moc_x.cpp (generated by moc)
-
x.ui ==> ui_x.h (generated by uic)
-
x.rcc ==> qrc_x.cpp (generated by rcc)
Then all you get is normal *.cpp and .h files now.
All the *.cpp and *.h files can be compiled by your C++ compiler, then linked by your linker.
-
If you are used to C++ builder and Delphi, you can easily solve such issues, do you?
Perhaps you copid some code from network which contains invalid C/C++ characters.
[quote author="p3aul" date="1337361431"]Whew! I got a bunch of errors that time! Here they are:
@/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\342' in program/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\200' in program
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\234' in program
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\342' in program
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\200' in program
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: stray '\235' in program
And last but not least:
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:-1: In member function 'void MainWindow::on_pushButton_clicked()':
/home/owner/qt/first-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../first/mainwindow.cpp:18: error: 'Hello' was not declared in this scope@I think I know why you added the ui: to reference the form?[/quote]
-
I just don't understand all this. My C++ builder coding was years ago. but I could do a simple app like this in under a minute. All I had to do was double-click the button, write that code snippet between the {} symbols and choose build project. compile and the choose run. It was that simple.
This is as simple as it gets. one label one pushbutton. why can't I set the property with lineEdit->setText("Hello")? is this even the right syntax? -
[quote author="p3aul" date="1337363837"]
why can't I set the property with lineEdit->setText("Hello")? is this even the right syntax?
[/quote]As I said before, you can always try to understand the code using your C++ knowledge as this is normal C++ code and there is no magic.
Whether you can use "lineEdit" depends in which way you defined the variable.
- Is a global variable
- Is a class member variable
- Is a member variable inherited from the parent class.
- Is a local variable
- ...
If you find it's hard to understand, maybe a C++ book will be useful for you.
Debao
-
From what you said, I guess you know the answer, but for your own reasons don't wish to reveal it. I see by observing parts of code in more complicated examples that my syntax is correct. Unfortunately, QT is not standard C++. If it was there wouldn't be this foolishness of Slots and Signals instead they would use Methods and Events as in standard C++. . I'm either going to have to get some answers either by waiting and hoping someone else will pitch in and help me or by creating another thread. I asked a simple question. Why can't I get a simple, complete answer. Is knowledge of QT hoarded and only given out bit by bit? I have a book by the way. It's just not a book on QT
Thanks,
Paul -
[quote author="p3aul" date="1337383959"]From what you said, I guess you know the answer, but for your own reasons don't wish to reveal it. I see by observing parts of code in more complicated examples that my syntax is correct.
Paul[/quote]
Sorry.And Sorry for my poor English.
As C++ builder is a IDE of C++, and you said that you used to C++ builder, so I wrongly thought that you will be very familiar with C++, but seems you not.
Qt is a library of C++, it isn't a good idea to deep into the Qt world before familiar with C++. Perhaps “C++ Primer” is a good book to start with.
bq. Unfortunately, QT is not standard C++. If it was there wouldn’t be this foolishness of Slots and Signals instead they would use Methods and Events as in standard C++.
It depends.
All the “non-standard” sources will be pre-processed by moc, uic, rcc, and then all you get are C++ “standard” files.
No matter what happened, your Qt application will be compiled by a “standard” C++ compiler such as GCC, MSVC, Intel-C++, Clang, ..., even C++ builder.
-
Main Window .cpp
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
lineEdit->setText("Hello");
}
@Errors:
/home/owner/qt/newattempt-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../newattempt/mainwindow.cpp:-1: In member function 'void MainWindow::on_pushButton_clicked()':
/home/owner/qt/newattempt-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../newattempt/mainwindow.cpp:18: error: 'lineEdit' was not declared in this scopeThis error about lineEdit seems to indicate that I need to declare it somewhere in mainwindow.cpp I don't know if this just the tip of the iceberg though. Once that is corrected others may show up. :(
-
[quote author="Wilk" date="1337390529"]Well, the simpliest solution is to declare a signal like sendText (QString) and connecting it with slot of your QLineEdit like setText, and then emit it in your on_pushButton_clicked () slot.[/quote]
It's an answer, but not the simpliest ;-) .The right answer have been given by VanDerSam. And I try to given reasons, but failed ;-(
-
[quote author="p3aul" date="1337390576"]Robot Herder: I have an excellent book that came with C++ builder version 3. The syntax is correct. In Builder I didn't need to add declarations of Components(Widgets) because Builder added them automatically.[/quote]
If I am right, you never try to see the code behind the drag and drop operation when using C++ builder.
-
Wilk:
@Well, the simpliest solution is to declare a signal like sendText (QString) and connecting it with slot of your QLineEdit like setText, and then emit it in your on_pushButton_clicked () slot.@
That's a perfect example of what I'm talking about. What you wrote is NOT C++ it's Qt! I need a book on Qt, not C++!If that's the simplest solution than I need to go back to windows and CodeGear or Visual C++
-
The fact is that VanDerSam's solution makes your program dependent on UI and actual names of it's components, while you can change UI itself and/or it elements and/or names of UI elements. While my solution makes your application dependent only on interface of class.
-
Robot Hearder:
@If I am right, you never try to see the code behind the drag and drop operation when using C++ builder.@Well yes, except for the code snippets you write your self. Let's face it, I'm no purist! I just want to get the job done, and Qt, like C++ builder is a RAD tool. Otherwise you would all just use a text editor and code it all for the fun of it.
-
[quote author="p3aul" date="1337391158"]Wilk:
@Well, the simpliest solution is to declare a signal like sendText (QString) and connecting it with slot of your QLineEdit like setText, and then emit it in your on_pushButton_clicked () slot.@
That's a perfect example of what I'm talking about. What you wrote is NOT C++ it's Qt! I need a book on Qt, not C++!If that's the simplest solution than I need to go back to windows and CodeGear or Visual C++ [/quote]
You're free do do what you want, but Qt is better at least because it's cross platform. If you don't want to learn something new it totally your problem.
P.S. Use "quote" function instead of code blocks highlighting for quotes. -
;-) Seems you don't know which problem encountered and complained by p3aul.
What he need is a C++ book, no matter which GUI library he want to use.
- Qt
- MFC
- wxWidgets
- ...
And which C++ IDE he want to selected
- Visual Studio
- C++ builder
- Ellipse
- Qt Creator
- ...
And which C++ compiler he want to choose
- cl.exe (MSVC)
- g++.exe
- clang.exe
- ....
[quote author="Wilk" date="1337391395"]The fact is that VanDerSam's solution makes your program dependent on UI and actual names of it's components, while you can change UI itself and/or it elements and/or names of UI elements. While my solution makes your application dependent only on interface of class.[/quote]