My first program in QT
-
I'm using the code format rather than the quote format because that's the only that I can figure out hon this forum. I guess I need a book on Qt forums also? Every one insists that i don't know C++ while I have proven that my syntax is correct. It's just a dead horse you're trying to beat. I know enough to know that I'll will never get an answer here because you are all die-hard loyalists to Qt who can't even accept its deficiencies. Well i guess this post will get me thrown off the forum, but it's just as well. Qt being what it is, i guess ya'll just love java!
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.
@The right answer have been given by VanDerSam. And I try to given reasons, but failed ;-(@
VanDerSam suggested that I use:
ui->lineEdit->setText(“Hello”); and I tried it and came up with about 13 errors. I know I copied and pasted it to replace the code I had written so I know that I used his syntax. Now what is it about that line that makes it the best solution?
Thanks,
Paul -
@1+1=2
If p3aul needs some book about C++ why than he started a thread here?
@p3aul
You've mentioned that events are from standard C++... AFAIK they are not. Furthermore, if you think that signals and slots are foolishness, then you just don't understand the idea of them.
And I didn't tell that you don't know C++. -
Wilk:
Mia culpa! you are right Events are a MS windows thing. C++ in the windows environment just makes use of them. Methods though are C++ (and Pascal/delphi which calls them functions) I guess I can sorta see why Qt would choose Slots and Signals I just wish they had chosen something a little more descriptive and a little less arcane! ;)Well it's not like I'm trying for a career as a professional programmer. I'm 70 years old and nobody would hire me anyway! But I used to Program! I learned on an NCR with 64K of memory. A lot back then!
I guess I'll stick with web programming Since the scripts are easier and I can even use them on my Linux machine, I guess I'll stick with that. -
AFAIK you use signals not to interact with mouse, but to interact with button. Button has it's own method for processing mouse events. When you push button, it processes this event and emits signal.
You have to use signals to get text into textbox because it's a more flexible way. As I've mentioned, you may totally change your UI, for example use text edit area instead of line edit. You may also use UI, created with desigber separate from your app, and then you will still be able to work with it just thanks to signal/slots.
Furthermore, in you example you may also try
@
#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() { QLineEdit *lineEdit = findChild <QLineEdit *> (); if (lineEdit) { lineEdit->setText("Hello"); } }
@
In this code you ask, if main window has line edit element, and if it does, you set it's text. -
-
[quote author="p3aul" date="1337397384"]Now, I ask; If that was the code needed to connect the button to the lineedit, why didn't Qt just put it there in the first place? Just to make me learn more about Qt and C++?[/quote]
It works, but it's really not a good way to do so.
-
I think it's so because Qt is a platform for building applications, it gives you some small independent blocks which you may use to build your own application. I have had the same problem as you have now. But then I've understood, that it's better not to use framework (Qt) classes directly, but create derived classes with behaviour you need.
-
[quote author="1+1=2" date="1337398002"][quote author="p3aul" date="1337397384"]Now, I ask; If that was the code needed to connect the button to the lineedit, why didn't Qt just put it there in the first place? Just to make me learn more about Qt and C++?[/quote]
It works, but it's really not a good way to do so.
[/quote]I guess better way is to find child by name. Even better is to find child and make connections in some method, and then call this method in constructor of main window. Am I wrong?
-
I don't know the reason behind finding the child anyway, but I like the idea of using a method anyway. Sounds more like I'm used to. I'm not going to write another "OpenOffice" type app. I write for my own pleasure and the pleasure I get from writing them. They are mostly simple, but in Delphi I have written front ends for databases.
What IS the reason for doing the code the way you did?(with searching for the child of lineEdit? and why didn't simply using lineEdit->setText("Hello"); work? Aren't you calling a method called setText() to change the property "text" of the object lineEdit? And you used a pointer in there too. It seems like an awful lot of trouble just to change a property value.
-
[quote author="p3aul" date="1337401988"]I don't know the reason behind finding the child anyway, but I like the idea of using a method anyway. Sounds more like I'm used to. I'm not going to write another "OpenOffice" type app. I write for my own pleasure and the pleasure I get from writing them. They are mostly simple, but in Delphi I have written front ends for databases.
What IS the reason for doing the code the way you did?(with searching for the child of lineEdit? and why didn't simply using lineEdit->setText("Hello"); work? Aren't you calling a method called setText() to change the property "text" of the object lineEdit? And you used a pointer in there too. It seems like an awful lot of trouble just to change a property value. [/quote]
Yes, your are right, it really seems not good. and in more than 99% cases, Qt user need not to use this.
http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
-
It is difficult for me to explain this , if you still not familiar with some basic C++ knowledge, such as Class, Friendship, Inheritance, Polymorphism, Templates, ... ;-). C++ is very very complex, much more that java, C#, etc, but Qt doesn't require user is a C++ expert.
[quote author="p3aul" date="1337443786"]
You said that Qt user would not use this. How would you code that simple example?
thanks,
Paul
[/quote]-
First, you design a GUI Window through Qt Creator or Qt Designer, which called mainwindow.ui And of course, C++ compiler can not deal with such a "non-standard" file
-
Then one of the Qt Tools uic was called.
@
uic mainwindow.ui -o ui_mainwindow.h
@
which convert your your designed GUI Element to a standard C++ file. All the elements are in a new Class called Ui::MainWindow. -
Then you need to use such a new Class in your code. There are more than 3 ways to do so and all of them are introduced in http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
BTW, Let try to see what you are doing now.
-
You are using The Single Inheritance Approach(Using a Pointer Member Variable) mentioned in above link. Which called Aggregation as a pointer member in Qt Creator.(See Menus Tools/Options/Designer ..., and you can selected a different one)
-
So you Create a class called MainWindow, which has a class member call ui that is a pointer to instance of Ui::MainWindow.
-
Then all of the Elements you designed can be access through the ui member in your class MainWindow.
@
ui->
@
Note that, all Of them are standard C++ code, you can deep into the ui_mainwindow.h file if you have interested.
OK, let's see what you are try to do.
- You have a class MainWindow more or less like this
@
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
@- Then you want to use a variable call lineedit in one of its member functions. Of course it will fail.
As it is not: - a global variable (doesn't defined outside all the functions and class)
- a class member variable (doesn't defined in your Mainwindow class)
- a member variable inherited from the parent class.(Not exists in QMainWindow)
- a local variable (doesn't defined in you member function)
…
...
Hope this will be helpful for you.
Regards,
Debao -