How to create an easy Qt application with Eclipse?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
A button is for exit application, another is for clear text field and the third is for changing text.
The first two buttons work fine, but the third doesn't.
The only I want for the change button is to change text "original" by "modified".
- How do I do this? With a function and where? Is a new slot?
Short answer, you need to create a slot and connect it to that button and write the contents of the slot function.
I have found that there are two or more schools of thought on UI design. Straight coding and design tools. I'm not a proponent of design tools, they create a ton of related files, abstract things to insane levels.
One suggestion if you are a programmer, or so inclined; is to pick up a reference: "C++ GUI Programming with Qt 4", Jasmin Blanchette, Mark Summerfield, Prentice Hall 2008, I have a second edition, maybe there's later.
Yes, a lot of this is online, all of it in fact. Having a printed reference showing the basics is important to me because the location for all the referential information is always where I put that sticky note and it never expires or becomes an invalid link, or I forget where I stored that PDF file.
The first 2-4 chapters of this book tell you how to write simple, straight-forward code on how to make buttons, text boxes, slider bars, and lay it all out on your window properly. The great part about it also is that it takes the ensuing example code they show you and goes line by line, section by section to tell you "this line does 'this'", and so forth.
Thanks for your fast reply, but I'm newbie with this: eclipse, Qt and not an expert with C++ (I used to develop with C and Java).
Using Eclipse. Can any tell me:
- How to create a slot for the function changeText?. Where do I've to write it to be available form the "Signal Slot Editor" (if really is better to configure it as a slot).
- I suppose that the code to write is something like:
void Change1::changeText()
{
text->setText("changed");
}
That code appears to be correct. You put it in a source file that you're compiling. THAT's why I don't use design tools like that, it created like 5 or 10 files for you didn't it?
You can have a main.cpp and a main.h and that's all you need, in fact you only need a main.cpp.
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QLabel *label = new QLabel("My Program");
label->show();
MyPushButton *mybutton = new MyPushButton("Button");
QObject::connect(mybutton, SIGNAL(clicked()), &app, SLOT(setValue()));
mybutton->show();
return app.exec();
}
This was candidly coded, I think it's right but didn't spend the time to compile and test it, just FYI
As I say though, you don't need a multitude of file to get this done.
In a directory, put your main.cpp and nothing else.
Type "qmake -project" to make a Qt project file, it will follow the directory name. You can edit that file to change the executable name if you wish, take a look at it, it'll be a simple file.
Next type "qmake -makefile" and it will make a Makefile for you.
Next type "make" and it will make your executable, providing your syntax is fine.
From there, you can code with more variety, read references for better examples, Qt examples gives you a lot.
You don't need to be a C++ expert, I've done C and assembler for 25 years, have run into C++ over the last 15 or so years, but it's not great for embedded, really C++ in my world is file names, a little bit of definitions. Otherwise it's all C internals. You still have printf(), sprintf(), fopen(), etc in there, and there are also C++ equivalents.
If you purchased Qt, then you have support from them. Open a case, they "have" been great, but they just got bought and I don't know if they're going downhill. But I could previously send them entire code clips, they'd figure it out and send me back a working example that usually got me where I needed to be.
Qt Designer actually creates 2 extra files, not 5-10 (one is an XML description of the interface, the other is the generated C++ code). And IMO is doesn't abstract everything out to insane levels. I'm not saying you have to use it o rthat it's the only way to go, but I think it's a great tool, and that it's much easier to manage two extra files than hundreds of lines of code to create/lay out widgets.
Also, unless you have a very good reason to use Eclipse, you should switch to Qt Creator. It's designed just for working with Qt (and has a lot of helpful features), and it's much more lightweight, too.
But still I've two problems:
- I'm unable to find a Slot in Change1Class with name changeText(),
- Even not assigning the signal, an error is displayed when running the application:
This is what Eclipse console displays:
make debug
make -f Makefile.Debug
make[1]: se ingresa al directorio `/home/athlon1/workspace2/Change1'
/usr/bin/uic change1.ui -o ui_change1.h
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/default -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -Idebug -I. -o debug/main.o main.cpp
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/default -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -Idebug -I. -o debug/change1.o change1.cpp
change1.cpp: In member function ‘void Change1::changeText()’:
change1.cpp:16:2: error: ‘text’ was not declared in this scope
make[1]: *** [debug/change1.o] Error 1
make[1]: se sale del directorio `/home/athlon1/workspace2/Change1'
make: *** [debug] Error 2
Now it works, but not sure why.
Even it works, I'd like to make work it in a maintenance way.
As i say, i use Eclipse, so why:
- If I've defined changeText() as public slot, why can't use Eclipse "Qt c++ Signal Slot Editor" to join the signal to the slot?.
- Why do I have to define "connect(ui.pushChange, SIGNAL(clicked()), this, SLOT(changeText()));" in change1.cpp instead of defining it in ui_change1.h as the other signals?
ui_change1.h
=====================
/********************************************************************************
** Form generated from reading UI file 'change1.ui'
**
** Created
** by: Qt User Interface Compiler version 4.6.3
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
Sorry, sorry.
I've only seen once the reference to the code tags.
I don't use them because I don't know how to use them and don't know what are they for.
That's why I've tried do use bold and different colors.
If you can tell me where to find information about the code tags, I won't write any more until having read it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.