PDA

View Full Version : help with connect() without using QT creator



wenn32
16th July 2010, 17:19
Hello i am really having a bad time with connect().And i really need help

1) I want my program to take input via the QT text box and copy the value to a file

2) I want to take value from a text file every 10 seconds and update the value of the QT label

i have no idea how to do this:crying:.Please help me and these are for just learning!

franz
16th July 2010, 17:50
There's several examples in the Qt docs that can be combined to provide the functionality that you want. Maybe start with a little c++ docs if you aren't familiar with the language.

wenn32
16th July 2010, 18:07
this is the last thing i am trying to do.if you know any good examples please guide me
Thanks!

squidge
16th July 2010, 19:01
Paste the code you have so far.

wenn32
17th July 2010, 03:13
Its just for learning purpose i need some one help me understand the connect() so i asked some simple programs which will help me understand.

ChrisW67
17th July 2010, 05:22
I cannot help thinking this is a, "Can you do my homework?" type of request.

There are plenty of examples of the use of connect() in the docs that come with Qt. You could start with reading Signals and Slots (http://doc.trolltech.com/4.6/signalsandslots.html) and Address Book Tutorial (http://doc.trolltech.com/4.6/tutorials-addressbook.html). Then look at the signals emitted by objects of class QLineEdit and QTimer to see what is useful for your purposes.

When you have written some code, and still cannot get any joy, then come back with the code and questions about specific elements.

wenn32
18th July 2010, 12:46
so this much



#include <QApplication>
#include <QWidget>
#include <QLineEdit>


int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget window;
QLineEdit password("",&window);


password.move(480,750);
password.resize(370,30);
password.setEchoMode(QLineEdit::Password);
window.setWindowState(Qt::WindowFullScreen);


QObject::connect(&password,SIGNAL(returnPressed()),&password,SLOT(write_to_file()));
window.show();
return app.exec();
}



now how do i create my own slot so that what i type in the line edit and press return it can be written to a file

Zlatomir
18th July 2010, 13:36
To use signals and slots you will need objects.

So to create (define your own) new slots you need to create a class (maybe derive from another class), use Q_OBJECT macro in the class definition, and define the slots as member function (with "public slots:" access specifier)

In your case you will need a class derived form QWidget (or QMainWindow or QDialog depends on how you want that piece of widget to look like) that will have a QLineEdit as a member (maybe a QString, that depends if you want to use the string more than send it to file) and that slot that will take a QString as a parameter and write it to a file (here you will use the QFile (http://doc.qt.nokia.com/4.6/qfile.html) to do the write and read in binary or text file)

Try to make what i explained here and if you get into troubles than post the code that is not doing what you wanted to do and we will help you ;)

ahmdsd_ostora
18th July 2010, 13:45
To use signals and slots you will need objects.

So to create (define your own) new slots you need to create a class (maybe derive from another class), use Q_OBJECT macro in the class definition, and define the slots as member function (with "public slots:" access specifier)

In your case you will need a class derived form QWidget (or QMainWindow or QDialog depends on how you want that piece of widget to look like) that will have a QLineEdit as a member (maybe a QString, that depends if you want to use the string more than send it to file) and that slot that will take a QString as a parameter and write it to a file (here you will use the QFile (http://doc.qt.nokia.com/4.6/qfile.html) to do the write and read in binary or text file)

Try to make what i explained here and if you get into troubles than post the code that is not doing what you wanted to do and we will help you ;)


You told him to define a slot takes QString argument and didn't tell him how to pass this argument. He will need to use a QString argument signal not the returnPressed().

Zlatomir
18th July 2010, 13:52
@ahmdsd_ostora: good point
Also, there are a lot more "improvements" to that little "design" of a class, maybe he will need more stuff (like username too, etc...) so my advice was only to give him the direction where to start not the "final design" of his class.

wenn32
18th July 2010, 13:56
#include <QApplication>
#include <QWidget>
#include <QLineEdit>

class NewSlot : public QWidget
{
Q_OBJECT

private slots:
void open()
{
/* code to write to a file */
// i know this part.
}
};



int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget window;
QLineEdit password("",&window);


password.move(480,750);
password.resize(370,30);
password.setEchoMode(QLineEdit::Password);
window.setWindowState(Qt::WindowFullScreen);


QObject::connect(&password,SIGNAL(returnPressed()),&password,SLOT(write_to_file()));
window.show();
return app.exec();
}


ok can i normally create a object of the class NewSlot and call in connect() and by the way how do i get the data of the password(QLineedit) field


Thank you very much for helping me!

ahmdsd_ostora
18th July 2010, 13:58
@wenn32

read this:
http://doc.trolltech.com/4.6/signalsandslots.html

ahmdsd_ostora
18th July 2010, 14:02
#include <QApplication>
#include <QWidget>
#include <QLineEdit>

class NewSlot : public QWidget
{
Q_OBJECT

private slots:
void open()
{
/* code to write to a file */
// i know this part.
}
};



int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget window;
QLineEdit password("",&window);


password.move(480,750);
password.resize(370,30);
password.setEchoMode(QLineEdit::Password);
window.setWindowState(Qt::WindowFullScreen);


QObject::connect(&password,SIGNAL(returnPressed()),&password,SLOT(write_to_file()));
window.show();
return app.exec();
}


ok can i normally create a object of the class NewSlot and call in connect() and by the way how do i get the data of the password(QLineedit) field


Thank you very much for helping me!


Why not making your class the one which holds the password LineEdit, instead of the QWidget object ??
this will give your slot easy access to get the text

wenn32
18th July 2010, 14:06
yeah i already read that article but its hard for me to catch up as i am new to QT.

and i know that you guys might think its my home work and no its not.if it was home work i would done this in WIN32 normal GUI but i really want to learn cross platform GUI so guys i don't want anyone helping me with entire code just help me learn this only just a push then i can move easily :)

wenn32
18th July 2010, 14:12
Why not making your class the one which holds the password LineEdit, instead of the QWidget object ??
this will give your slot easy access to get the text

now i really need help please!

Lykurg
18th July 2010, 14:32
now i really need help please!
May I ask you a question befor: Are you familiar with C++ or is it also new to you?

squidge
18th July 2010, 14:41
EDIT: Really should refresh before replying :(

ahmdsd_ostora
18th July 2010, 16:18
now i really need help please!


it's easier than you imagine:



class MyScreen: public QWidget
{
Q_OBJECT

private:
QLineEdit* password;

private slots:
void write_to_file()
{
QString text = password->text();
// write it to your file
}

public:
MyScreen()
{
password = new QLineEdit("" , this);

password->move(480,750);
password->resize(370,30);
password->setEchoMode(QLineEdit::Password);
setWindowState(Qt::WindowFullScreen);

connect( password,SIGNAL(returnPressed()), this ,SLOT(write_to_file())
}


};


int main(int argc, char* argv[])
{

QApplication app(argc, argv);

NewSlot window;
window.show();

return app.exec();

}

Lykurg
18th July 2010, 16:21
instead of using move and resize, you really should use layouts: http://doc.trolltech.com/4.6/layout.html