PDA

View Full Version : Signals and Slots



merry
22nd February 2007, 08:06
Hi all

How can we make connections manually using Signals and Slots.

Thanx & Regards
merry

sarode
22nd February 2007, 08:16
Hello merry,

You can use thse functions of QObject class



QObject::connect(sender, SIGNAL(signal(...,)), receiver, SLOT(slot(...,)));


To disconnect the signals and slots



QObject::disconnect(sender, SIGNAL(signal(...,)), receiver, SLOT(slot(...,)));


You can take this example..


......
QApplication a(argc, argv);
QWidget w;
a.setMainWidget(&w);
QPushButton b(&w);

QObject::connect(&b, SIGNAL(clicked()), &a, SLOT(quit()));
..........



The code i have written for Qt 3.3.3 and is same to Qt 4 also..
This is just to use the signals and slots already availiable.. To create custom slots and signals we have a procedure.

Regards,

December
22nd February 2007, 08:17
Can you be a little more specific about what exactly you want to do?

Signals can be connected to slots using connect...



connect( aButton, SIGNAL(clicked()), SLOT(myUsefulSlot()) );


In that example (from the docs) aButton is the object emitting the signal, clicked() is the name of the signal we are interested in, and myUsefulSignal() is the slot in our program we want it connected to.

You define slots in your header with the slot keyword:



public slots:
void myUsefulSlot();


Hope that helps.

merry
22nd February 2007, 08:34
Thanks for the reply

I am sending you the code with this code a treestructure/listview of files & folders is Created and also both files and folders are represented by different different images.

Now what I want is that When I clicked on any of the item of the listview it can be replaced by some other image. For this I Use signals and slots but it wont works.It wont give any error but at click event nothing happens,

View this code and reply me with the solution :

#include <qfiledialog.h>
#include <qlistview.h>
#include <qstring.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qstringlist.h>
#include<qmessagebox.h>
#include <qpixmap.h>
#include <qdir.h>
#include <qstringlist.h>



void Form1::fileOpen()
{
QListViewItem* root=new QListViewItem(listView);
m_source = QFileDialog::getExistingDirectory(
"/home/",
this,
"get existing directory",
"Choose a directory",
TRUE );
root->setText(0,m_source );
CreateDirTree(root,m_source);
}

void Form1::CreateDirTree(QListViewItem *item,QString path )
{
QDir dir(path);

const QFileInfoList * dirs=dir.entryInfoList();
{
if(dirs)

{
QFileInfoListIterator it(*dirs);
QFileInfo * fileInfo;
while(fileInfo=it.current())
{
++it;

if(fileInfo->fileName() =="." || fileInfo->fileName() =="..")
;
else if (fileInfo->isDir())
{
QString FilePath;
FilePath =path +"/"+ fileInfo->fileName();
m_temp = new QListViewItem(item);
m_temp->setText(0,fileInfo->fileName());
m_temp->setPixmap(0,QPixmap("/root/qt/Tree/images/image1.png"));
CreateDirTree(m_temp,FilePath);



}



else
{
m_temp = new QListViewItem(item);
m_temp->setText(0,fileInfo->fileName());
m_temp->setPixmap(0,QPixmap("/root/qt/Tree/images/pic1.png"));



}

}


}

}

connect(listView,SIGNAL(clicked(QListViewItem*)),t his,SLOT(reverse_images(QlistViewItem*)));

}


void Form1::reverse_images(QListViewItem* item)
{
item->setPixmap(0,QPixmap("/root/qt/Tree/images/pic2.png"));
}




Thanx

merry

jpn
22nd February 2007, 09:11
Does the declaration of Form1 have the required Q_OBJECT macro? Is reverse_images() declared as a slot? What does the connect statement return?

Please

use the code tags to make the code readable
post only relevant parts of the code