LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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

Reply
 
LinkBack Search this Thread
Old 03-24-2011, 08:04 AM   #1
Felipe
Member
 
Registered: Oct 2006
Posts: 248

Rep: Reputation: 26
How to create an easy Qt application with Eclipse?


Hallo:

I'm trying to develop a Qt application with Eclipse.

What I've is what you see. A Form with a text, and 3 buttons:

http://picpaste.com/cambiar1-uxlrX6Ug.png



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?

Thanks
 
Old 03-24-2011, 08:17 AM   #2
rtmistler
Member
 
Registered: Mar 2011
Location: Milford, MA. USA
Distribution: MontaVista
Posts: 33

Rep: Reputation: 6
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.
 
Old 03-24-2011, 08:31 AM   #3
Felipe
Member
 
Registered: Oct 2006
Posts: 248

Original Poster
Rep: Reputation: 26
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");
}

Is right? Which file do I put it?


Thanks
 
Old 03-24-2011, 08:45 AM   #4
rtmistler
Member
 
Registered: Mar 2011
Location: Milford, MA. USA
Distribution: MontaVista
Posts: 33

Rep: Reputation: 6
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.

Example of something:

#include <QApplication>
#include <QLabel>
#include <QPushButton>

class MyPushButton : public QPushButton
{
public:
MyPushButton(const QString &text, QWidget *parent = 0) : QPushButton(text, parent) {}

int myValue;

protected:
void keyPressEvent(QKeyEvent *e)
{
... cod in here replaces the standard keyPressEvent, because I needed to change that
}

private slots:
void setValue(int v)
{
myValue = v;
}
};

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.

Can't help you with Eclipse, I don't use that.
 
Old 03-24-2011, 09:04 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707
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.

Last edited by MTK358; 03-24-2011 at 09:07 AM.
 
Old 03-24-2011, 09:20 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707
Here's a Qt Creator project that does what I understood you want, open it and play around with it:
Attached Files
File Type: txt stuff.tar.gz.txt (2.4 KB, 2 views)
 
Old 03-24-2011, 09:41 AM   #7
Felipe
Member
 
Registered: Oct 2006
Posts: 248

Original Poster
Rep: Reputation: 26
Trying to create the simple application in Eclipse...

I've added what I put in black:

change1.h
=====================
#ifndef CHANGE1_H
#define CHANGE1_H

#include <QtGui/QWidget>
#include "ui_change1.h"

class Change1 : public QWidget
{
Q_OBJECT

public:
Change1(QWidget *parent = 0);
~Change1();

public slots:
void changeText();

private:
Ui::Change1Class ui;
};

#endif // CHANGE1_H





change1.cpp
======================
#include "change1.h"

Change1::Change1(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}

Change1::~Change1()
{

}

void Change1::changeText()
{
text->setText("changed");
}





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


Any suggestion?

Thanks
 
Old 03-24-2011, 10:16 AM   #8
almatic
Member
 
Registered: Mar 2007
Distribution: Debian
Posts: 547

Rep: Reputation: 63
you need to "connect" a signal to the slot. Like this:
Code:
connect(changeButton, SIGNAL(clicked()), this, SLOT(changeText()));
use your names of course for the widgets.
also consider using code-tags when posting code, because that makes it much more readable.
 
Old 03-24-2011, 11:03 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707
@Felipe

Please use BBCode [code] tags.

Also, did you see my previous post?
 
Old 03-24-2011, 11:17 AM   #10
Felipe
Member
 
Registered: Oct 2006
Posts: 248

Original Poster
Rep: Reputation: 26
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?

Thanks and sorry by going so slow...

Here are my files:

change1.cpp
=========================

#include "change1.h"

Change1::Change1(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);

connect(ui.pushChange, SIGNAL(clicked()), this, SLOT(changeText()));

}

Change1::~Change1()
{

}

void Change1::changeText()
{
ui.text->setText("Changed");
}



Change1.h
==========================

#ifndef CHANGE1_H
#define CHANGE1_H

#include <QtGui/QWidget>
#include "ui_change1.h"

class Change1 : public QWidget
{
Q_OBJECT

public:
Change1(QWidget *parent = 0);
~Change1();

public slots:
void changeText();

private:
Ui::Change1Class ui;
};

#endif // CHANGE1_H


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!
********************************************************************************/

#ifndef UI_CHANGE1_H
#define UI_CHANGE1_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Change1Class
{
public:
QPushButton *pushDelete;
QPushButton *pushExit;
QLineEdit *text;
QPushButton *pushChange;

void setupUi(QWidget *Change1Class)
{
if (Change1Class->objectName().isEmpty())
Change1Class->setObjectName(QString::fromUtf8("Change1Class"));
Change1Class->resize(400, 300);
pushDelete = new QPushButton(Change1Class);
pushDelete->setObjectName(QString::fromUtf8("pushDelete"));
pushDelete->setGeometry(QRect(80, 160, 75, 24));
pushExit = new QPushButton(Change1Class);
pushExit->setObjectName(QString::fromUtf8("pushExit"));
pushExit->setGeometry(QRect(230, 200, 75, 24));
text = new QLineEdit(Change1Class);
text->setObjectName(QString::fromUtf8("text"));
text->setGeometry(QRect(130, 60, 113, 20));
pushChange = new QPushButton(Change1Class);
pushChange->setObjectName(QString::fromUtf8("pushChange"));
pushChange->setGeometry(QRect(80, 200, 75, 24));

retranslateUi(Change1Class);
QObject::connect(pushExit, SIGNAL(clicked()), Change1Class, SLOT(close()));
QObject::connect(pushDelete, SIGNAL(clicked()), text, SLOT(clear()));


QMetaObject::connectSlotsByName(Change1Class);
} // setupUi

void retranslateUi(QWidget *Change1Class)
{
Change1Class->setWindowTitle(QApplication::translate("Change1Class", "Change1", 0, QApplication::UnicodeUTF8));
pushDelete->setText(QApplication::translate("Change1Class", "delete", 0, QApplication::UnicodeUTF8));
pushExit->setText(QApplication::translate("Change1Class", "Exit", 0, QApplication::UnicodeUTF8));
#ifndef QT_NO_STATUSTIP
text->setStatusTip(QApplication::translate("Change1Class", "original", 0, QApplication::UnicodeUTF8));
#endif // QT_NO_STATUSTIP
text->setText(QApplication::translate("Change1Class", "original", 0, QApplication::UnicodeUTF8));
pushChange->setText(QApplication::translate("Change1Class", "Change", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class Change1Class: public Ui_Change1Class {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_CHANGE1_H


main.cpp
====================

#include "change1.h"

#include <QtGui>
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Change1 w;
w.show();
return a.exec();
}
 
Old 03-24-2011, 12:46 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707
Did you see what I wrote about [code] tags?!?
 
Old 03-24-2011, 04:29 PM   #12
Felipe
Member
 
Registered: Oct 2006
Posts: 248

Original Poster
Rep: Reputation: 26
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.

Sorry again.
 
Old 03-24-2011, 04:35 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707Reputation: 707
Code tags make the font monospaced, preserve indentation, and add a nice scroll box so the code doesn't clutter up the thread.

Use them like this when posting:

[code]
Put source code here
[/code]

It will look like this once posted:

Code:
Put source code here
 
  


Reply

Tags
eclipse, qt


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Create a mobile application for Android using Scala and Eclipse LXer Syndicated Linux News 0 07-06-2009 09:11 PM
LXer: Easy Ruby development, the Eclipse way LXer Syndicated Linux News 0 08-14-2008 07:42 PM
LXer: ARM Performance Monitoring Made Easy with Eclipse LXer Syndicated Linux News 0 02-09-2007 03:21 PM
LXer: Make Ruby on Rails Easy With RadRails eclipse IDE LXer Syndicated Linux News 0 09-20-2006 01:54 AM
Fedora and Eclipse-No Application Window Decorations fortezza Linux - Software 2 07-19-2006 08:19 AM


All times are GMT -5. The time now is 05:39 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration