Results 1 to 5 of 5

Thread: Signals and Slots

  1. #1
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Signals and Slots

    Hi all

    How can we make connections manually using Signals and Slots.

    Thanx & Regards
    merry

  2. #2
    Join Date
    May 2006
    Posts
    32
    Thanks
    1
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals and Slots

    Hello merry,

    You can use thse functions of QObject class

    Qt Code:
    1. QObject::connect(sender, SIGNAL(signal(...,)), receiver, SLOT(slot(...,)));
    To copy to clipboard, switch view to plain text mode 

    To disconnect the signals and slots

    Qt Code:
    1. QObject::disconnect(sender, SIGNAL(signal(...,)), receiver, SLOT(slot(...,)));
    To copy to clipboard, switch view to plain text mode 

    You can take this example..
    Qt Code:
    1. ......
    2. QApplication a(argc, argv);
    3. a.setMainWidget(&w);
    4.  
    5. QObject::connect(&b, SIGNAL(clicked()), &a, SLOT(quit()));
    6. ..........
    To copy to clipboard, switch view to plain text mode 

    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,

  3. The following user says thank you to sarode for this useful post:

    merry (22nd February 2007)

  4. #3
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signals and Slots

    Can you be a little more specific about what exactly you want to do?

    Signals can be connected to slots using connect...

    Qt Code:
    1. connect( aButton, SIGNAL(clicked()), SLOT(myUsefulSlot()) );
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. public slots:
    2. void myUsefulSlot();
    To copy to clipboard, switch view to plain text mode 

    Hope that helps.

  5. The following user says thank you to December for this useful post:

    merry (22nd February 2007)

  6. #4
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Signals and Slots

    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

  7. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Signals and Slots

    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
    J-P Nurmi

Similar Threads

  1. Signals and Slots question
    By Thoosle in forum Qt Programming
    Replies: 5
    Last Post: 5th December 2006, 01:24
  2. Nested signals and slots
    By vishva in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2006, 10:47
  3. Signals and Slots in dll
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2006, 09:12
  4. Order of signals and slots
    By Morea in forum Newbie
    Replies: 1
    Last Post: 26th February 2006, 23:09
  5. Problem with Signals and Slots
    By Kapil in forum Newbie
    Replies: 11
    Last Post: 15th February 2006, 12:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.